add cast times and cooldowns

This commit is contained in:
2025-01-12 03:30:52 +09:00
parent 2eb914a680
commit e0dd7dcaf3
7 changed files with 142 additions and 47 deletions
+37 -39
View File
@@ -1,46 +1,44 @@
import { Vector2 } from 'three'
import Projectile from './projectile.js'
import Effect from './effect.js'
export default class Ability {
static skillshot({ range, radius, speed, onCollide, after }) {
return function(x, y) {
const projectile = new Projectile()
const destination = this.position.clone().add(new Vector2(x, y).sub(this.position).normalize().multiplyScalar(range))
projectile.owner = this.id
projectile.position.copy(this.position)
projectile.destination = destination
projectile.radius = radius
projectile.speed = speed
projectile.after = after
projectile.onCollide = onCollide
this.game?.spawnProjectile(projectile)
}
id = crypto.randomUUID()
name = 'Ability'
cooldown = 0
effect = () => {}
castTime = 0
constructor(options) {
if (options.name != null) { this.name = options.name }
if (options.effect != null) { this.effect = options.effect }
if (options.cooldown != null) { this.cooldown = options.cooldown }
if (options.castTime != null) { this.castTime = options.castTime }
}
static homingProjectile({ range, radius, speed, onCollide, after }) {
return function(x, y) {
const cursor = new Vector2(x, y)
let closest = null
let distance = Infinity
this.game?.entities.filter((e) => e.id != this.id && e.position.clone().sub(this.position).length() < range).forEach((e) => {
const newDistance = e.position.clone().sub(cursor).length() < distance
if (newDistance < distance) {
closest = e
distance = newDistance
}
})
static basicAttack(range, radius, speed) {
return new this({
name: 'Basic Attack',
castTime: 0.25,
cooldown: 1.25,
effect: Effect.homingProjectile({
range,
radius,
speed,
after: Effect.damage({ despawn: true }).bind(this),
}),
})
}
if (closest == null) { return } // TODO: refund
const projectile = new Projectile()
projectile.owner = this.id
projectile.position.copy(this.position)
projectile.homingTarget = closest
projectile.radius = radius
projectile.speed = speed
projectile.after = after
projectile.onCollide = onCollide
this.game?.spawnProjectile(projectile)
}
static straightShot(range, radius, speed) {
return new this({
name: 'Straight Shot',
castTime: 0.1,
cooldown: 7,
effect: Effect.skillshot({
range,
radius,
speed,
onCollide: Effect.damage({ despawn: true }).bind(this)
}),
})
}
}