diff --git a/src/client/haxe/ru/m/tankz/render/Render.hx b/src/client/haxe/ru/m/tankz/render/Render.hx index c87dfd4..ef01f9b 100755 --- a/src/client/haxe/ru/m/tankz/render/Render.hx +++ b/src/client/haxe/ru/m/tankz/render/Render.hx @@ -130,6 +130,9 @@ class Render extends SpriteView implements IRender { public function onMove(entity:EntityType):Void { } + public function onDestroy(entity:EntityType):Void { + } + public function onGameEvent(event:GameEvent):Void { switch event { case DESTROY(TANK(tank, who, wherewith, score)): diff --git a/src/common/haxe/ru/m/tankz/engine/Engine.hx b/src/common/haxe/ru/m/tankz/engine/Engine.hx index e6f6447..775825f 100755 --- a/src/common/haxe/ru/m/tankz/engine/Engine.hx +++ b/src/common/haxe/ru/m/tankz/engine/Engine.hx @@ -36,10 +36,17 @@ import ru.m.tankz.map.LevelMap; public function destroy(entityId:Int):Void { if (entities.exists(entityId)) { - entities.remove(entityId); + var entity = entities.remove(entityId); + destroySignal.emit(EntityTypeResolver.of(entity)); } } + public function destroyCell(x:Int, y:Int):Void { + var cell = map.grid.cells.get(new Point(x, y)); + cell.destroyed = true; + destroySignal.emit(EntityTypeResolver.of(cell)); + } + public function move(entityId:Int, direction:Direction):Void { if (entities.exists(entityId)) { cast(entities.get(entityId), MobileEntity).move(direction); @@ -71,15 +78,6 @@ import ru.m.tankz.map.LevelMap; }*/ if (entity.mx != 0 || entity.my != 0) { - var asTank:Tank = EntityTypeResolver.as(entity, Tank); - var asBullet:Bullet = EntityTypeResolver.as(entity, Bullet); - - if (asTank != null) { - if (asTank.freezing.active) { - continue; - } - } - var side:Line = entity.rect.getSide(entity.rect.direction.reverse()); var step:Point = new Point(entity.rect.direction.x * map.cellWidth / 4, entity.rect.direction.y * map.cellHeight / 4); var end:Point = side.center.add(new Point(entity.mx * (d / 30), entity.my * (d / 30))); @@ -123,22 +121,6 @@ import ru.m.tankz.map.LevelMap; } } - if (asBullet != null) { - if (collision) { - cells = map.grid.getCells(side.setLength(map.grid.cellWidth * 3)); - for (cell in cells) { - if (cell.armor > 0) { - if (cell.armor == asBullet.config.piercing) { - cell.destroyed = true; - } else if (cell.armor < asBullet.config.piercing) { - var brick = map.getBrick(cell); - brick.destroyed = true; - } - } - } - } - } - if (isStop) break; } diff --git a/src/common/haxe/ru/m/tankz/engine/IEngine.hx b/src/common/haxe/ru/m/tankz/engine/IEngine.hx index bf75149..b7a7e01 100644 --- a/src/common/haxe/ru/m/tankz/engine/IEngine.hx +++ b/src/common/haxe/ru/m/tankz/engine/IEngine.hx @@ -16,6 +16,7 @@ interface IEngine { public var spawnSignal(default, null):Signal1; public var collisionSignal(default, null):Signal2; public var moveSignal(default, null):Signal1; + public var destroySignal(default, null):Signal1; public function spawn(entity:Entity):Void; @@ -25,6 +26,8 @@ interface IEngine { public function destroy(entityId:Int):Void; + public function destroyCell(x:Int, y:Int):Void; + public function update():Void; public function iterTanks(filter:Tank->Bool):Iterator; @@ -40,4 +43,5 @@ interface EngineListener { public function onSpawn(entity:EntityType):Void; public function onCollision(entity:EntityType, with:EntityType):Void; public function onMove(entity:EntityType):Void; + public function onDestroy(entity:EntityType):Void; } diff --git a/src/common/haxe/ru/m/tankz/game/Game.hx b/src/common/haxe/ru/m/tankz/game/Game.hx index eb49826..228997e 100644 --- a/src/common/haxe/ru/m/tankz/game/Game.hx +++ b/src/common/haxe/ru/m/tankz/game/Game.hx @@ -1,5 +1,6 @@ package ru.m.tankz.game; +import haxe.Timer; import ru.m.geom.Point; import ru.m.tankz.bundle.IConfigBundle; import ru.m.tankz.config.Config; @@ -26,6 +27,7 @@ import ru.m.tankz.Type; public var state(default, null):GameState; private var builder:EntityBuilder; + private var timer:Timer; @:provide var configBundle:IConfigBundle; @@ -50,6 +52,10 @@ import ru.m.tankz.Type; } } + private function update():Void { + 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; @@ -67,9 +73,15 @@ import ru.m.tankz.Type; switch event { case GameEvent.START(state): this.state = state; + timer = new Timer(10); + timer.run = update; case GameEvent.COMPLETE(state, team): this.state = state; this.winner = team.id; + if (timer != null) { + timer.stop(); + timer = null; + } case GameEvent.SPAWN(entityId, EAGLE(teamId, point)): var eagle = builder.buildEagle(entityId, teamId); applyPoint(eagle, point); @@ -109,6 +121,8 @@ import ru.m.tankz.Type; 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.ACTION(tankId, MOVE(direction)): engine.move(tankId, direction); case GameEvent.ACTION(tankId, STOP): @@ -118,6 +132,10 @@ import ru.m.tankz.Type; } public function dispose():Void { + if (timer != null) { + timer.stop(); + timer = null; + } gameEventSignal.dispose(); engine.dispose(); } diff --git a/src/common/haxe/ru/m/tankz/game/GameRunner.hx b/src/common/haxe/ru/m/tankz/game/GameRunner.hx index e7623e1..b019784 100644 --- a/src/common/haxe/ru/m/tankz/game/GameRunner.hx +++ b/src/common/haxe/ru/m/tankz/game/GameRunner.hx @@ -1,8 +1,9 @@ package ru.m.tankz.game; -import haxe.ds.Option; import haxe.Timer; +import haxe.ds.Option; import haxework.signal.Signal; +import ru.m.geom.Line; import ru.m.geom.Point; import ru.m.tankz.config.Config; import ru.m.tankz.control.Control; @@ -24,7 +25,6 @@ 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; @@ -42,10 +42,6 @@ class GameRunner implements EngineListener implements GameListener { game.engine.disconnect(this); } - private function updateEngine():Void { - game.engine.update(); - } - 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; @@ -72,8 +68,6 @@ class GameRunner implements EngineListener implements GameListener { } } gameEventSignal.emit(GameEvent.START(state)); - timer = new Timer(10); - timer.run = updateEngine; } public function next():Option { @@ -182,7 +176,9 @@ class GameRunner implements EngineListener implements GameListener { } public function onMove(entity:EntityType):Void { + } + public function onDestroy(entity:EntityType):Void { } private function spawnBonus():Void { @@ -263,10 +259,7 @@ class GameRunner implements EngineListener implements GameListener { case GameEvent.START(_): case GameEvent.COMPLETE(_, _): - if (timer != null) { - timer.stop(); - timer = null; - } + case GameEvent.ACTION(tankId, SHOT): var tank:Tank = cast game.engine.entities.get(tankId); var player = game.getPlayer(tank.playerId); @@ -315,6 +308,20 @@ class GameRunner implements EngineListener implements GameListener { case GameEvent.DESTROY(BULLET(bullet)): 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)); + var cells = game.engine.map.grid.getCells(side.setLength(game.engine.map.grid.cellWidth * 3)); + for (cell in cells) { + if (cell.armor > 0) { + if (cell.armor == bullet.config.piercing) { + gameEventSignal.emit(GameEvent.DESTROY(CELL(cell, bullet.tank, 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))); + } + } + } + } case _: } }