XGravity/Public/ecma/Models/AsteroidThrowerEnemyModel.ecma.js

112 lines
3.2 KiB
JavaScript

"use strict";
const AsteroidThrowerEnemyModel = (function(){
const AsteroidThrowerEnemyModel = function(xgravity, config = {}){
const self = this;
const resolve_image = (img) => {
if(Array.isArray(img) && img.length > 0){
return img[Math.floor(Math.random() * img.length)];
}
return typeof img === "string" ? img : null;
};
const image_src = resolve_image(config.image);
const base_size = config.size || 2.0;
const points = config.points || 250;
const mass = config.mass || 2.2;
let health = config.health || 45;
const max_health = health;
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 spawn_strategy = SpawnStrategies.get(config.spawn_mode || "perimeter");
const behavior_strategy = BehaviorStrategies.get(config.behavior || "thrower_ai");
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 ? "#eab308" : "#fef08a"
});
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, all_enemies) => {
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, all_enemies);
}
};
const constructor = () => {
spawn_strategy.init(entity, config, xgravity);
};
constructor();
};
return AsteroidThrowerEnemyModel;
})();