XGravity/Public/ecma/Models/StaticMineEnemyModel.ecma.js

105 lines
2.9 KiB
JavaScript

"use strict";
const StaticMineEnemyModel = (function(){
const StaticMineEnemyModel = function(xgravity, config = {}){
const self = this;
const entity = {
x : 0,
y : 0,
vx : 0,
vy : 0,
angle : Math.random() * Math.PI * 2,
scale : 0.0,
alpha : 0.3,
ready : false,
alive : true,
age : 0,
config : config
};
const base_size = config.size || 2.0;
const points = config.points || 50;
const mass = config.mass || 5.0;
let health = config.health || 40;
const max_health = health;
const image_src = config.image || null;
const spawn_strategy = SpawnStrategies.get(config.spawn_mode || "deep_space");
const behavior_strategy = BehaviorStrategies.get(config.behavior || "static");
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 ? "#ef4444" : "#f59e0b"
});
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){
spawn_strategy.update(entity, delta_time);
} else {
behavior_strategy(entity, delta_time, drone_info);
}
};
const constructor = () => {
spawn_strategy.init(entity, config, xgravity);
};
constructor();
};
return StaticMineEnemyModel;
})();