Throwable working

This commit is contained in:
adeliptr 2025-05-06 23:13:43 +09:00
parent 1569132f8d
commit c4aeab6ec5
4 changed files with 103 additions and 48 deletions

62
Cat.js
View File

@ -1,8 +1,9 @@
import { gameFrame } from './prototype.js'; import { gameFrame } from './prototype.js';
import { catAnimation, imageAssets } from './sketch.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 = { export const catAniDesc = {
chefCat: { chefCat: {
idle: { row: 0, frames: 4, frameSize: [200, 200], frameDelay: 10 }, idle: { row: 0, frames: 4, frameSize: [200, 200], frameDelay: 10 },
@ -33,9 +34,9 @@ export class Cat {
this.sprite.spriteSheet = spriteSheet; this.sprite.spriteSheet = spriteSheet;
this.sprite.addAnis(ani); this.sprite.addAnis(ani);
this.sprite.collider = 'static'; this.sprite.collider = 'static';
this.sprite.collides(mouseGroup);
this.sprite.overlaps(throwableGroup);
this.sprite.layer = 1; this.sprite.layer = 1;
console.log(this.spriteSheet);
console.log(this.sprite.animation);
this.sprite.changeAni('idle'); this.sprite.changeAni('idle');
this.active = false; this.active = false;
@ -137,16 +138,15 @@ export class SingleYarnCat extends Cat {
else this.switchToIdle(); else this.switchToIdle();
if (this.active && (millis() - this.lastShot > 3000)) { if (this.active && (millis() - this.lastShot > 3000)) {
const yarn = createSprite(this.x + gameFrame.tileWidth / 2, this.y, 20, 20); let yarnX = this.x + gameFrame.tileWidth / 2;
yarn.image = imageAssets.yarn; let yarnY = this.y;
yarn.scale = gameFrame.tileWidth / 1024;
yarn.vel.x = 1; const yarn = new Yarn(yarnX, yarnY);
yarn.life = 600; if (yarn) {
const newMovingObject = { throwables.push(yarn);
sprite: yarn, throwableGroup.add(yarn.sprite);
point: 15
} }
movingObjects.push(newMovingObject);
this.lastShot = millis(); this.lastShot = millis();
} }
} }
@ -165,18 +165,18 @@ export class DoubleYarnCat extends Cat {
else this.switchToIdle(); else this.switchToIdle();
if (this.active && (millis() - this.lastShot > 3000)) { if (this.active && (millis() - this.lastShot > 3000)) {
// TODO: check on the offset again
for (let offset of [0, 20]) { for (let offset of [0, 20]) {
const yarn = createSprite(this.x + gameFrame.tileWidth / 2 + offset, this.y, 20, 20); let yarnX = this.x + gameFrame.tileWidth / 2 + offset;
yarn.image = imageAssets.yarn; let yarnY = this.y;
yarn.scale = gameFrame.tileWidth / 1024;
yarn.vel.x = 1; const yarn = new Yarn(yarnX, yarnY);
yarn.life = 600; if (yarn) {
const newMovingObject = { throwables.push(yarn);
sprite: yarn, throwableGroup.add(yarn.sprite);
point: 15
} }
movingObjects.push(newMovingObject);
} }
this.lastShot = millis(); this.lastShot = millis();
} }
} }
@ -191,7 +191,6 @@ export class SleepyCat extends Cat {
} }
action(targetMouse) { action(targetMouse) {
console.log(`do i get here`);
if (this.awake) { if (this.awake) {
this.changeAni('action'); this.changeAni('action');
this.wakeStart = millis(); this.wakeStart = millis();
@ -221,16 +220,15 @@ export class IceCat extends Cat {
else this.switchToIdle(); else this.switchToIdle();
if (this.active && (millis() - this.lastShot > 3000)) { if (this.active && (millis() - this.lastShot > 3000)) {
const snowball = createSprite(this.x + gameFrame.tileWidth / 2, this.y, 20, 20); const snowballX = this.x + gameFrame.tileWidth / 2;
snowball.image = imageAssets.snowball; const snowballY = this.y;
snowball.scale = gameFrame.tileWidth / 1024;
snowball.vel.x = 1; const snowball = new Snowball(snowballX, snowballY)
snowball.life = 600; if (snowball) {
const newMovingObject = { throwables.push(snowball);
sprite: snowball, throwableGroup.add(snowball.sprite);
point: 20
} }
movingObjects.push(newMovingObject);
this.lastShot = millis(); this.lastShot = millis();
} }
} }

View File

@ -1,7 +1,7 @@
import { prototypeFrame, gameFrame } from './prototype.js'; import { prototypeFrame, gameFrame } from './prototype.js';
import { imageAssets, catImages, catAnimation, selectedCatType, resetCatType } from './sketch.js'; import { imageAssets, selectedCatType, resetCatType } from './sketch.js';
import { Cat, ChefCat, SingleYarnCat, DoubleYarnCat, SleepyCat, IceCat } from './Cat.js'; import { ChefCat, SingleYarnCat, DoubleYarnCat, SleepyCat, IceCat, throwables } from './Cat.js';
import { Mice, BasicMouse, HelmetMouse } from './Mouse.js'; import { BasicMouse, HelmetMouse } from './Mouse.js';
import { level1Mice } from './level/level1.js'; import { level1Mice } from './level/level1.js';
import { RobotVacuum } from './RobotVacuum.js'; import { RobotVacuum } from './RobotVacuum.js';
@ -18,6 +18,7 @@ export let cheeses = [];
export let grid = Array(5).fill().map(() => Array(9).fill(null)); export let grid = Array(5).fill().map(() => Array(9).fill(null));
let startTime; let startTime;
let levelMice = [...level1Mice]; let levelMice = [...level1Mice];
export let catGroup, mouseGroup, throwableGroup;
function createCat(type, x, y) { function createCat(type, x, y) {
switch (type) { switch (type) {
@ -38,12 +39,12 @@ function createCat(type, x, y) {
} }
} }
function createMouse(type, x, y) { function createMouse(type, x, y, row) {
switch (type) { switch (type) {
case 'basicMouse': case 'basicMouse':
return new BasicMouse(x, y); return new BasicMouse(x, y, row);
case 'helmetMouse': case 'helmetMouse':
return new HelmetMouse(x, y); return new HelmetMouse(x, y, row);
default: default:
return undefined; return undefined;
} }
@ -86,7 +87,7 @@ export function GameScene() {
let vacuum = new RobotVacuum(x, y, row); let vacuum = new RobotVacuum(x, y, row);
gameSprites.push(vacuum); gameSprites.push(vacuum.sprite);
robotVacuums.push(vacuum); robotVacuums.push(vacuum);
} }
@ -106,6 +107,10 @@ export function GameScene() {
}) })
gameFrame.catRatio = 1.2 * gameFrame.tileWidth/200; gameFrame.catRatio = 1.2 * gameFrame.tileWidth/200;
catGroup = new Group();
mouseGroup = new Group();
throwableGroup = new Group();
} }
this.draw = function() { this.draw = function() {
@ -142,6 +147,13 @@ export function GameScene() {
vacuum.action(); 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; newCat.sprite.scale = gameFrame.catRatio;
grid[row][col] = newCat; grid[row][col] = newCat;
activeCats.push(newCat); activeCats.push(newCat);
catGroup.add(newCat.sprite);
gameSprites.push(newCat.sprite); // Is this redundant? kedouble2 gameSprites.push(newCat.sprite); // Is this redundant? kedouble2
if (newCat instanceof SleepyCat) sleepyCats.push(newCat); if (newCat instanceof SleepyCat) sleepyCats.push(newCat);
resetCatType(); resetCatType();
@ -239,10 +252,11 @@ function spawnMouse(type, row) {
let x = width; let x = width;
let y = gameFrame.padding_up + row * gameFrame.tileHeight + gameFrame.tileHeight / 2; 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) { if (newMouse) {
newMouse.sprite.scale = gameFrame.catRatio; newMouse.sprite.scale = gameFrame.catRatio;
activeMice[row].push(newMouse); activeMice[row].push(newMouse);
mouseGroup.add(newMouse.sprite);
gameSprites.push(newMouse.sprite); // Is this redundant? kedouble2 sama allSprites gameSprites.push(newMouse.sprite); // Is this redundant? kedouble2 sama allSprites
} }
} }

View File

@ -1,12 +1,14 @@
import { gameFrame } from './prototype.js'; import { gameFrame } from './prototype.js';
import { imageAssets } from './sketch.js'; import { imageAssets } from './sketch.js';
import { activeMice } from './GameScene.js';
export class Mice { export class Mouse {
constructor(x, y, speed, HP, AP, img, width) { constructor(x, y, row, speed, HP, AP, img, width) {
this.sprite = createSprite(x, y, width, width); this.sprite = createSprite(x, y, width, width);
this.sprite.image = img; this.sprite.image = img;
this.sprite.layer = 3; this.sprite.layer = 3;
this.sprite.velocity.x = speed; this.sprite.velocity.x = speed;
this.row = row;
this.HP = HP; this.HP = HP;
this.AP = AP; this.AP = AP;
this.width = width; this.width = width;
@ -14,17 +16,27 @@ export class Mice {
remove() { remove() {
this.sprite.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 { export class BasicMouse extends Mouse {
constructor(x, y) { constructor(x, y, row) {
super(x, y, -0.25, 100, 20, imageAssets.mouse, gameFrame.tileWidth); super(x, y, row, -0.25, 100, 20, imageAssets.mouse, gameFrame.tileWidth);
} }
} }
export class HelmetMouse extends Mice { export class HelmetMouse extends Mouse {
constructor(x, y) { constructor(x, y, row) {
super(x, y, -0.25, 150, 20, imageAssets.mouse, gameFrame.tileWidth); super(x, y, row, -0.25, 150, 20, imageAssets.mouse, gameFrame.tileWidth);
} }
} }

31
Throwable.js Normal file
View File

@ -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);
}
}