collapse Effect into Ability

This commit is contained in:
2025-01-12 13:58:35 +09:00
parent d9849f770b
commit d9d62d7070
4 changed files with 124 additions and 101 deletions
+98 -33
View File
@@ -1,48 +1,113 @@
import Effect from './effect.js'
import Projectile from './projectile.js'
// Q: damage -> self sustain / crowd control
// W: control -> mobility
// E: support -> vision / selfless support / creative
export default class Ability {
id = crypto.randomUUID()
name = 'Ability'
cooldown = 0
castTime = 0
cooldown = 0
damage = 0
radius = 1
range = 0
speed = 1000
#effect = () => {}
get effect() { return this.#effect }
set effect(value) { this.#effect = value }
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 }
constructor(options = {}) {
Object.entries(options).forEach(([key, value]) => this[key] = value)
}
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),
}),
})
}
static straightShot = new Ability({
id: 'straight_shot',
name: 'Straight Shot',
castTime: 0.1,
cooldown: 7,
damage: 10,
radius: 7,
range: 800,
speed: 3000,
effect: function straightShotEffect(caster, cursor) {
const ability = this
const straightShotCollision = function straightShotCollision(projectile, collidingEntity) {
collidingEntity.damage(ability.damage)
projectile.despawn()
}
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)
}),
})
}
const projectile = new Projectile({
onCollide: straightShotCollision,
owner: caster,
position: caster.position.clone(),
radius: ability.radius,
speed: ability.speed,
})
projectile.destination = caster.position.clone().add(cursor.clone().sub(caster.position).normalize().multiplyScalar(ability.range))
caster.game?.spawnProjectile(projectile)
caster.cooldown(ability.id)
},
})
static basicAttack = new Ability({
id: 'basic_attack',
name: 'Basic Attack',
castTime: 0.25,
cooldown: 1.25,
damage: 5,
radius: 5,
range: 600,
speed: 600,
effect: function basicAttackEffect(caster, cursor) {
const ability = this
let closest = null
let distance = Infinity
caster.game?.entities.filter((e) => e.id != caster.id && e.position.clone().sub(caster.position).length() < ability.range).forEach((e) => {
const newDistance = e.position.clone().sub(cursor).length() < distance
if (newDistance < distance) {
closest = e
distance = newDistance
}
})
if (closest == null) { return }
const basicAttackAfter = function basicAttackAfter() {
closest.damage(ability.damage)
}
const projectile = new Projectile({
after: basicAttackAfter,
homingTarget: closest,
owner: caster,
position: caster.position.clone(),
radius: ability.radius,
speed: ability.speed,
})
caster.game?.spawnProjectile(projectile)
caster.cooldown(ability.id)
},
})
static control = new Ability({
id: 'control',
name: 'Control',
castTime: 1,
cooldown: 5,
effect: function controlEffect() { console.log('Control is still work in progress.') },
})
static shieldThrow = new Ability({
id: 'shield_throw',
name: 'Shield Throw',
castTime: 0.1,
cooldown: 7,
effect: function shieldThrowEffect() { console.log('Shield throw is still work in progress.') },
})
}