[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

@@ -78,11 +78,11 @@ class SoundManager implements GameListener {
switch event { switch event {
case START(state): case START(state):
play('start'); play('start');
case SPAWN(BULLET(_)): case SPAWN(_, BULLET(_)):
if (false /* ToDo: human tank */) { if (false /* ToDo: human tank */) {
play('shot'); play('shot');
} }
case SPAWN(BONUS(_, _)): case SPAWN(_, BONUS(_, _)):
play('bonus_add'); play('bonus_add');
case HIT(TANK(tank, who, wherewith)): case HIT(TANK(tank, who, wherewith)):
play('bullet_hit'); play('bullet_hit');

View File

@@ -22,7 +22,11 @@ import ru.m.tankz.game.record.GameRecord;
public function iterator():Iterator<GameRecord> { public function iterator():Iterator<GameRecord> {
var data:DynamicAccess<String> = so.data; var data:DynamicAccess<String> = so.data;
for (id in data.keys()) { for (id in data.keys()) {
@yield return read(id); try {
@yield return read(id);
} catch (error:Dynamic) {
}
} }
} }
} }

View File

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

View File

@@ -11,10 +11,10 @@ class Bullet extends MobileEntity {
public var tank(default, default):Tank; public var tank(default, default):Tank;
public var config(default, null):BulletConfig; 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.playerId = playerId;
this.config = config; 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; this.layer = 2;
} }

View File

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

View File

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

View File

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

View File

@@ -15,8 +15,8 @@ class Tank extends MobileEntity {
public var protect(default, null):Modificator; public var protect(default, null):Modificator;
public var freezing(default, null):Modificator; public var freezing(default, null):Modificator;
public function new(playerId:PlayerId, config:TankConfig) { public function new(id:Int, playerId:PlayerId, config:TankConfig) {
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.protect = new Modificator(); this.protect = new Modificator();
this.freezing = new Modificator(); this.freezing = new Modificator();
this.playerId = playerId; this.playerId = playerId;

View File

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

View File

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

View File

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

View File

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

View File

@@ -21,8 +21,8 @@ class SpawnPointEntity extends Entity {
public var point(default, null):SpawnPoint; public var point(default, null):SpawnPoint;
public function new(point:SpawnPoint, rect:Rectangle) { public function new(id:Int, point:SpawnPoint, rect:Rectangle) {
super(rect); super(id, rect);
this.point = point; this.point = point;
} }
} }