105 lines
2.9 KiB
JavaScript
105 lines
2.9 KiB
JavaScript
"use strict";
|
|
|
|
const RusherEnemyModel = (function(){
|
|
|
|
const RusherEnemyModel = function(xgravity, config = {}){
|
|
|
|
const self = this;
|
|
|
|
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.5;
|
|
const points = config.points || 200;
|
|
const mass = config.mass || 4.0;
|
|
let health = config.health || 20;
|
|
const max_health = health;
|
|
const image_src = config.image || null;
|
|
|
|
const spawn_strategy = SpawnStrategies.get(config.spawn_mode || "perimeter");
|
|
const behavior_strategy = BehaviorStrategies.get(config.behavior || "cross_screen");
|
|
|
|
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 ? "#e11d48" : "#fb7185"
|
|
});
|
|
|
|
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 RusherEnemyModel;
|
|
})(); |