1
0
Fork 0

Spin automation

This commit is contained in:
Pabloader 2025-06-27 13:55:55 +00:00
parent 1e4be0ff2c
commit d2df62cdbe
4 changed files with 28 additions and 5 deletions

View File

@ -14,12 +14,14 @@ const TILE_SIZE = (MAP_PIXEL_SIZE - MAP_PADDING * 2) / MAP_SIZE;
const SIDEBAR_SIZE = clamp((window.innerWidth - window.innerHeight) / 2, 300, 600); const SIDEBAR_SIZE = clamp((window.innerWidth - window.innerHeight) / 2, 300, 600);
const canvas = createCanvas(MAP_PIXEL_SIZE + SIDEBAR_SIZE * 2, MAP_PIXEL_SIZE); const canvas = createCanvas(MAP_PIXEL_SIZE + SIDEBAR_SIZE * 2, MAP_PIXEL_SIZE);
const spinner = new Spinner([MAP_PIXEL_SIZE + SIDEBAR_SIZE, 0], [SIDEBAR_SIZE, SIDEBAR_SIZE]);
const map = new TileMap( const map = new TileMap(
[MAP_PADDING + SIDEBAR_SIZE, MAP_PADDING], [MAP_PADDING + SIDEBAR_SIZE, MAP_PADDING],
MAP_SIZE, MAP_SIZE,
TILE_SIZE, TILE_SIZE,
spinner,
); );
const spinner = new Spinner([MAP_PIXEL_SIZE + SIDEBAR_SIZE, 0], [SIDEBAR_SIZE, SIDEBAR_SIZE]);
const inventory = new Inventory( const inventory = new Inventory(
map, map,
[0, 0], [0, 0],

View File

@ -97,10 +97,10 @@ export default class Player extends Character {
return true; return true;
case SpinnerAction.MELEE: case SpinnerAction.MELEE:
return this.meleeWeapon != null && !this.tile.enemy?.isBoss; return !this.tile.enemy?.isBoss && this.meleeWeapon != null;
case SpinnerAction.SHOOT: case SpinnerAction.SHOOT:
return this.removeItem(this.gun) && !this.tile.enemy?.isBoss; return !this.tile.enemy?.isBoss && this.removeItem(this.gun);
} }
} }
} }

View File

@ -92,12 +92,20 @@ export default class Spinner extends Entity {
} }
public override onClick() { public override onClick() {
this.spin();
}
public spin() {
if (!this.fired) return; if (!this.fired) return;
this.fired = false; this.fired = false;
this.nextAngle = this.angle + (randInt(5, 10) + Math.random()) * 2 * Math.PI; this.nextAngle = this.angle + (randInt(5, 10) + Math.random()) * 2 * Math.PI;
} }
public stop() {
this.fired = true;
}
public addListener(listener: SpinnerListener) { public addListener(listener: SpinnerListener) {
this.listeners.add(listener); this.listeners.add(listener);
} }

View File

@ -4,7 +4,7 @@ import Tile, { TileType } from "./tile";
import Pathfinding from "./pathfinding"; import Pathfinding from "./pathfinding";
import Item, { ItemType } from "./item"; import Item, { ItemType } from "./item";
import Player, { Players } from "./player"; import Player, { Players } from "./player";
import { SpinnerAction } from "./spinner"; import Spinner, { SpinnerAction } from "./spinner";
enum GameState { enum GameState {
NORMAL, NORMAL,
@ -20,7 +20,13 @@ export default class TileMap extends Entity {
private state = GameState.NORMAL; private state = GameState.NORMAL;
private availableTiles: Tile[] = []; private availableTiles: Tile[] = [];
constructor(position: [number, number], private mapSize: number, private tileSize: number, numPlayers: number = 2) { constructor(
position: [number, number],
private mapSize: number,
private tileSize: number,
private spinner: Spinner,
numPlayers: number = 2,
) {
super(position, [mapSize * tileSize, mapSize * tileSize]); super(position, [mapSize * tileSize, mapSize * tileSize]);
this.players = shuffle(Object.values(Players)).slice(0, numPlayers); this.players = shuffle(Object.values(Players)).slice(0, numPlayers);
this.startTile = this.createMap(); this.startTile = this.createMap();
@ -218,6 +224,7 @@ export default class TileMap extends Entity {
if (tile.isPointInBounds(x - this.left, y - this.top)) { if (tile.isPointInBounds(x - this.left, y - this.top)) {
const path = Pathfinding.findPath(this.player.tile, tile); const path = Pathfinding.findPath(this.player.tile, tile);
if (path.length > 1) { if (path.length > 1) {
this.spinner.stop();
this.player.lastDoor = path.find(t => t.type === TileType.DOOR); this.player.lastDoor = path.find(t => t.type === TileType.DOOR);
this.player.moveTo(tile, path); this.player.moveTo(tile, path);
tile.open(); tile.open();
@ -265,6 +272,8 @@ export default class TileMap extends Entity {
this.players.splice(this.currentPlayerIdx, 1); this.players.splice(this.currentPlayerIdx, 1);
this.currentPlayerIdx = this.currentPlayerIdx % this.players.length; this.currentPlayerIdx = this.currentPlayerIdx % this.players.length;
this.setNormalState(); this.setNormalState();
} else if (!this.player.grenade) {
this.spinner.spin();
} }
break; break;
case SpinnerAction.MELEE: case SpinnerAction.MELEE:
@ -273,8 +282,11 @@ export default class TileMap extends Entity {
break; break;
case SpinnerAction.RUN: case SpinnerAction.RUN:
this.setNormalState(); this.setNormalState();
this.spinner.spin();
break; break;
} }
} else if (!this.player.grenade) {
this.spinner.spin();
} }
break; break;
} }
@ -311,6 +323,7 @@ export default class TileMap extends Entity {
} }
private nextPlayer() { private nextPlayer() {
this.spinner.stop();
this.currentPlayerIdx = (this.currentPlayerIdx + 1) % this.players.length; this.currentPlayerIdx = (this.currentPlayerIdx + 1) % this.players.length;
} }