From aa10825b128c733366bcb19645ba56e29ce26193 Mon Sep 17 00:00:00 2001 From: shmyga Date: Tue, 14 May 2019 12:24:29 +0300 Subject: [PATCH] [common] update GameEvent --- src/client/haxe/ru/m/tankz/render/Render.hx | 69 +++++++----- .../haxe/ru/m/tankz/sound/SoundManager.hx | 22 ++-- .../haxe/ru/m/tankz/storage/RecordStorage.hx | 3 - src/client/haxe/ru/m/tankz/view/GameFrame.hx | 2 +- src/common/haxe/ru/m/tankz/bot/BotControl.hx | 2 +- src/common/haxe/ru/m/tankz/core/Entity.hx | 10 +- src/common/haxe/ru/m/tankz/core/IKey.hx | 5 - src/common/haxe/ru/m/tankz/engine/Engine.hx | 10 +- src/common/haxe/ru/m/tankz/engine/IEngine.hx | 2 + src/common/haxe/ru/m/tankz/game/Game.hx | 57 +++++----- src/common/haxe/ru/m/tankz/game/GameEvent.hx | 45 ++++---- src/common/haxe/ru/m/tankz/game/GameRunner.hx | 105 +++++++++++------- src/common/haxe/ru/m/tankz/game/IGame.hx | 2 +- src/common/haxe/ru/m/tankz/map/Brick.hx | 11 +- 14 files changed, 194 insertions(+), 151 deletions(-) delete mode 100644 src/common/haxe/ru/m/tankz/core/IKey.hx diff --git a/src/client/haxe/ru/m/tankz/render/Render.hx b/src/client/haxe/ru/m/tankz/render/Render.hx index ef01f9b..2ade2d8 100755 --- a/src/client/haxe/ru/m/tankz/render/Render.hx +++ b/src/client/haxe/ru/m/tankz/render/Render.hx @@ -1,5 +1,6 @@ package ru.m.tankz.render; +import ru.m.geom.Rectangle; import flash.display.DisplayObjectContainer; import flash.display.Graphics; import flash.display.Sprite; @@ -24,7 +25,7 @@ class Render extends SpriteView implements IRender { private var upperLayer:Sprite; private var background:Sprite; - private var items:Map>; + private var items:Map>; public function new() { super(); @@ -53,7 +54,7 @@ class Render extends SpriteView implements IRender { public function draw(game:IEngine):Void { for (brick in game.map.bricks) if (brick.config.index > 0) { - if (!items.exists(brick.key)) { + if (!items.exists(brick.id)) { var item:RenderItem = switch(brick.config.type) { case 'ace' | 'bush': new BrickItem(brick); case 'water': new BrickAnimateItem(brick); @@ -61,7 +62,7 @@ class Render extends SpriteView implements IRender { case x: null; }; if (item != null) { - items[brick.key] = item; + items[brick.id] = item; if (brick.config.layer > 2) { upLayer.addChild(item.view); } else { @@ -101,23 +102,23 @@ class Render extends SpriteView implements IRender { switch entity { case EAGLE(eagle): var item = new EagleItem(eagle); - items.set(eagle.key, item); + items.set(eagle.id, item); entryLayer.addChild(item.view); item.update(); case TANK(tank): var item = new TankItem(tank); - items.set(tank.key, item); + items.set(tank.id, item); entryLayer.addChild(item.view); item.update(); playAnimate(tank.rect.center, AnimateBundle.tankSpawn()); case BULLET(bullet): var item = new BulletItem(bullet); - items.set(bullet.key, item); + items.set(bullet.id, item); entryLayer.addChild(item.view); item.update(); case BONUS(bonus): var item = new BonusItem(bonus); - items.set(bonus.key, item); + items.set(bonus.id, item); upperLayer.addChild(item.view); item.update(); case _: @@ -135,34 +136,44 @@ class Render extends SpriteView implements IRender { public function onGameEvent(event:GameEvent):Void { switch event { - case DESTROY(TANK(tank, who, wherewith, score)): - if (items.exists(tank.key)) { - entryLayer.removeChild(items.get(tank.key).view); - items.remove(tank.key); - playAnimate(tank.rect.center, AnimateBundle.tankBoom()); - if (score != 0) { - showScore(tank.rect.center, score); + case DESTROY(TANK(id, shot)): + if (items.exists(id)) { + var item = items[id]; + entryLayer.removeChild(item.view); + var rect:Rectangle = item.value.rect; + playAnimate(rect.center, AnimateBundle.tankBoom()); + if (shot.score != 0) { + showScore(rect.center, shot.score); } + items.remove(id); } - case DESTROY(BULLET(bullet)): - if (items.exists(bullet.key)) { - entryLayer.removeChild(items.get(bullet.key).view); - items.remove(bullet.key); - var point = bullet.rect.center.add(new Point(bullet.rect.width * bullet.rect.direction.x, bullet.rect.height * bullet.rect.direction.y)); + case DESTROY(BULLET(id)): + if (items.exists(id)) { + var item = items[id]; + entryLayer.removeChild(item.view); + var rect:Rectangle = item.value.rect; + var point = rect.center.add(new Point(rect.width * rect.direction.x, rect.height * rect.direction.y)); playAnimate(point, AnimateBundle.bulletBoom()); + items.remove(id); } - case DESTROY(BONUS(bonus, who, score)): - if (items.exists(bonus.key)) { - upperLayer.removeChild(items.get(bonus.key).view); - items.remove(bonus.key); - if (score != 0) { - showScore(bonus.rect.center, score); + case DESTROY(BONUS(id, shot)): + if (items.exists(id)) { + var item = items[id]; + upperLayer.removeChild(item.view); + var rect:Rectangle = item.value.rect; + if (shot.score != 0) { + showScore(rect.center, shot.score); } + items.remove(id); } - case DESTROY(EAGLE(eagle, who, wherewith, score)): - playAnimate(eagle.rect.center, AnimateBundle.tankBoom()); - if (score != 0) { - showScore(eagle.rect.center, score); + case DESTROY(EAGLE(id, shot)): + if (items.exists(id)) { + var item = items[id]; + var rect:Rectangle = item.value.rect; + playAnimate(rect.center, AnimateBundle.tankBoom()); + if (shot.score != 0) { + showScore(rect.center, shot.score); + } } case _: } diff --git a/src/client/haxe/ru/m/tankz/sound/SoundManager.hx b/src/client/haxe/ru/m/tankz/sound/SoundManager.hx index 1729fd0..40fcc6e 100644 --- a/src/client/haxe/ru/m/tankz/sound/SoundManager.hx +++ b/src/client/haxe/ru/m/tankz/sound/SoundManager.hx @@ -76,26 +76,28 @@ class SoundManager implements GameListener { public function onGameEvent(event:GameEvent):Void { switch event { - case START(state): + case START(_): play('start'); - case SPAWN(_, BULLET(_)): - if (false /* ToDo: human tank */) { + 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)): + case HIT(TANK(_, _)): play('bullet_hit'); - case DESTROY(TANK(tank, who, wherewith, score)): - if (true /* ToDo: human tank */) { + case DESTROY(TANK(_, _)): + if (true /*ToDo: human tank*/) { play('boom_player'); } else { play('boom_bot'); } - case DESTROY(EAGLE(eagle, who, wherewith, score)): + case DESTROY(EAGLE(_, _)): play('boom_player'); - case DESTROY(BONUS(bonus, who, score)): - if (bonus.type == 'life') { + case DESTROY(BONUS(_, _)): + // ToDo: bonus type + play('bonus_get'); + if (false /*ToDo: bonus.type == 'life'*/) { play('live'); } else { play('bonus_get'); diff --git a/src/client/haxe/ru/m/tankz/storage/RecordStorage.hx b/src/client/haxe/ru/m/tankz/storage/RecordStorage.hx index a6f0622..e8a8055 100644 --- a/src/client/haxe/ru/m/tankz/storage/RecordStorage.hx +++ b/src/client/haxe/ru/m/tankz/storage/RecordStorage.hx @@ -13,9 +13,6 @@ import ru.m.tankz.game.record.GameRecord; } public function save(record:GameRecord):Void { - trace(record.id); - trace(record.date); - trace(record.events); write(record.id, record); } diff --git a/src/client/haxe/ru/m/tankz/view/GameFrame.hx b/src/client/haxe/ru/m/tankz/view/GameFrame.hx index ddd42e2..c92fbe9 100644 --- a/src/client/haxe/ru/m/tankz/view/GameFrame.hx +++ b/src/client/haxe/ru/m/tankz/view/GameFrame.hx @@ -98,7 +98,7 @@ import ru.m.tankz.view.game.GameView; public function onGameEvent(event:GameEvent):Void { switch event { - case GameEvent.COMPLETE(state, winner): + case GameEvent.COMPLETE(state, _): // ToDo: recordStorage.save(recorder.record); result = state; diff --git a/src/common/haxe/ru/m/tankz/bot/BotControl.hx b/src/common/haxe/ru/m/tankz/bot/BotControl.hx index 294c0dc..8d5f3ca 100644 --- a/src/common/haxe/ru/m/tankz/bot/BotControl.hx +++ b/src/common/haxe/ru/m/tankz/bot/BotControl.hx @@ -12,7 +12,7 @@ class BotControl extends Control { private var tank(get, null):Tank; private inline function get_tank():Tank { - return handler == null ? null : cast handler.engine.entities[tankId]; + return handler == null ? null : handler.engine.getEntity(tankId); } override public function stop():Void { diff --git a/src/common/haxe/ru/m/tankz/core/Entity.hx b/src/common/haxe/ru/m/tankz/core/Entity.hx index 8f8b88b..172591d 100755 --- a/src/common/haxe/ru/m/tankz/core/Entity.hx +++ b/src/common/haxe/ru/m/tankz/core/Entity.hx @@ -3,12 +3,10 @@ package ru.m.tankz.core; import ru.m.geom.Rectangle; import Type; - -class Entity implements IKey { +class Entity { 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(id:Int, rect:Rectangle) { @@ -17,11 +15,7 @@ class Entity implements IKey { this.rect = rect; } - private function get_key():String { - return '$type:$id'; - } - public function toString():String { - return key; + return '$type($id)'; } } diff --git a/src/common/haxe/ru/m/tankz/core/IKey.hx b/src/common/haxe/ru/m/tankz/core/IKey.hx deleted file mode 100644 index 56e61ec..0000000 --- a/src/common/haxe/ru/m/tankz/core/IKey.hx +++ /dev/null @@ -1,5 +0,0 @@ -package ru.m.tankz.core; - -interface IKey { - public var key(get, null):String; -} diff --git a/src/common/haxe/ru/m/tankz/engine/Engine.hx b/src/common/haxe/ru/m/tankz/engine/Engine.hx index 775825f..549e397 100755 --- a/src/common/haxe/ru/m/tankz/engine/Engine.hx +++ b/src/common/haxe/ru/m/tankz/engine/Engine.hx @@ -18,6 +18,7 @@ import ru.m.tankz.map.LevelMap; public var config(default, default):Config; public var map(default, null):LevelMap; + public var allEntities(default, null):Map; public var entities(default, null):Map; private var time:Float; @@ -25,19 +26,26 @@ import ru.m.tankz.map.LevelMap; public function new(config:Config) { this.config = config; map = new LevelMap(config.map); + allEntities = new Map(); entities = new Map(); time = Date.now().getTime(); } + public function getEntity(entityId:Int):T { + return cast allEntities.get(entityId); + } + public function spawn(entity:Entity):Void { + allEntities.set(entity.id, entity); entities.set(entity.id, entity); spawnSignal.emit(EntityTypeResolver.of(entity)); } public function destroy(entityId:Int):Void { if (entities.exists(entityId)) { - var entity = entities.remove(entityId); + var entity = entities[entityId]; destroySignal.emit(EntityTypeResolver.of(entity)); + entities.remove(entityId); } } diff --git a/src/common/haxe/ru/m/tankz/engine/IEngine.hx b/src/common/haxe/ru/m/tankz/engine/IEngine.hx index b7a7e01..52dc073 100644 --- a/src/common/haxe/ru/m/tankz/engine/IEngine.hx +++ b/src/common/haxe/ru/m/tankz/engine/IEngine.hx @@ -18,6 +18,8 @@ interface IEngine { public var moveSignal(default, null):Signal1; public var destroySignal(default, null):Signal1; + public function getEntity(entityId:Int):T; + public function spawn(entity:Entity):Void; public function move(entityId:Int, direction:Direction):Void; diff --git a/src/common/haxe/ru/m/tankz/game/Game.hx b/src/common/haxe/ru/m/tankz/game/Game.hx index 228997e..8260c86 100644 --- a/src/common/haxe/ru/m/tankz/game/Game.hx +++ b/src/common/haxe/ru/m/tankz/game/Game.hx @@ -10,6 +10,7 @@ import ru.m.tankz.core.EntityType; import ru.m.tankz.core.Tank; import ru.m.tankz.engine.Engine; import ru.m.tankz.engine.IEngine; +import ru.m.tankz.game.GameEvent; import ru.m.tankz.game.GameState; import ru.m.tankz.game.IGame; import ru.m.tankz.Type; @@ -56,9 +57,11 @@ import ru.m.tankz.Type; engine.update(); } - private function applyPoint(entity:Entity, point:SpawnPoint):Void { - entity.rect.center = new Point((point.x + 1) * config.map.cellWidth, (point.y + 1) * config.map.cellHeight); - entity.rect.direction = point.direction; + private function applyPosition(entity:Entity, position:Position):Void { + entity.rect.center = new Point(position.x, position.y); + if (position.direction != null) { + entity.rect.direction = position.direction; + } } public inline function getTeam(teamId:TeamId):Team { @@ -75,22 +78,22 @@ import ru.m.tankz.Type; this.state = state; timer = new Timer(10); timer.run = update; - case GameEvent.COMPLETE(state, team): + case GameEvent.COMPLETE(state, winnerId): this.state = state; - this.winner = team.id; + this.winner = winnerId; if (timer != null) { timer.stop(); timer = null; } - case GameEvent.SPAWN(entityId, EAGLE(teamId, point)): - var eagle = builder.buildEagle(entityId, teamId); - applyPoint(eagle, point); + case GameEvent.SPAWN(EAGLE(id, position, teamId)): + var eagle = builder.buildEagle(id, teamId); + applyPosition(eagle, position); var team = getTeam(teamId); team.eagleId = eagle.id; engine.spawn(eagle); - case GameEvent.SPAWN(entityId, TANK(playerId, type, point)): - var tank = builder.buildTank(entityId, playerId, type); - applyPoint(tank, point); + case GameEvent.SPAWN(TANK(id, position, playerId, type)): + var tank = builder.buildTank(id, playerId, type); + applyPosition(tank, position); var player = getPlayer(playerId); player.tankId = tank.id; player.state.tank = tank.config.type; @@ -101,28 +104,26 @@ import ru.m.tankz.Type; tank.bonus = Math.random() < player.config.bonus; // engine.spawn(tank); - case GameEvent.SPAWN(entityId, BULLET(playerId)): + case GameEvent.SPAWN(BULLET(id, position, playerId)): var player = getPlayer(playerId); var tank:Tank = cast engine.entities.get(player.tankId); - var bullet = builder.buildBullet(entityId, playerId, tank.config.type); + var bullet = builder.buildBullet(id, playerId, tank.config.type); + applyPosition(bullet, position); 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); + bullet.move(bullet.rect.direction); engine.spawn(bullet); - 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; + case GameEvent.SPAWN(BONUS(id, position, type)): + var bonus = builder.buildBonus(id, type); + applyPosition(bonus, position); engine.spawn(bonus); - case GameEvent.DESTROY(TANK(tank, who, wherewith, score)): - engine.destroy(tank.id); - case GameEvent.DESTROY(BONUS(bonus, who, score)): - engine.destroy(bonus.id); - case GameEvent.DESTROY(BULLET(bullet)): - engine.destroy(bullet.id); - case GameEvent.DESTROY(CELL(cell, tank, bullet)): - engine.destroyCell(cell.cellX, cell.cellY); + case GameEvent.DESTROY(TANK(id, _)): + engine.destroy(id); + case GameEvent.DESTROY(BONUS(id, _)): + engine.destroy(id); + case GameEvent.DESTROY(BULLET(id)): + engine.destroy(id); + case GameEvent.DESTROY(CELL(cellX, cellY, _)): + engine.destroyCell(cellX, cellY); case GameEvent.ACTION(tankId, MOVE(direction)): engine.move(tankId, direction); case GameEvent.ACTION(tankId, STOP): diff --git a/src/common/haxe/ru/m/tankz/game/GameEvent.hx b/src/common/haxe/ru/m/tankz/game/GameEvent.hx index af3c96a..bd21650 100644 --- a/src/common/haxe/ru/m/tankz/game/GameEvent.hx +++ b/src/common/haxe/ru/m/tankz/game/GameEvent.hx @@ -1,32 +1,39 @@ package ru.m.tankz.game; -import ru.m.tankz.config.Config; +import ru.m.geom.Direction; import ru.m.tankz.control.Control; -import ru.m.tankz.core.Bonus; -import ru.m.tankz.core.Bullet; -import ru.m.tankz.core.Eagle; -import ru.m.tankz.core.Tank; -import ru.m.tankz.map.Grid; import ru.m.tankz.Type; +typedef Position = { + var x:Float; + var y:Float; + @:optional var direction:Direction; +} + enum SpawnEvent { - EAGLE(teamId:TeamId, point:SpawnPoint); - TANK(playerId:PlayerId, type:TankType, point:SpawnPoint); - BULLET(playerId:PlayerId); - BONUS(type:BonusType, point:{x:Int, y:Int}); + EAGLE(id:Int, position:Position, teamId:TeamId); + TANK(id:Int, position:Position, playerId:PlayerId, type:TankType); + BULLET(id:Int, position:Position, playerId:PlayerId); + BONUS(id:Int, position:Position, type:BonusType); +} + +typedef Shot = { + var tankId:Int; + @:optional var bulletId:Int; + @:optional var score:Int; } enum HitEvent { - TANK(tank:Tank, who:Tank, wherewith:Bullet); - CELL(cell:GridCell, who:Tank, wherewith:Bullet); + TANK(id:Int, shot:Shot); + CELL(cellX:Int, cellY:Int, shot:Shot); } enum DestroyEvent { - EAGLE(eagle:Eagle, who:Tank, wherewith:Bullet, score:Int); - TANK(tank:Tank, who:Tank, wherewith:Bullet, score:Int); - CELL(cell:GridCell, who:Tank, wherewith:Bullet); - BONUS(bonus:Bonus, who:Tank, score:Int); - BULLET(bullet:Bullet); + EAGLE(id:Int, shot:Shot); + TANK(id:Int, shot:Shot); + BONUS(id:Int, shot:Shot); + BULLET(id:Int); + CELL(cellX:Int, cellY:Int, shot:Shot); } enum ChangeEvent { @@ -38,10 +45,10 @@ enum ChangeEvent { enum GameEvent { START(state:GameState); - SPAWN(entityId:Int, event:SpawnEvent); + SPAWN(event:SpawnEvent); HIT(event:HitEvent); DESTROY(event:DestroyEvent); CHANGE(event:ChangeEvent); - COMPLETE(state:GameState, winner:Team); + COMPLETE(state:GameState, winnerId:TeamId); ACTION(tankId:Int, action:TankAction); } diff --git a/src/common/haxe/ru/m/tankz/game/GameRunner.hx b/src/common/haxe/ru/m/tankz/game/GameRunner.hx index b019784..4a2ff68 100644 --- a/src/common/haxe/ru/m/tankz/game/GameRunner.hx +++ b/src/common/haxe/ru/m/tankz/game/GameRunner.hx @@ -1,7 +1,7 @@ package ru.m.tankz.game; -import haxe.Timer; import haxe.ds.Option; +import haxe.Timer; import haxework.signal.Signal; import ru.m.geom.Line; import ru.m.geom.Point; @@ -10,11 +10,12 @@ import ru.m.tankz.control.Control; import ru.m.tankz.control.Controller; import ru.m.tankz.control.IControlFactory; import ru.m.tankz.core.Bonus; +import ru.m.tankz.core.Bullet; import ru.m.tankz.core.Eagle; -import ru.m.tankz.core.Entity; import ru.m.tankz.core.EntityType; import ru.m.tankz.core.Tank; import ru.m.tankz.engine.IEngine; +import ru.m.tankz.game.GameEvent; import ru.m.tankz.game.IGame; import ru.m.tankz.game.Spawner; import ru.m.tankz.Type; @@ -42,9 +43,12 @@ class GameRunner implements EngineListener implements GameListener { game.engine.disconnect(this); } - private function applyPoint(entity:Entity, point:SpawnPoint):Void { - entity.rect.center = new Point((point.x + 1) * game.engine.map.cellWidth, (point.y + 1) * game.engine.map.cellHeight); - entity.rect.direction = point.direction; + private function pointToPosition(point:{x:Int, y:Int, direction:String}):Position { + return { + x: (point.x + 1) * game.config.map.cellWidth, + y: (point.y + 1) * game.config.map.cellHeight, + direction: point.direction, + } } public function start(state:GameState):Void { @@ -64,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(++entityId, EAGLE(team.id, point))); + gameEventSignal.emit(GameEvent.SPAWN(EAGLE(++entityId, pointToPosition(point), team.id))); } } gameEventSignal.emit(GameEvent.START(state)); @@ -82,7 +86,7 @@ class GameRunner implements EngineListener implements GameListener { } private function spawn(task:SpawnTask):Void { - gameEventSignal.emit(GameEvent.SPAWN(++entityId, TANK(task.playerId, task.tankType, task.point))); + gameEventSignal.emit(GameEvent.SPAWN(TANK(++entityId, pointToPosition(task.point), task.playerId, task.tankType))); } private function checkComplete():Void { @@ -114,13 +118,21 @@ class GameRunner implements EngineListener implements GameListener { } } Timer.delay(function() { - gameEventSignal.emit(GameEvent.COMPLETE(game.state, game.getTeam(winner))); + gameEventSignal.emit(GameEvent.COMPLETE(game.state, winner)); }, 5000); } public function onSpawn(entity:EntityType):Void { } + private static function buildShot(bullet:Bullet, score:Int = 0):Shot { + return { + tankId: bullet.tank.id, + bulletId: bullet.id, + score: score, + } + } + public function onCollision(entity:EntityType, with:EntityType):Void { switch entity { case EntityType.TANK(tank): @@ -134,13 +146,13 @@ class GameRunner implements EngineListener implements GameListener { case [TANK(tank), EAGLE(eagle)]: tank.rect.lean(eagle.rect); case [BULLET(bullet), BULLET(other_bullet)]: - gameEventSignal.emit(GameEvent.DESTROY(BULLET(bullet))); - gameEventSignal.emit(GameEvent.DESTROY(BULLET(other_bullet))); + gameEventSignal.emit(GameEvent.DESTROY(BULLET(bullet.id))); + gameEventSignal.emit(GameEvent.DESTROY(BULLET(other_bullet.id))); case [BULLET(bullet), CELL(cell)]: - gameEventSignal.emit(GameEvent.HIT(CELL(cell, bullet.tank, bullet))); - gameEventSignal.emit(GameEvent.DESTROY(BULLET(bullet))); + gameEventSignal.emit(GameEvent.HIT(CELL(cell.cellX, cell.cellY, buildShot(bullet)))); + gameEventSignal.emit(GameEvent.DESTROY(BULLET(bullet.id))); case [TANK(tank), BONUS(bonus)]: - gameEventSignal.emit(GameEvent.DESTROY(BONUS(bonus, tank, bonus.config.score))); + gameEventSignal.emit(GameEvent.DESTROY(BONUS(bonus.id, {tankId: tank.id, score: bonus.config.score}))); case [BULLET(bullet), TANK(tank)]/* | [TANK(tank), BULLET(bullet)]*/: if (bullet.tankId == tank.id || (!game.config.game.friendlyFire && tank.playerId.team == bullet.playerId.team)) { // Nothing @@ -152,25 +164,25 @@ class GameRunner implements EngineListener implements GameListener { tank.bonus = false; spawnBonus(); } - gameEventSignal.emit(GameEvent.HIT(TANK(tank, bullet.tank, bullet))); + gameEventSignal.emit(GameEvent.HIT(TANK(tank.id, buildShot(bullet)))); } else if (tank.config.downgrade != null) { tank.config = game.config.getTank(tank.config.downgrade); - gameEventSignal.emit(GameEvent.HIT(TANK(tank, bullet.tank, bullet))); + gameEventSignal.emit(GameEvent.HIT(TANK(tank.id, buildShot(bullet)))); } else { var score = tank.config.score; if (tank.playerId.team == bullet.playerId.team) { score = Math.round(score * -0.5); } - gameEventSignal.emit(GameEvent.DESTROY(TANK(tank, bullet.tank, bullet, score))); + gameEventSignal.emit(GameEvent.DESTROY(TANK(tank.id, buildShot(bullet, score)))); } } - gameEventSignal.emit(GameEvent.DESTROY(BULLET(bullet))); + gameEventSignal.emit(GameEvent.DESTROY(BULLET(bullet.id))); } case [BULLET(bullet), EAGLE(eagle)]: if (!eagle.protect.active) { - gameEventSignal.emit(GameEvent.DESTROY(EAGLE(eagle, bullet.tank, bullet, eagle.score))); + gameEventSignal.emit(GameEvent.DESTROY(EAGLE(eagle.id, buildShot(bullet, eagle.score)))); } - gameEventSignal.emit(GameEvent.DESTROY(BULLET(bullet))); + gameEventSignal.emit(GameEvent.DESTROY(BULLET(bullet.id))); case _: } } @@ -186,8 +198,9 @@ class GameRunner implements EngineListener implements GameListener { var point = { x: Math.floor(Math.random() * (game.engine.map.gridWidth - 1)), y: Math.floor(Math.random() * (game.engine.map.gridHeight - 1)), + direction: "right", } - gameEventSignal.emit(GameEvent.SPAWN(++entityId, BONUS(type, point))); + gameEventSignal.emit(GameEvent.SPAWN(BONUS(++entityId, pointToPosition(point), type))); } private inline function alienTank(team:TeamId):Tank->Bool { @@ -202,7 +215,7 @@ class GameRunner implements EngineListener implements GameListener { upgradeTank(tank); case "grenade": for (t in game.engine.iterTanks(alienTank(tank.playerId.team))) { - gameEventSignal.emit(GameEvent.DESTROY(TANK(t, tank, null, 0))); + gameEventSignal.emit(GameEvent.DESTROY(TANK(t.id, {tankId: tank.id}))); } case "helmet": tank.protect.on(bonus.config.duration); @@ -220,7 +233,7 @@ class GameRunner implements EngineListener implements GameListener { case "gun": upgradeTank(tank, 5); case _: - gameEventSignal.emit(GameEvent.DESTROY(TANK(tank, null, null, 0))); // :-D + gameEventSignal.emit(GameEvent.DESTROY(TANK(tank.id, {tankId: tank.id}))); // :-D } } @@ -264,19 +277,29 @@ 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(++entityId, BULLET(tank.playerId))); + var rect = tank.rect; + var point = rect.center.add(new Point(rect.width / 4 * rect.direction.x, rect.height / 4 * rect.direction.y)); + var position = { + x: point.x, + y: point.y, + direction: rect.direction, + } + gameEventSignal.emit(GameEvent.SPAWN(BULLET(++entityId, position, tank.playerId))); } - case GameEvent.SPAWN(entityId, TANK(playerId, _)): + case GameEvent.SPAWN(TANK(_, _, playerId, _)): game.getPlayer(playerId).control.start(); - case GameEvent.SPAWN(entityId, BULLET(playerId)): + case GameEvent.SPAWN(BULLET(_, _, playerId)): game.getPlayer(playerId).bullets++; - case GameEvent.DESTROY(EAGLE(eagle, who, wherewith, score)): + case GameEvent.DESTROY(EAGLE(id, shot)): + var eagle:Eagle = game.engine.getEntity(id); eagle.death = true; - if (score != 0) { - changeScore(who.playerId, score); + if (shot.score != 0) { + var tank:Tank = game.engine.getEntity(shot.tankId); + changeScore(tank.playerId, shot.score); } checkComplete(); - case GameEvent.DESTROY(TANK(tank, who, wherewith, score)): + case GameEvent.DESTROY(TANK(id, shot)): + var tank:Tank = game.engine.getEntity(id); var team = game.getTeam(tank.playerId.team); var player = game.getPlayer(tank.playerId); player.control.stop(); @@ -294,18 +317,22 @@ class GameRunner implements EngineListener implements GameListener { if (!team.isAlive) { checkComplete(); } - if (tank.bonus && wherewith != null) { + if (tank.bonus && shot.bulletId != null) { spawnBonus(); } - if (score != 0) { - changeScore(who.playerId, score); + if (shot.score != 0) { + var shooterTank:Tank = game.engine.getEntity(shot.tankId); + changeScore(shooterTank.playerId, shot.score); } - case GameEvent.DESTROY(BONUS(bonus, who, score)): - applyBonus(who, bonus); - if (score != 0) { - changeScore(who.playerId, score); + case GameEvent.DESTROY(BONUS(id, shot)): + var bonus:Bonus = game.engine.getEntity(id); + var tank:Tank = game.engine.getEntity(shot.tankId); + applyBonus(tank, bonus); + if (shot.score != 0) { + changeScore(tank.playerId, shot.score); } - case GameEvent.DESTROY(BULLET(bullet)): + case GameEvent.DESTROY(BULLET(id)): + var bullet:Bullet = game.engine.getEntity(id); var player = game.getPlayer(bullet.playerId); player.bullets--; var side:Line = bullet.rect.getSide(bullet.rect.direction.reverse()).move(new Point(bullet.mx, bullet.my)); @@ -313,11 +340,11 @@ class GameRunner implements EngineListener implements GameListener { for (cell in cells) { if (cell.armor > 0) { if (cell.armor == bullet.config.piercing) { - gameEventSignal.emit(GameEvent.DESTROY(CELL(cell, bullet.tank, bullet))); + gameEventSignal.emit(GameEvent.DESTROY(CELL(cell.cellX, cell.cellY, buildShot(bullet)))); } else if (cell.armor < bullet.config.piercing) { var brick = game.engine.map.getBrick(cell); for (cell in brick.cells) { - gameEventSignal.emit(GameEvent.DESTROY(CELL(cell, bullet.tank, bullet))); + gameEventSignal.emit(GameEvent.DESTROY(CELL(cell.cellX, cell.cellY, buildShot(bullet)))); } } } diff --git a/src/common/haxe/ru/m/tankz/game/IGame.hx b/src/common/haxe/ru/m/tankz/game/IGame.hx index abbf7d6..6d21b0c 100644 --- a/src/common/haxe/ru/m/tankz/game/IGame.hx +++ b/src/common/haxe/ru/m/tankz/game/IGame.hx @@ -5,7 +5,7 @@ import ru.m.tankz.config.Config; import ru.m.tankz.engine.IEngine; import ru.m.tankz.Type; -interface IGame { +interface IGame extends GameListener { public var type(default, null):GameType; public var teams(default, null):Map; public var config(default, null):Config; diff --git a/src/common/haxe/ru/m/tankz/map/Brick.hx b/src/common/haxe/ru/m/tankz/map/Brick.hx index ea5daaf..130bdcb 100644 --- a/src/common/haxe/ru/m/tankz/map/Brick.hx +++ b/src/common/haxe/ru/m/tankz/map/Brick.hx @@ -5,13 +5,12 @@ import ru.m.geom.Rectangle; import haxe.ds.HashMap; import ru.m.geom.Point; import ru.m.tankz.config.Config; -import ru.m.tankz.core.IKey; +class Brick { + public var id(get, null):Int; -class Brick implements IKey { public var cellX(default, null):Int; public var cellY(default, null):Int; - public var key(get, null):String; public var mapConfig(default, null):MapConfig; public var config(default, default):BrickConfig; @@ -67,11 +66,11 @@ class Brick implements IKey { return value; } - public function get_key():String { - return 'brick:$cellX:$cellY'; + public function get_id():Int { + return -((cellX * 1000) + cellY); } public function toString() { - return 'Brick{${config.type},$cellX:$cellY}'; + return 'Brick(${config.type},$cellX:$cellY)'; } }