XGravity/Public/ecma/Models/ChaserEnemyModel.ecma.js

111 lines
3.3 KiB
JavaScript

"use strict";
const ChaserEnemyModel = (function(){
const ChaserEnemyModel = function(xgravity, config = {}){
const self = this;
// Objeto de contexto mutable que manipulan las estrategias
const entity = {
x : 0,
y : 0,
vx : 0,
vy : 0,
angle : 0,
scale : 1.0,
alpha : 1.0,
ready : false,
alive : true,
age : 0,
config : config
};
const base_size = config.size || 1.8;
const points = config.points || 150;
const mass = config.mass || 1.8;
let health = config.health || 30;
const max_health = health;
const image_src = config.image || null;
// Enlace con las estrategias según config
const spawn_strategy = SpawnStrategies.get(config.spawn_mode || "overflight");
const behavior_strategy = BehaviorStrategies.get(config.behavior || "track_drone");
// Accesores de estado para EnemiesManager y físicas
this.is_ready = () => entity.ready;
this.is_alive = () => entity.alive;
this.get_points = () => points;
this.get_age = () => entity.age;
this.get_mass = () => mass;
this.get_health = () => health;
this.get_max_health = () => max_health;
this.get_velocity = () => ({ vx: entity.vx, vy: entity.vy });
this.get_state = () => ({
x : entity.x,
y : entity.y,
angle : entity.angle,
scale : entity.scale,
alpha : entity.alpha,
size : base_size,
image_src : image_src,
ready : entity.ready,
alive : entity.alive,
health : health,
max_health : max_health,
radius : (base_size * entity.scale) * 0.45,
fallback_color : entity.ready ? "#a855f7" : "#c084fc"
});
this.get_bounds = () => ({
x : entity.x,
y : entity.y,
radius : (base_size * entity.scale) * 0.45
});
this.apply_impulse = (ivx, ivy) => {
entity.vx += ivx;
entity.vy += ivy;
};
this.set_position = (nx, ny) => {
entity.x = nx;
entity.y = ny;
};
this.take_damage = (amount) => {
health = Math.max(0, health - amount);
if(health <= 0){
entity.alive = false;
}
return entity.alive;
};
this.on_hit = () => {
entity.alive = false;
};
this.update = (delta_time, drone_info) => {
if(!entity.alive) return;
entity.age += delta_time;
if(!entity.ready){
// Ejecuta la cinemática de spawn hasta que marque ready = true
spawn_strategy.update(entity, delta_time);
} else {
// Ejecuta la IA de combate
behavior_strategy(entity, delta_time, drone_info);
}
};
const constructor = () => {
// Inicializa la posición y cinemática de entrada según la estrategia configurada
spawn_strategy.init(entity, config, xgravity);
};
constructor();
};
return ChaserEnemyModel;
})();