[common] enity id

This commit is contained in:
2019-05-08 16:40:58 +03:00
parent 183c6c16f1
commit b0ae85334b
13 changed files with 46 additions and 42 deletions

View File

@@ -8,8 +8,8 @@ class Bonus extends Entity {
public var config(default, null):BonusConfig;
public function new(config:BonusConfig) {
super(new Rectangle(0, 0, 44, 44));
public function new(id:Int, config:BonusConfig) {
super(id, new Rectangle(0, 0, 44, 44));
this.config = config;
}
}

View File

@@ -11,10 +11,10 @@ class Bullet extends MobileEntity {
public var tank(default, default):Tank;
public var config(default, null):BulletConfig;
public function new(playerId:PlayerId, config:BulletConfig) {
public function new(id:Int, playerId:PlayerId, config:BulletConfig) {
this.playerId = playerId;
this.config = config;
super(new Rectangle(0, 0, config.width, config.height), config.speed, Direction.RIGHT);
super(id, new Rectangle(0, 0, config.width, config.height), config.speed, Direction.RIGHT);
this.layer = 2;
}

View File

@@ -15,8 +15,8 @@ class Eagle extends Entity {
public var score(get, null):Int;
public function new(team:TeamId, config:EagleConfig) {
super(new Rectangle(0, 0, 44, 44)); // ToDo: hardcode size
public function new(id:Int, team:TeamId, config:EagleConfig) {
super(id, new Rectangle(0, 0, 44, 44)); // ToDo: hardcode size
this.team = team;
this.config = config;
this.death = false;

View File

@@ -5,15 +5,14 @@ import Type;
class Entity implements IKey {
private static var idCounter:Int = 0;
public var id(default, null):Int;
public var type(default, null):String;
public var key(get, null):String;
public var rect(default, null):Rectangle;
public function new(rect:Rectangle) {
this.id = ++idCounter;
public function new(id:Int, rect:Rectangle) {
this.id = id;
this.type = Type.getClassName(Type.getClass(this)).split('.').pop().toLowerCase();
this.rect = rect;
}

View File

@@ -11,8 +11,8 @@ class MobileEntity extends Entity {
public var layer(default, null):Int;
public var speed(default, null):Float;
public function new(rect:Rectangle, speed:Float, direction:Direction) {
super(rect);
public function new(id:Int, rect:Rectangle, speed:Float, direction:Direction) {
super(id, rect);
this.speed = speed;
this.layer = 0;
this.mx = 0;

View File

@@ -15,8 +15,8 @@ class Tank extends MobileEntity {
public var protect(default, null):Modificator;
public var freezing(default, null):Modificator;
public function new(playerId:PlayerId, config:TankConfig) {
super(new Rectangle(0, 0, config.width, config.height), config.speed, Direction.RIGHT);
public function new(id:Int, playerId:PlayerId, config:TankConfig) {
super(id, new Rectangle(0, 0, config.width, config.height), config.speed, Direction.RIGHT);
this.protect = new Modificator();
this.freezing = new Modificator();
this.playerId = playerId;

View File

@@ -15,31 +15,31 @@ class EntityBuilder {
this.config = config;
}
public function buildEagle(teamId:TeamId):Eagle {
public function buildEagle(id:Int, teamId:TeamId):Eagle {
var eageleConfig = config.getTeam(teamId).eagle;
var eaglePoint = config.getPoint(teamId, "eagle");
var eagle = new Eagle(teamId, eageleConfig);
var eagle = new Eagle(id, teamId, eageleConfig);
eagle.color = config.getColor(new PlayerId(teamId, -1));
return eagle;
}
public function buildTank(playerId:PlayerId, type:TankType):Tank {
public function buildTank(id:Int, playerId:PlayerId, type:TankType):Tank {
var playerConfig = config.getPlayer(playerId);
var tankConfig = config.getTank(type);
var tank = new Tank(playerId, tankConfig);
var tank = new Tank(id, playerId, tankConfig);
tank.color = config.getColor(playerId);
return tank;
}
public function buildBullet(playerId:PlayerId, type:TankType):Bullet {
public function buildBullet(id:Int, playerId:PlayerId, type:TankType):Bullet {
var tankConfig = config.getTank(type);
var bullet = new Bullet(playerId, tankConfig.bullet);
var bullet = new Bullet(id, playerId, tankConfig.bullet);
return bullet;
}
public function buildBonus(type:BonusType):Bonus {
public function buildBonus(id:Int, type:BonusType):Bonus {
var bonusConfig = config.getBonus(type);
var bonus = new Bonus(bonusConfig);
var bonus = new Bonus(id, bonusConfig);
return bonus;
}
}

View File

@@ -70,14 +70,14 @@ import ru.m.tankz.Type;
case GameEvent.COMPLETE(state, team):
this.state = state;
this.winner = team.id;
case GameEvent.SPAWN(EAGLE(teamId, point)):
var eagle = builder.buildEagle(teamId);
case GameEvent.SPAWN(entityId, EAGLE(teamId, point)):
var eagle = builder.buildEagle(entityId, teamId);
applyPoint(eagle, point);
var team = getTeam(teamId);
team.eagleId = eagle.id;
engine.spawn(eagle);
case GameEvent.SPAWN(TANK(playerId, type, point)):
var tank = builder.buildTank(playerId, type);
case GameEvent.SPAWN(entityId, TANK(playerId, type, point)):
var tank = builder.buildTank(entityId, playerId, type);
applyPoint(tank, point);
var player = getPlayer(playerId);
player.tankId = tank.id;
@@ -89,17 +89,17 @@ import ru.m.tankz.Type;
tank.bonus = Math.random() < player.config.bonus;
//
engine.spawn(tank);
case GameEvent.SPAWN(BULLET(playerId)):
case GameEvent.SPAWN(entityId, BULLET(playerId)):
var player = getPlayer(playerId);
var tank:Tank = cast engine.entities.get(player.tankId);
var bullet = builder.buildBullet(playerId, tank.config.type);
var bullet = builder.buildBullet(entityId, playerId, tank.config.type);
bullet.tank = tank;
var rect = tank.rect;
bullet.rect.center = rect.center.add(new Point(rect.width / 4 * rect.direction.x, rect.height / 4 * rect.direction.y));
bullet.move(rect.direction);
engine.spawn(bullet);
case GameEvent.SPAWN(BONUS(type, point)):
var bonus = builder.buildBonus(type);
case GameEvent.SPAWN(entityId, BONUS(type, point)):
var bonus = builder.buildBonus(entityId, type);
bonus.rect.x = point.x * config.map.cellWidth;
bonus.rect.y = point.y * config.map.cellHeight;
engine.spawn(bonus);

View File

@@ -38,7 +38,7 @@ enum ChangeEvent {
enum GameEvent {
START(state:GameState);
SPAWN(event:SpawnEvent);
SPAWN(entityId:Int, event:SpawnEvent);
HIT(event:HitEvent);
DESTROY(event:DestroyEvent);
CHANGE(event:ChangeEvent);

View File

@@ -23,13 +23,14 @@ class GameRunner implements EngineListener implements GameListener {
private var game(default, null):IGame;
private var gameEventSignal(get, null):Signal<GameEvent>;
private var entityId:Int;
private var timer:Timer;
public function new(game:IGame) {
this.game = game;
this.game.connect(this);
this.game.engine.connect(this);
this.entityId = 0;
}
private inline function get_gameEventSignal():Signal<GameEvent> {
@@ -67,7 +68,7 @@ class GameRunner implements EngineListener implements GameListener {
}
if (team.config.eagle != null) {
var point = game.config.getPoint(team.id, "eagle");
gameEventSignal.emit(GameEvent.SPAWN(EAGLE(team.id, point)));
gameEventSignal.emit(GameEvent.SPAWN(++entityId, EAGLE(team.id, point)));
}
}
gameEventSignal.emit(GameEvent.START(state));
@@ -87,7 +88,7 @@ class GameRunner implements EngineListener implements GameListener {
}
private function spawn(task:SpawnTask):Void {
gameEventSignal.emit(GameEvent.SPAWN(TANK(task.playerId, task.tankType, task.point)));
gameEventSignal.emit(GameEvent.SPAWN(++entityId, TANK(task.playerId, task.tankType, task.point)));
}
private function checkComplete():Void {
@@ -190,7 +191,7 @@ class GameRunner implements EngineListener implements GameListener {
x: Math.floor(Math.random() * (game.engine.map.gridWidth - 1)),
y: Math.floor(Math.random() * (game.engine.map.gridHeight - 1)),
}
gameEventSignal.emit(GameEvent.SPAWN(BONUS(type, point)));
gameEventSignal.emit(GameEvent.SPAWN(++entityId, BONUS(type, point)));
}
private inline function alienTank(team:TeamId):Tank->Bool {
@@ -270,11 +271,11 @@ class GameRunner implements EngineListener implements GameListener {
var tank:Tank = cast game.engine.entities.get(tankId);
var player = game.getPlayer(tank.playerId);
if (!tank.freezing.active && player.bullets < tank.config.bullets) {
gameEventSignal.emit(GameEvent.SPAWN(BULLET(tank.playerId)));
gameEventSignal.emit(GameEvent.SPAWN(++entityId, BULLET(tank.playerId)));
}
case GameEvent.SPAWN(TANK(playerId, _)):
case GameEvent.SPAWN(entityId, TANK(playerId, _)):
game.getPlayer(playerId).control.start();
case GameEvent.SPAWN(BULLET(playerId)):
case GameEvent.SPAWN(entityId, BULLET(playerId)):
game.getPlayer(playerId).bullets++;
case GameEvent.DESTROY(EAGLE(eagle, who, wherewith, score)):
eagle.death = true;