154 lines
4.9 KiB
JavaScript
154 lines
4.9 KiB
JavaScript
import { TOWERS_CONFIG } from './towerConfig.js';
|
|
|
|
export class TowerManager {
|
|
|
|
constructor(scene) {
|
|
this.scene = scene;
|
|
this.lastFired = {}; // Track last fire time for each tower
|
|
}
|
|
|
|
createTower(type, x, y) {
|
|
const posX = this.scene.gridToLocation(x);
|
|
const posY = this.scene.gridToLocation(y);
|
|
|
|
const tower = this.scene.add.image(posX, posY, 'josh');
|
|
tower.props = {
|
|
'type': type,
|
|
'level': 1
|
|
}
|
|
this.scene.towers.add(tower);
|
|
|
|
// Draw range circle
|
|
const config = TOWERS_CONFIG[type].level1;
|
|
if (config) {
|
|
const range = config.range;
|
|
const circle = this.scene.add.circle(posX, posY, range, 0x00ff00, 0.2);
|
|
}
|
|
}
|
|
|
|
update(time, delta) {
|
|
// Iterate through all towers
|
|
this.scene.towers.children.iterate((tower) => {
|
|
const towerX = tower.x;
|
|
const towerY = tower.y;
|
|
|
|
// Get tower config
|
|
const type = tower.props.type;
|
|
const level = 'level'+tower.props.level;
|
|
const config = TOWERS_CONFIG[type][level];
|
|
|
|
if (!config) return;
|
|
|
|
const range = config.range;
|
|
const rate = config.rate;
|
|
|
|
// Check if enough time has passed since last fire
|
|
if (this.lastFired[tower.id] === undefined ||
|
|
time - this.lastFired[tower.id] >= rate) {
|
|
|
|
// Check for enemies in range
|
|
let inRange = false;
|
|
this.scene.enemies.children.iterate((enemy) => {
|
|
const distanceTraveled = enemy.props.distanceTraveled;
|
|
const distance = Phaser.Math.Distance.Between(
|
|
towerX, towerY,
|
|
enemy.x, enemy.y
|
|
);
|
|
|
|
if (distance <= range) {
|
|
inRange = true;
|
|
this.attackTarget(tower, enemy);
|
|
return false; // Stop iterating once we find one enemy
|
|
}
|
|
});
|
|
|
|
// Fire if enemies are in range
|
|
if (inRange) {
|
|
console.log('fire');
|
|
this.lastFired[tower.id] = time;
|
|
}
|
|
}
|
|
});
|
|
}
|
|
|
|
attackTarget(tower, enemy) {
|
|
// Tower Properties
|
|
const type = tower.props.type;
|
|
const level = 'level'+tower.props.level;
|
|
const config = TOWERS_CONFIG[type][level];
|
|
|
|
if (!config) return;
|
|
|
|
const dmgLow = config.dmgLow;
|
|
const dmgHigh = config.dmgHigh;
|
|
|
|
// Enemy Information
|
|
const fullHealth = enemy.props.fullHealth;
|
|
const currentHealth = enemy.props.health;
|
|
|
|
// Calculate damage (random between low and high)
|
|
const damage = Phaser.Math.Between(dmgLow, dmgHigh);
|
|
|
|
// Apply damage to enemy
|
|
enemy.props.health -= damage;
|
|
|
|
// Create or update health bar
|
|
if (!enemy.healthBar) {
|
|
this.createHealthBar(enemy);
|
|
}
|
|
|
|
// Update health bar display
|
|
this.updateHealthBar(enemy);
|
|
|
|
// Check if enemy should be destroyed
|
|
if (enemy.props.health <= 0) {
|
|
this.destroyEnemy(enemy);
|
|
}
|
|
}
|
|
|
|
createHealthBar(enemy) {
|
|
const barWidth = 30;
|
|
const barHeight = 5;
|
|
const barX = enemy.x - barWidth/2;
|
|
const barY = enemy.y - enemy.displayHeight/2 - 10;
|
|
|
|
// Create health bar container
|
|
enemy.healthBar = this.scene.add.container(enemy.x, enemy.y - enemy.displayHeight/2 - 10);
|
|
|
|
// Background bar (gray)
|
|
const background = this.scene.add.rectangle(0, 0, barWidth, barHeight, 0x808080);
|
|
|
|
// Health fill (green)
|
|
const healthFill = this.scene.add.rectangle(0, 0, barWidth, barHeight, 0x00ff00);
|
|
|
|
// Position the fill relative to background
|
|
healthFill.x = -barWidth/2 + (barWidth * (enemy.props.health / enemy.props.fullHealth)) / 2;
|
|
|
|
enemy.healthBar.add([background, healthFill]);
|
|
}
|
|
|
|
updateHealthBar(enemy) {
|
|
if (!enemy.healthBar) return;
|
|
|
|
const barWidth = 30;
|
|
const healthPercentage = enemy.props.health / enemy.props.fullHealth;
|
|
|
|
// Update the fill width based on current health
|
|
enemy.healthBar.list[1].width = barWidth * healthPercentage;
|
|
|
|
// Position the container correctly
|
|
enemy.healthBar.x = enemy.x;
|
|
enemy.healthBar.y = enemy.y - enemy.displayHeight/2 - 10;
|
|
}
|
|
|
|
destroyEnemy(enemy) {
|
|
// Remove health bar if exists
|
|
if (enemy.healthBar) {
|
|
enemy.healthBar.destroy();
|
|
}
|
|
|
|
// Destroy the enemy sprite
|
|
enemy.destroy();
|
|
}
|
|
|
|
} |