From b0ae85334b6807efacbd201f848a932f229aa046 Mon Sep 17 00:00:00 2001 From: shmyga Date: Wed, 8 May 2019 16:40:58 +0300 Subject: [PATCH] [common] enity id --- src/client/haxe/ru/m/tankz/sound/SoundManager.hx | 4 ++-- .../haxe/ru/m/tankz/storage/RecordStorage.hx | 6 +++++- src/common/haxe/ru/m/tankz/core/Bonus.hx | 4 ++-- src/common/haxe/ru/m/tankz/core/Bullet.hx | 4 ++-- src/common/haxe/ru/m/tankz/core/Eagle.hx | 4 ++-- src/common/haxe/ru/m/tankz/core/Entity.hx | 5 ++--- src/common/haxe/ru/m/tankz/core/MobileEntity.hx | 4 ++-- src/common/haxe/ru/m/tankz/core/Tank.hx | 4 ++-- src/common/haxe/ru/m/tankz/game/EntityBuilder.hx | 16 ++++++++-------- src/common/haxe/ru/m/tankz/game/Game.hx | 16 ++++++++-------- src/common/haxe/ru/m/tankz/game/GameEvent.hx | 2 +- src/common/haxe/ru/m/tankz/game/GameRunner.hx | 15 ++++++++------- .../haxe/ru/m/tankz/editor/level/MapEditView.hx | 4 ++-- 13 files changed, 46 insertions(+), 42 deletions(-) diff --git a/src/client/haxe/ru/m/tankz/sound/SoundManager.hx b/src/client/haxe/ru/m/tankz/sound/SoundManager.hx index 1644bd6..1729fd0 100644 --- a/src/client/haxe/ru/m/tankz/sound/SoundManager.hx +++ b/src/client/haxe/ru/m/tankz/sound/SoundManager.hx @@ -78,11 +78,11 @@ class SoundManager implements GameListener { switch event { case START(state): play('start'); - case SPAWN(BULLET(_)): + case SPAWN(_, BULLET(_)): if (false /* ToDo: human tank */) { play('shot'); } - case SPAWN(BONUS(_, _)): + case SPAWN(_, BONUS(_, _)): play('bonus_add'); case HIT(TANK(tank, who, wherewith)): play('bullet_hit'); diff --git a/src/client/haxe/ru/m/tankz/storage/RecordStorage.hx b/src/client/haxe/ru/m/tankz/storage/RecordStorage.hx index e239208..a6f0622 100644 --- a/src/client/haxe/ru/m/tankz/storage/RecordStorage.hx +++ b/src/client/haxe/ru/m/tankz/storage/RecordStorage.hx @@ -22,7 +22,11 @@ import ru.m.tankz.game.record.GameRecord; public function iterator():Iterator { var data:DynamicAccess = so.data; for (id in data.keys()) { - @yield return read(id); + try { + @yield return read(id); + } catch (error:Dynamic) { + + } } } } diff --git a/src/common/haxe/ru/m/tankz/core/Bonus.hx b/src/common/haxe/ru/m/tankz/core/Bonus.hx index 85c5dd0..d5dbb22 100644 --- a/src/common/haxe/ru/m/tankz/core/Bonus.hx +++ b/src/common/haxe/ru/m/tankz/core/Bonus.hx @@ -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; } } diff --git a/src/common/haxe/ru/m/tankz/core/Bullet.hx b/src/common/haxe/ru/m/tankz/core/Bullet.hx index 7b3303a..a15e5db 100644 --- a/src/common/haxe/ru/m/tankz/core/Bullet.hx +++ b/src/common/haxe/ru/m/tankz/core/Bullet.hx @@ -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; } diff --git a/src/common/haxe/ru/m/tankz/core/Eagle.hx b/src/common/haxe/ru/m/tankz/core/Eagle.hx index 115a756..234b2d1 100644 --- a/src/common/haxe/ru/m/tankz/core/Eagle.hx +++ b/src/common/haxe/ru/m/tankz/core/Eagle.hx @@ -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; diff --git a/src/common/haxe/ru/m/tankz/core/Entity.hx b/src/common/haxe/ru/m/tankz/core/Entity.hx index 232ec26..8f8b88b 100755 --- a/src/common/haxe/ru/m/tankz/core/Entity.hx +++ b/src/common/haxe/ru/m/tankz/core/Entity.hx @@ -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; } diff --git a/src/common/haxe/ru/m/tankz/core/MobileEntity.hx b/src/common/haxe/ru/m/tankz/core/MobileEntity.hx index db470a2..df6480d 100755 --- a/src/common/haxe/ru/m/tankz/core/MobileEntity.hx +++ b/src/common/haxe/ru/m/tankz/core/MobileEntity.hx @@ -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; diff --git a/src/common/haxe/ru/m/tankz/core/Tank.hx b/src/common/haxe/ru/m/tankz/core/Tank.hx index 31b0532..2b0ba58 100755 --- a/src/common/haxe/ru/m/tankz/core/Tank.hx +++ b/src/common/haxe/ru/m/tankz/core/Tank.hx @@ -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; diff --git a/src/common/haxe/ru/m/tankz/game/EntityBuilder.hx b/src/common/haxe/ru/m/tankz/game/EntityBuilder.hx index df7e998..a6af52c 100644 --- a/src/common/haxe/ru/m/tankz/game/EntityBuilder.hx +++ b/src/common/haxe/ru/m/tankz/game/EntityBuilder.hx @@ -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; } } diff --git a/src/common/haxe/ru/m/tankz/game/Game.hx b/src/common/haxe/ru/m/tankz/game/Game.hx index aea2f5c..eb49826 100644 --- a/src/common/haxe/ru/m/tankz/game/Game.hx +++ b/src/common/haxe/ru/m/tankz/game/Game.hx @@ -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); diff --git a/src/common/haxe/ru/m/tankz/game/GameEvent.hx b/src/common/haxe/ru/m/tankz/game/GameEvent.hx index e5c8f6f..af3c96a 100644 --- a/src/common/haxe/ru/m/tankz/game/GameEvent.hx +++ b/src/common/haxe/ru/m/tankz/game/GameEvent.hx @@ -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); diff --git a/src/common/haxe/ru/m/tankz/game/GameRunner.hx b/src/common/haxe/ru/m/tankz/game/GameRunner.hx index 39da7e8..e7623e1 100644 --- a/src/common/haxe/ru/m/tankz/game/GameRunner.hx +++ b/src/common/haxe/ru/m/tankz/game/GameRunner.hx @@ -23,13 +23,14 @@ class GameRunner implements EngineListener implements GameListener { private var game(default, null):IGame; private var gameEventSignal(get, null):Signal; - + 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 { @@ -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; diff --git a/src/editor/haxe/ru/m/tankz/editor/level/MapEditView.hx b/src/editor/haxe/ru/m/tankz/editor/level/MapEditView.hx index b4b9ae9..57036d9 100644 --- a/src/editor/haxe/ru/m/tankz/editor/level/MapEditView.hx +++ b/src/editor/haxe/ru/m/tankz/editor/level/MapEditView.hx @@ -21,8 +21,8 @@ class SpawnPointEntity extends Entity { public var point(default, null):SpawnPoint; - public function new(point:SpawnPoint, rect:Rectangle) { - super(rect); + public function new(id:Int, point:SpawnPoint, rect:Rectangle) { + super(id, rect); this.point = point; } }