Skip to content

Commit

Permalink
バトルゾーン
Browse files Browse the repository at this point in the history
  • Loading branch information
kurumayusamugariko committed Feb 10, 2024
1 parent ab4c525 commit 187213d
Show file tree
Hide file tree
Showing 10 changed files with 1,057 additions and 56 deletions.
805 changes: 805 additions & 0 deletions battlezone.json

Large diffs are not rendered by default.

27 changes: 21 additions & 6 deletions classes.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,22 @@
//playerのアニメーション
class Sprite {
constructor({ position, velocity, image, frames = { max: 1 } }) {
constructor({ position, velocity, image, frames = { max: 1 }, sprites }) {
this.position = position;
this.image = image;
this.frames = frames;
this.frames = { ...frames, val: 0, elapsed: 0 };

this.image.onload = () => {
this.width = this.image.width / this.frames.max;
this.height = this.image.height;
};
this.moving = false;
this.sprites = sprites;
}

draw() {
c.drawImage(
this.image,
0,
this.frames.val * this.width,
0,
this.image.width / this.frames.max,
this.image.height,
Expand All @@ -23,6 +25,19 @@ class Sprite {
this.image.width / this.frames.max,
this.image.height
);

if (!this.moving) return

if (this.frames.max > 1) {
this.frames.elapsed++;
}

//フレームの切り替え
if (this.frames.elapsed % 10 === 0) {
if (this.frames.val < this.frames.max - 1) this.frames.val++;
else this.frames.val = 0;
}

}
}

Expand All @@ -34,9 +49,9 @@ class Boundary {
this.width = 40;
this.height = 20;
}
//当たり判定の描画
//当たり判定の描画
draw() {
c.fillStyle = "rgba(255, 0, 0, 0)";//透明
c.fillStyle = "rgba(255, 0, 0, 0.2)"; //透明
c.fillRect(this.position.x, this.position.y, this.width, this.height);
}
}
}
61 changes: 61 additions & 0 deletions data/battleZones.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions game2.html
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
<body>
<h1>Nomatem</h1>
<canvas></canvas>
<script src ="data/battleZones.js"></script>
<script src ="data/collisions.js"></script>
<script src ="classes.js"></script>
<script src ="game2.js"></script>
Expand Down
62 changes: 58 additions & 4 deletions game2.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,11 @@ for (let i = 0; i < collisions.length; i += 80) {
collisionsMap.push(collisions.slice(i, 80 + i));
}

const battleZonesMap = [];
for (let i = 0; i < battleZonesData.length; i += 80) {
battleZonesMap.push(battleZonesData.slice(i, 80 + i));
}

const boundaries = [];
const offset = {
x: -544,
Expand All @@ -29,6 +34,24 @@ collisionsMap.forEach((row, i) => {
});
});

const battleZones = [];

battleZonesMap.forEach((row, i) => {
row.forEach((symbol, j) => {
if (symbol === 1025)
battleZones.push(
new Boundary({
position: {
x: j * Boundary.width + offset.x,
y: i * Boundary.height + offset.y,
},
})
);
});
});

console.log(battleZones);

//map画像objを作成
const image = new Image();
image.src = "./public/metamon/metown.png";
Expand All @@ -38,19 +61,34 @@ const foregroundImage = new Image();
foregroundImage.src = "./public/metamon/foreground.png";

//player画像objを作成
const playerImage = new Image();
playerImage.src = "./public/metamon/player/playerDown.png";
const playerDownImage = new Image();
playerDownImage.src = "./public/metamon/player/playerDown.png";

const playerUpImage = new Image();
playerUpImage.src = "./public/metamon/player/playerUp.png";

const playerLeftImage = new Image();
playerLeftImage.src = "./public/metamon/player/playerLeft.png";

const playerRightImage = new Image();
playerRightImage.src = "./public/metamon/player/playerRight.png";

//playerの初期位置
const player = new Sprite({
position: {
x: canvas.width / 2 - 192 / 4 / 2,
y: canvas.height / 2 - 68 / 2,
},
image: playerImage,
image: playerDownImage,
frames: {
max: 4,
},
sprites: {
up: playerUpImage,
left: playerLeftImage,
right: playerRightImage,
down: playerDownImage,
}
});

const background = new Sprite({
Expand Down Expand Up @@ -84,7 +122,7 @@ const keys = {
},
};

const moveables = [background, ...boundaries, foreground];
const moveables = [background, ...boundaries, foreground, ...battleZones];

function rectangularCollision({ rectangle1, rectangle2 }) {
return (
Expand All @@ -101,12 +139,19 @@ function animate() {
boundaries.forEach((boundary) => {
boundary.draw();
});
battleZones.forEach(battleZone =>{
battleZone.draw();
});
player.draw();
foreground.draw();

let moving = true;
player.moving = false;
//playerの移動。まぁ動かしてるのは背景だけど
if (keys.w.pressed && lastKey === "w") {
player.moving = true;
player.image = player.sprites.up;

for (let i = 0; i < boundaries.length; i++) {
const boundary = boundaries[i];
//player当たり判定
Expand All @@ -133,6 +178,9 @@ function animate() {
moveable.position.y += 3;
});
} else if (keys.a.pressed && lastKey === "a") {
player.moving = true;
player.image = player.sprites.left;

for (let i = 0; i < boundaries.length; i++) {
const boundary = boundaries[i];
//player当たり判定
Expand All @@ -159,6 +207,9 @@ function animate() {
moveable.position.x += 3;
});
} else if (keys.s.pressed && lastKey === "s") {
player.moving = true;
player.image = player.sprites.down;

for (let i = 0; i < boundaries.length; i++) {
const boundary = boundaries[i];
//player当たり判定
Expand All @@ -185,6 +236,9 @@ function animate() {
moveable.position.y -= 3;
});
} else if (keys.d.pressed && lastKey === "d") {
player.moving = true;
player.image = player.sprites.right;

for (let i = 0; i < boundaries.length; i++) {
const boundary = boundaries[i];
//player当たり判定
Expand Down
Loading

0 comments on commit 187213d

Please sign in to comment.