From c4aeab6ec5b0722c89bea65d9644d89b91cc2f67 Mon Sep 17 00:00:00 2001 From: adeliptr Date: Tue, 6 May 2025 23:13:43 +0900 Subject: [PATCH] Throwable working --- Cat.js | 62 +++++++++++++++++++++++++--------------------------- GameScene.js | 30 ++++++++++++++++++------- Mouse.js | 28 +++++++++++++++++------- Throwable.js | 31 ++++++++++++++++++++++++++ 4 files changed, 103 insertions(+), 48 deletions(-) create mode 100644 Throwable.js diff --git a/Cat.js b/Cat.js index 3a34f51..c329d64 100644 --- a/Cat.js +++ b/Cat.js @@ -1,8 +1,9 @@ import { gameFrame } from './prototype.js'; import { catAnimation, imageAssets } from './sketch.js'; -import { grid, cheeses, activeMice, calculateCell } from './GameScene.js'; +import { grid, cheeses, activeMice, calculateCell, mouseGroup, throwableGroup } from './GameScene.js'; +import { Yarn, Snowball } from './Throwable.js'; -export const movingObjects = []; +export const throwables = []; export const catAniDesc = { chefCat: { idle: { row: 0, frames: 4, frameSize: [200, 200], frameDelay: 10 }, @@ -33,9 +34,9 @@ export class Cat { this.sprite.spriteSheet = spriteSheet; this.sprite.addAnis(ani); this.sprite.collider = 'static'; + this.sprite.collides(mouseGroup); + this.sprite.overlaps(throwableGroup); this.sprite.layer = 1; - console.log(this.spriteSheet); - console.log(this.sprite.animation); this.sprite.changeAni('idle'); this.active = false; @@ -137,16 +138,15 @@ export class SingleYarnCat extends Cat { else this.switchToIdle(); if (this.active && (millis() - this.lastShot > 3000)) { - const yarn = createSprite(this.x + gameFrame.tileWidth / 2, this.y, 20, 20); - yarn.image = imageAssets.yarn; - yarn.scale = gameFrame.tileWidth / 1024; - yarn.vel.x = 1; - yarn.life = 600; - const newMovingObject = { - sprite: yarn, - point: 15 + let yarnX = this.x + gameFrame.tileWidth / 2; + let yarnY = this.y; + + const yarn = new Yarn(yarnX, yarnY); + if (yarn) { + throwables.push(yarn); + throwableGroup.add(yarn.sprite); } - movingObjects.push(newMovingObject); + this.lastShot = millis(); } } @@ -165,18 +165,18 @@ export class DoubleYarnCat extends Cat { else this.switchToIdle(); if (this.active && (millis() - this.lastShot > 3000)) { + // TODO: check on the offset again for (let offset of [0, 20]) { - const yarn = createSprite(this.x + gameFrame.tileWidth / 2 + offset, this.y, 20, 20); - yarn.image = imageAssets.yarn; - yarn.scale = gameFrame.tileWidth / 1024; - yarn.vel.x = 1; - yarn.life = 600; - const newMovingObject = { - sprite: yarn, - point: 15 + let yarnX = this.x + gameFrame.tileWidth / 2 + offset; + let yarnY = this.y; + + const yarn = new Yarn(yarnX, yarnY); + if (yarn) { + throwables.push(yarn); + throwableGroup.add(yarn.sprite); } - movingObjects.push(newMovingObject); } + this.lastShot = millis(); } } @@ -191,7 +191,6 @@ export class SleepyCat extends Cat { } action(targetMouse) { - console.log(`do i get here`); if (this.awake) { this.changeAni('action'); this.wakeStart = millis(); @@ -221,16 +220,15 @@ export class IceCat extends Cat { else this.switchToIdle(); if (this.active && (millis() - this.lastShot > 3000)) { - const snowball = createSprite(this.x + gameFrame.tileWidth / 2, this.y, 20, 20); - snowball.image = imageAssets.snowball; - snowball.scale = gameFrame.tileWidth / 1024; - snowball.vel.x = 1; - snowball.life = 600; - const newMovingObject = { - sprite: snowball, - point: 20 + const snowballX = this.x + gameFrame.tileWidth / 2; + const snowballY = this.y; + + const snowball = new Snowball(snowballX, snowballY) + if (snowball) { + throwables.push(snowball); + throwableGroup.add(snowball.sprite); } - movingObjects.push(newMovingObject); + this.lastShot = millis(); } } diff --git a/GameScene.js b/GameScene.js index 626e939..7b2c9ad 100644 --- a/GameScene.js +++ b/GameScene.js @@ -1,7 +1,7 @@ import { prototypeFrame, gameFrame } from './prototype.js'; -import { imageAssets, catImages, catAnimation, selectedCatType, resetCatType } from './sketch.js'; -import { Cat, ChefCat, SingleYarnCat, DoubleYarnCat, SleepyCat, IceCat } from './Cat.js'; -import { Mice, BasicMouse, HelmetMouse } from './Mouse.js'; +import { imageAssets, selectedCatType, resetCatType } from './sketch.js'; +import { ChefCat, SingleYarnCat, DoubleYarnCat, SleepyCat, IceCat, throwables } from './Cat.js'; +import { BasicMouse, HelmetMouse } from './Mouse.js'; import { level1Mice } from './level/level1.js'; import { RobotVacuum } from './RobotVacuum.js'; @@ -18,6 +18,7 @@ export let cheeses = []; export let grid = Array(5).fill().map(() => Array(9).fill(null)); let startTime; let levelMice = [...level1Mice]; +export let catGroup, mouseGroup, throwableGroup; function createCat(type, x, y) { switch (type) { @@ -38,12 +39,12 @@ function createCat(type, x, y) { } } -function createMouse(type, x, y) { +function createMouse(type, x, y, row) { switch (type) { case 'basicMouse': - return new BasicMouse(x, y); + return new BasicMouse(x, y, row); case 'helmetMouse': - return new HelmetMouse(x, y); + return new HelmetMouse(x, y, row); default: return undefined; } @@ -86,7 +87,7 @@ export function GameScene() { let vacuum = new RobotVacuum(x, y, row); - gameSprites.push(vacuum); + gameSprites.push(vacuum.sprite); robotVacuums.push(vacuum); } @@ -106,6 +107,10 @@ export function GameScene() { }) gameFrame.catRatio = 1.2 * gameFrame.tileWidth/200; + + catGroup = new Group(); + mouseGroup = new Group(); + throwableGroup = new Group(); } this.draw = function() { @@ -142,6 +147,13 @@ export function GameScene() { vacuum.action(); } }) + + throwables.forEach((throwable) => { + if (throwable.sprite.overlaps(currMouse.sprite)) { + currMouse.attacked(throwable.point); + throwable.remove(); + } + }) } } } @@ -176,6 +188,7 @@ export function GameScene() { newCat.sprite.scale = gameFrame.catRatio; grid[row][col] = newCat; activeCats.push(newCat); + catGroup.add(newCat.sprite); gameSprites.push(newCat.sprite); // Is this redundant? kedouble2 if (newCat instanceof SleepyCat) sleepyCats.push(newCat); resetCatType(); @@ -239,10 +252,11 @@ function spawnMouse(type, row) { let x = width; let y = gameFrame.padding_up + row * gameFrame.tileHeight + gameFrame.tileHeight / 2; - let newMouse = new createMouse(type, x, y); + let newMouse = new createMouse(type, x, y, row); if (newMouse) { newMouse.sprite.scale = gameFrame.catRatio; activeMice[row].push(newMouse); + mouseGroup.add(newMouse.sprite); gameSprites.push(newMouse.sprite); // Is this redundant? kedouble2 sama allSprites } } \ No newline at end of file diff --git a/Mouse.js b/Mouse.js index 9bb95df..9589446 100644 --- a/Mouse.js +++ b/Mouse.js @@ -1,12 +1,14 @@ import { gameFrame } from './prototype.js'; import { imageAssets } from './sketch.js'; +import { activeMice } from './GameScene.js'; -export class Mice { - constructor(x, y, speed, HP, AP, img, width) { +export class Mouse { + constructor(x, y, row, speed, HP, AP, img, width) { this.sprite = createSprite(x, y, width, width); this.sprite.image = img; this.sprite.layer = 3; this.sprite.velocity.x = speed; + this.row = row; this.HP = HP; this.AP = AP; this.width = width; @@ -14,17 +16,27 @@ export class Mice { remove() { this.sprite.remove(); + const index = activeMice[this.row].indexOf(this); + if (index != -1) { + activeMice[this.row].splice(index, 1); + } + // console.log(`there are now ${activeMice[this.row].length} mice in row ${this.row}`) + } + + attacked(point) { + this.HP -= point; + if (this.HP <= 0) this.remove(); } } -export class BasicMouse extends Mice { - constructor(x, y) { - super(x, y, -0.25, 100, 20, imageAssets.mouse, gameFrame.tileWidth); +export class BasicMouse extends Mouse { + constructor(x, y, row) { + super(x, y, row, -0.25, 100, 20, imageAssets.mouse, gameFrame.tileWidth); } } -export class HelmetMouse extends Mice { - constructor(x, y) { - super(x, y, -0.25, 150, 20, imageAssets.mouse, gameFrame.tileWidth); +export class HelmetMouse extends Mouse { + constructor(x, y, row) { + super(x, y, row, -0.25, 150, 20, imageAssets.mouse, gameFrame.tileWidth); } } \ No newline at end of file diff --git a/Throwable.js b/Throwable.js new file mode 100644 index 0000000..249ffd5 --- /dev/null +++ b/Throwable.js @@ -0,0 +1,31 @@ +import { gameFrame } from "./prototype.js"; +import { imageAssets } from "./sketch.js"; + +export class Throwable { + constructor(x, y, point, img, width) { + this.sprite = createSprite(x, y, width, width); + this.sprite.image = img; + // TODO: check on the scale again + this.sprite.scale = gameFrame.tileWidth / 1024; + this.sprite.vel.x = 1; + this.sprite.life = 600; + this.point = point; + this.width = width; + } + + remove() { + this.sprite.remove(); + } +} + +export class Yarn extends Throwable { + constructor(x, y) { + super(x, y, 15, imageAssets.yarn, gameFrame.tileWidth / 4); + } +} + +export class Snowball extends Throwable { + constructor(x, y) { + super(x, y, 20, imageAssets.snowball, gameFrame.tileWidth / 4); + } +} \ No newline at end of file