From 4c76d5dbde0822372e381a09f8b88ce72ab78d43 Mon Sep 17 00:00:00 2001 From: Thayol Date: Wed, 22 Jan 2025 23:21:39 +0900 Subject: [PATCH] restrict casting vision to nearby enemies --- src/entity.js | 34 ++++++++++++++++++++++------------ src/game.js | 2 +- src/level.js | 18 +++++++++++++----- 3 files changed, 36 insertions(+), 18 deletions(-) diff --git a/src/entity.js b/src/entity.js index fe2608c..48e7c51 100644 --- a/src/entity.js +++ b/src/entity.js @@ -201,12 +201,6 @@ export default class Entity { this.rotation = targetPosition.clone().sub(this.position).angle() } - if (ability.castTime == null) { - ability.effect(this, cursor) - - return true - } - const cooldown = this.game?.secToTick(ability.cooldown) ?? 0 const lastCast = this.cooldowns[ability.id] const timestamp = this.game?.currentTick ?? 0 @@ -214,6 +208,13 @@ export default class Entity { return false } + if (ability.castTime == null) { + this.#castingVision() + ability.effect(this, cursor) + + return true + } + this.casting = { ability: ability.id, cursor, timestamp } return true @@ -357,6 +358,7 @@ export default class Entity { const timestamp = this.game?.currentTick ?? 0 if (ability.castTime == null) { + this.#castingVision() ability.effect(this, cursor) } else { @@ -534,10 +536,7 @@ export default class Entity { update() { this.#calculateVision() this.#checkHealth() - if (this.dead) { - // TODO: do something while the entity is dead (and disallow casting, vision, etc) - } - else { + if (!this.dead) { this.#cast() this.#move() this.#tickBuffs() @@ -620,11 +619,22 @@ export default class Entity { this.casting = null } - // TODO: only spawn castingVision if slightly outside regular vision (or obstructed) - Ability.castingVision.effect(this, this.position) + this.#castingVision() return true } + #castingVision() { + const enemyTeam = this.team == Team.blue ? Team.red : (this.team == Team.red ? Team.blue : null) + if (enemyTeam == null) { + return // only blue/red teams have casting vision + } + + const enemiesNearby = (this.game?.entities ?? []).some((it) => !it.dead && it.team == enemyTeam && it.distanceTo(this.position) <= (it.visionRange + this.radius)) + if (enemiesNearby) { + Ability.castingVision.effect(this, this.position) + } + } + #checkHealth() { if (!this.dead && this.health <= 0) { this.dead = true diff --git a/src/game.js b/src/game.js index 5690c6f..393decd 100644 --- a/src/game.js +++ b/src/game.js @@ -142,7 +142,7 @@ export default class Game { } const callUpdate = function callUpdate(object) { object.update() } - this.entities.forEach(callUpdate) // TODO: entity with lower ID has unfair collision advantage (regular loop + until it fully loops around with an offset?) + this.entities.forEach(callUpdate) this.projectiles.forEach(callUpdate) if (this.#logic != null) { this.#logic() diff --git a/src/level.js b/src/level.js index 4556383..ffd259e 100644 --- a/src/level.js +++ b/src/level.js @@ -10,14 +10,22 @@ export class Dungeon { game.height = 1500 const team = Team.blue - const enemy = Team.neutral + const enemy = team == Team.blue ? Team.red : Team.blue - game.spawnEntity(new Entity(Template.player({ id: '1', spawnPosition: new Vector2(1500, 700), team }))) + const castQ = function castQ() { + const entity = this + if (game.currentTick != 0 && game.currentTick % (game.tickRate * 5) == 0) { + entity.castAction('q', new Vector2(2500, 1500)) + } + } + + game.addTerrain(new Terrain([new Vector2(2000, 1500), new Vector2(2500, 1000), new Vector2(2500, 1500)], false)) + game.spawnEntity(new Entity(Template.player({ id: '6', spawnPosition: new Vector2(2400, 1400), team: enemy, logic: castQ }))) + + game.spawnEntity(new Entity(Template.player({ id: '1', spawnPosition: new Vector2(1500, 700), team, dead: true }))) game.spawnEntity(new Entity(Template.player({ id: '2', spawnPosition: new Vector2(200, 1300), team, health: 10 }))) - game.spawnEntity(new Entity(Template.basilisk({ id: 'boss', spawnPosition: new Vector2(2200, 750), team: enemy }))) - - setTimeout(() => game.entities.find((it) => it.id == '1').damage(9999), 10) + game.spawnEntity(new Entity(Template.basilisk({ id: 'boss', spawnPosition: new Vector2(2200, 750), team: Team.neutral }))) game.start() }