xxxxxxxxxx
276
// camera
// bullet hell style enemy firing patterns
// can walk away from selected and still pick up - bug
//
let num_rows, num_cols;
let cell_size, half_cell;
let draw_cell_size;
let dirty = true;
let bg_col;
let player, enemy;
let entities;
let game_map;
let sprite_sheet;
function preload() {
sprite_sheet = loadImage("monochrome-transparent_packed.png");
}
function setup() {
noiseDetail(8, 0.15);
num_rows = 24;
num_cols = 36;
cell_size = 16; //24;
draw_cell_size = cell_size * 2;
half_cell = draw_cell_size / 2;
createCanvas(num_cols * draw_cell_size, num_rows * draw_cell_size);
bg_col = color(20);
game_map = {};
let grid = [];
let open_cells = [];
for (let r = 0; r < num_rows; r++) {
grid[r] = [];
for (let c = 0; c < num_cols; c++) {
let n = noise(c * 0.1, r * 0.1);
let ch = " ";
if (n < 0.25) ch = "#";
if (ch == " ") open_cells.push(createVector(c, r));
grid[r][c] = { ch: ch, col: color(220) };
}
}
game_map.grid = grid;
game_map.cells = open_cells;
entities = [];
player = {};
player.pos = random(open_cells); //createVector(5,5);
player.inventory = {};
player.last_bumped = createVector(-1, -1); // track what the player wants to carve out
player.anim_frame = 0;
player.draw = () => {
drawPlayer();
};
entities.push(player);
enemy = {};
enemy.pos = random(open_cells);
enemy.anim_frame = 0;
enemy.draw = () => {
drawEnemies();
};
entities.push(enemy);
for (let r = 3; r < 7; r++) {
for (let c = 3; c < 7; c++) {
game_map.grid[r][c].ch = " ";
}
}
textSize(draw_cell_size);
textFont("Consolas");
textAlign(CENTER, CENTER);
frameRate(10);
}
function draw() {
if (dirty) {
background(bg_col);
drawMap();
dirty = false;
}
// animated tiles
// drawEnemies();
// drawPlayer();
for (let e of entities) {
e.draw();
// e.update();
}
if (keyIsPressed) {
if (key === "f") {
//testing firing
let f = {};
f.pos = player.pos;
f.type = "fireball";
f.life = 5;
f.draw = () => {}
entities.push(f);
}
if (
key === "p" &&
player.last_bumped.x != -1 &&
player.last_bumped.y != -1
) {
if (game_map.grid[player.last_bumped.y][player.last_bumped.x].ch != " ") {
// player.inventory.push(grid[player.r][player.c].ch);
if (
player.inventory[
game_map.grid[player.last_bumped.y][player.last_bumped.x].ch
] === undefined
) {
player.inventory[
game_map.grid[player.last_bumped.y][player.last_bumped.x].ch
] = 1;
} else {
player.inventory[
game_map.grid[player.last_bumped.y][player.last_bumped.x].ch
]++;
}
game_map.grid[player.last_bumped.y][player.last_bumped.x].ch = " ";
game_map.grid[player.last_bumped.y][
player.last_bumped.x
].selected = false;
player.last_bumped.x = -1;
player.last_bumped.y = -1;
console.log(player.inventory);
}
}
let player_move = createVector(0, 0);
if (key === "ArrowDown") player_move.y = 1;
if (key === "ArrowUp") player_move.y = -1;
if (key === "ArrowLeft") player_move.x = -1;
if (key === "ArrowRight") player_move.x = 1;
if (player_move.x == 0 && player_move.y == 0) {
}
if (tryMove(player, player_move)) player.pos.add(player_move);
else if (tryBump(player, player_move)) {
player.last_bumped = p5.Vector.add(player.pos, player_move);
game_map.grid[player.last_bumped.y][player.last_bumped.x].selected = true;
}
// player.pos.x = constrain(player.pos.x, 0, num_cols-1);
// player.pos.y = constrain(player.pos.y, 0, num_rows-1);
// console.log(player, player_move, key);
dirty = true;
}
}
function tryMove(e, n) {
let next = p5.Vector.add(e.pos, n);
if (inRange(next)) {
if (game_map.grid[next.y][next.x].ch != "#") return true;
}
return false;
}
function tryBump(e, n) {
let next = p5.Vector.add(e.pos, n);
if (inRange(next)) {
if (game_map.grid[next.y][next.x].ch == "#") {
if (e.last_bumped.x != -1 && e.last_bumped.y != -1)
// uncolor the last one
game_map.grid[e.last_bumped.y][e.last_bumped.x].selected = false;
return true;
}
}
return false;
}
function inRange(e) {
if (e.x < 0 || e.x > num_cols - 1 || e.y < 0 || e.y > num_rows - 1)
return false;
return true;
}
function drawPlayer() {
fill(20);
noStroke();
rect(
player.pos.x * draw_cell_size,
player.pos.y * draw_cell_size,
draw_cell_size,
draw_cell_size
);
drawSprite("@", player.pos.x, player.pos.y, player.anim_frame);
player.anim_frame++;
if (player.anim_frame > 1) player.anim_frame = 0;
// fill(color(255, 0, 255));
// text(
// "@",
// half_cell + player.pos.x * cell_size,
// half_cell + player.pos.y * cell_size
// );
}
function drawEnemies() {
fill(20);
noStroke();
rect(
enemy.pos.x * draw_cell_size,
enemy.pos.y * draw_cell_size,
draw_cell_size,
draw_cell_size
);
drawSprite("O", enemy.pos.x, enemy.pos.y, enemy.anim_frame);
enemy.anim_frame++;
if (enemy.anim_frame > 1) enemy.anim_frame = 0;
}
function drawSprite(ch, c, r, anim_frame = 0) {
if (ch != " ") {
if (anim_frame == 0)
image(
sprite_sheet,
c * draw_cell_size,
r * draw_cell_size,
draw_cell_size,
draw_cell_size,
SPRITES[ch].c * cell_size,
SPRITES[ch].r * cell_size,
cell_size,
cell_size
);
else
image(
sprite_sheet,
c * draw_cell_size,
r * draw_cell_size,
draw_cell_size,
draw_cell_size * 0.95,
SPRITES[ch].c * cell_size,
SPRITES[ch].r * cell_size,
cell_size,
cell_size
);
}
}
function drawMap() {
// stroke(color(40))
for (let r = 0; r < num_rows; r++) {
for (let c = 0; c < num_cols; c++) {
if (game_map.grid[r][c].selected) {
stroke(color(255, 0, 255));
noFill();
rect(
c * draw_cell_size,
r * draw_cell_size,
draw_cell_size,
draw_cell_size
);
}
drawSprite(game_map.grid[r][c].ch, c, r);
// fill(game_map.grid[r][c].col);
// text(game_map.grid[r][c].ch, half_cell+c*cell_size,half_cell+r*cell_size);
}
}
}