From 5ff7b1947579517bbb5d793e27974d4b28cb3497 Mon Sep 17 00:00:00 2001 From: shmyga Date: Mon, 22 Apr 2019 17:40:36 +0300 Subject: [PATCH] [common] add GameEvent --- .../haxe/ru/m/tankz/sound/SoundManager.hx | 65 +++++++++++-------- .../m/tankz/view/classic/ClassicGamePanel.hx | 5 ++ .../haxe/ru/m/tankz/view/common/GameFrame.hx | 7 +- .../ru/m/tankz/view/death/DeathGamePanel.hx | 7 +- .../ru/m/tankz/view/dota/DotaGamePanel.hx | 7 +- src/common/haxe/ru/m/tankz/control/Control.hx | 1 + src/common/haxe/ru/m/tankz/engine/Engine.hx | 41 ------------ src/common/haxe/ru/m/tankz/game/Game.hx | 19 ++++++ src/common/haxe/ru/m/tankz/game/GameEvent.hx | 17 +++++ 9 files changed, 97 insertions(+), 72 deletions(-) create mode 100644 src/common/haxe/ru/m/tankz/game/GameEvent.hx diff --git a/src/client/haxe/ru/m/tankz/sound/SoundManager.hx b/src/client/haxe/ru/m/tankz/sound/SoundManager.hx index f43dd57..225ac5b 100644 --- a/src/client/haxe/ru/m/tankz/sound/SoundManager.hx +++ b/src/client/haxe/ru/m/tankz/sound/SoundManager.hx @@ -5,11 +5,12 @@ import flash.media.Sound; import flash.media.SoundChannel; import flash.media.SoundTransform; import openfl.utils.Assets; -import ru.m.tankz.core.EntityType; -import ru.m.tankz.engine.Engine; -import ru.m.tankz.Type; +import ru.m.tankz.control.HumanControl; +import ru.m.tankz.game.Game; +import ru.m.tankz.game.GameEvent; +import ru.m.tankz.game.GameState; -class SoundManager implements EngineListener { +class SoundManager implements GameListener { private static var TAG(default, never):String = 'SoundManager'; #if flash @@ -75,32 +76,42 @@ class SoundManager implements EngineListener { channels.remove(event.currentTarget); } - public function onSpawn(entity:EntityType):Void { - switch entity { - case BULLET(_.tank.playerId.team => 'human'): - play('shot'); - case BONUS(_): - play('bonus_add'); - case _: - } + public function onGameStart(state:GameState):Void { + play('start'); } - public function onCollision(entity:EntityType, with:EntityType):Void { - switch [entity, with] { - case [BULLET(_), CELL(cell)]: - //play('bullet_wall'); - case [BULLET(_), EAGLE(_)]: + public function onGameChange(state:GameState):Void { + } + + public function onGameComplete(state:GameState):Void { + } + + public function onGameEvent(event:GameEvent):Void { + switch event { + case SPAWN_BULLET(player): + // ToDo: + if (Std.is(player.control, HumanControl)) { + play('shot'); + } + case HIT_TANK(player, target): + play('bullet_hit'); + case DESTROY_TANK(player, target): + // ToDo: + if (Std.is(target.control, HumanControl)) { + play('boom_player'); + } else { + play('boom_bot'); + } + case DESTROY_EAGLE(player, eagleTeamId): play('boom_player'); - case _: - } - } - - public function onDestroy(entity:EntityType, ?playerId:PlayerId):Void { - switch entity { - case TANK(_): - play('boom_bot'); - case BONUS(_): - play('bonus_get'); + case SPAWN_BONUS(bonus): + play('bonus'); + case TAKE_BONUS(player, bonus): + if (bonus.type == 'life') { + play('live'); + } else { + play('bonus_get'); + } case _: } } diff --git a/src/client/haxe/ru/m/tankz/view/classic/ClassicGamePanel.hx b/src/client/haxe/ru/m/tankz/view/classic/ClassicGamePanel.hx index 0c002e6..a7d1f1e 100644 --- a/src/client/haxe/ru/m/tankz/view/classic/ClassicGamePanel.hx +++ b/src/client/haxe/ru/m/tankz/view/classic/ClassicGamePanel.hx @@ -1,5 +1,6 @@ package ru.m.tankz.view.classic; +import ru.m.tankz.game.GameEvent; import haxework.view.LabelView; import haxework.view.VGroupView; import ru.m.tankz.game.GameState; @@ -38,4 +39,8 @@ import ru.m.tankz.view.common.LifeView; public function onGameComplete(state:GameState):Void { } + + public function onGameEvent(event:GameEvent):Void { + + } } diff --git a/src/client/haxe/ru/m/tankz/view/common/GameFrame.hx b/src/client/haxe/ru/m/tankz/view/common/GameFrame.hx index f318d65..b0e60b2 100644 --- a/src/client/haxe/ru/m/tankz/view/common/GameFrame.hx +++ b/src/client/haxe/ru/m/tankz/view/common/GameFrame.hx @@ -6,6 +6,7 @@ import haxe.Timer; import haxework.view.frame.FrameSwitcher; import haxework.view.GroupView; import ru.m.tankz.game.Game; +import ru.m.tankz.game.GameEvent; import ru.m.tankz.game.GameState; import ru.m.tankz.network.NetworkManager; import ru.m.tankz.render.Render; @@ -45,7 +46,7 @@ class GameFrame extends GroupView implements GameListener { private function start(state:GameState):Void { game = new Game(state.type); game.engine.connect(render); - game.engine.connect(soundManager); + game.connect(soundManager); game.connect(this); if (panel != null) { game.connect(panel); @@ -55,7 +56,6 @@ class GameFrame extends GroupView implements GameListener { timer.run = updateEngine; content.addEventListener(Event.ENTER_FRAME, _redraw); render.draw(game.engine); - soundManager.play('start'); } private function stop():Void { @@ -90,6 +90,9 @@ class GameFrame extends GroupView implements GameListener { switcher.change(ResultFrame.ID); } + public function onGameEvent(event:GameEvent):Void { + } + public function onHide():Void { stop(); } diff --git a/src/client/haxe/ru/m/tankz/view/death/DeathGamePanel.hx b/src/client/haxe/ru/m/tankz/view/death/DeathGamePanel.hx index de000a9..3340ee8 100644 --- a/src/client/haxe/ru/m/tankz/view/death/DeathGamePanel.hx +++ b/src/client/haxe/ru/m/tankz/view/death/DeathGamePanel.hx @@ -1,10 +1,11 @@ package ru.m.tankz.view.death; -import ru.m.tankz.game.GameState; import haxework.view.DataView; import haxework.view.LabelView; import haxework.view.VGroupView; +import ru.m.tankz.game.GameEvent; import ru.m.tankz.game.GameState.PlayerState; +import ru.m.tankz.game.GameState; import ru.m.tankz.view.common.IGamePanel; import ru.m.tankz.view.common.LifeView; @@ -27,4 +28,8 @@ import ru.m.tankz.view.common.LifeView; public function onGameComplete(state:GameState):Void { } + + public function onGameEvent(event:GameEvent):Void { + + } } diff --git a/src/client/haxe/ru/m/tankz/view/dota/DotaGamePanel.hx b/src/client/haxe/ru/m/tankz/view/dota/DotaGamePanel.hx index 29a1a70..b9527e5 100644 --- a/src/client/haxe/ru/m/tankz/view/dota/DotaGamePanel.hx +++ b/src/client/haxe/ru/m/tankz/view/dota/DotaGamePanel.hx @@ -1,9 +1,10 @@ package ru.m.tankz.view.dota; -import ru.m.tankz.preset.DotaGame; import haxework.view.HGroupView; import haxework.view.LabelView; +import ru.m.tankz.game.GameEvent; import ru.m.tankz.game.GameState; +import ru.m.tankz.preset.DotaGame; import ru.m.tankz.view.common.IGamePanel; import ru.m.tankz.view.common.LifeView; @@ -27,4 +28,8 @@ import ru.m.tankz.view.common.LifeView; public function onGameComplete(state:GameState):Void { } + + public function onGameEvent(event:GameEvent):Void { + + } } diff --git a/src/common/haxe/ru/m/tankz/control/Control.hx b/src/common/haxe/ru/m/tankz/control/Control.hx index 8baaa53..1165da8 100644 --- a/src/common/haxe/ru/m/tankz/control/Control.hx +++ b/src/common/haxe/ru/m/tankz/control/Control.hx @@ -13,6 +13,7 @@ enum TankAction { } class Control { + public var type:String; public var playerId(default, null):PlayerId; public var tankId(default, default):Int; private var handler:ControlHandler; diff --git a/src/common/haxe/ru/m/tankz/engine/Engine.hx b/src/common/haxe/ru/m/tankz/engine/Engine.hx index e7d4ac9..b691a6e 100755 --- a/src/common/haxe/ru/m/tankz/engine/Engine.hx +++ b/src/common/haxe/ru/m/tankz/engine/Engine.hx @@ -19,40 +19,6 @@ interface EngineListener { public function onDestroy(entity:EntityType, ?playerId:PlayerId):Void; } -class CollisionProcessor { - - private var engine:Engine; - - public function new(engine:Engine) { - this.engine = engine; - engine.collisionSignal.connect(onCollision); - } - - public function onCollision(entity:EntityType, with:EntityType):Void { - switch [entity, with] { - case [TANK(tank), TANK(other_tank)]: - tank.rect.lean(other_tank.rect); - case [TANK(tank), EAGLE(eagle)]: - tank.rect.lean(eagle.rect); - case [BULLET(bullet), BULLET(other_bullet)]: - engine.destroy(bullet); - engine.destroy(other_bullet); - case [BULLET(bullet), CELL(cell)]: - engine.destroy(bullet); - case [BULLET(bullet), EAGLE(eagle)]: - engine.destroy(bullet); - case _: - } - } - - public function dispose():Void { - if (engine != null) { - engine.collisionSignal.disconnect(onCollision); - engine = null; - } - } -} - @:yield @:dispatcher(EngineListener) class Engine implements ControlHandler { public var config(default, default):Config; @@ -60,8 +26,6 @@ class CollisionProcessor { public var entities(default, null):Map; - private var collision:CollisionProcessor; - private var time:Float; public function new(config:Config) { @@ -69,7 +33,6 @@ class CollisionProcessor { map = new LevelMap(config.map); entities = new Map(); time = Date.now().getTime(); - collision = new CollisionProcessor(this); } public function spawn(entity:Entity):Void { @@ -207,10 +170,6 @@ class CollisionProcessor { } public function dispose():Void { - if (collision != null) { - collision.dispose(); - collision = null; - } entities = new Map(); } diff --git a/src/common/haxe/ru/m/tankz/game/Game.hx b/src/common/haxe/ru/m/tankz/game/Game.hx index 0670351..88372dc 100644 --- a/src/common/haxe/ru/m/tankz/game/Game.hx +++ b/src/common/haxe/ru/m/tankz/game/Game.hx @@ -22,6 +22,7 @@ interface GameListener { public function onGameStart(state:GameState):Void; public function onGameChange(state:GameState):Void; public function onGameComplete(state:GameState):Void; + public function onGameEvent(event:GameEvent):Void; } @:dispatcher(GameListener) class Game implements EngineListener { @@ -174,6 +175,7 @@ interface GameListener { getPlayer(tank.playerId).control.start(); case BULLET(bullet): getPlayer(bullet.playerId).state.shots++; + gameEventSignal.emit(GameEvent.SPAWN_BULLET(getPlayer(bullet.playerId))); case _: } } @@ -186,9 +188,20 @@ interface GameListener { case _: } switch [entity, with] { + case [TANK(tank), TANK(other_tank)]: + tank.rect.lean(other_tank.rect); + case [TANK(tank), EAGLE(eagle)]: + tank.rect.lean(eagle.rect); + case [BULLET(bullet), BULLET(other_bullet)]: + engine.destroy(bullet); + engine.destroy(other_bullet); + case [BULLET(bullet), CELL(cell)]: + engine.destroy(bullet); + gameEventSignal.emit(GameEvent.HIT_CELL(getPlayer(bullet.playerId), cell)); case [TANK(tank), BONUS(bonus)]: applyBonus(tank, bonus); engine.destroy(bonus, tank.playerId); + gameEventSignal.emit(GameEvent.TAKE_BONUS(getPlayer(tank.playerId), bonus.config)); case [BULLET(bullet), TANK(tank)]/* | [TANK(tank), BULLET(bullet)]*/: if (bullet.tankId == tank.id || (!engine.config.game.friendlyFire && tank.playerId.team == bullet.playerId.team)) { // Nothing @@ -200,10 +213,13 @@ interface GameListener { tank.bonus = false; spawnBonus(); } + gameEventSignal.emit(GameEvent.HIT_TANK(getPlayer(bullet.playerId), getPlayer(tank.playerId))); } else if (tank.config.downgrade != null) { tank.config = engine.config.getTank(tank.config.downgrade); + gameEventSignal.emit(GameEvent.HIT_TANK(getPlayer(bullet.playerId), getPlayer(tank.playerId))); } else { engine.destroy(tank, bullet.tank.playerId); + gameEventSignal.emit(GameEvent.DESTROY_TANK(getPlayer(bullet.playerId), getPlayer(tank.playerId))); } } engine.destroy(bullet); @@ -217,6 +233,7 @@ interface GameListener { } checkComplete(); gameChangeSignal.emit(state); + gameEventSignal.emit(GameEvent.DESTROY_EAGLE(getPlayer(bullet.tank.playerId), eagle.team)); } case _: } @@ -274,6 +291,7 @@ interface GameListener { gameStartSignal.dispose(); gameChangeSignal.dispose(); gameCompleteSignal.dispose(); + gameEventSignal.dispose(); } private function spawnBonus(?type:BonusType):Void { @@ -282,6 +300,7 @@ interface GameListener { bonus.rect.x = Math.round(Math.random() * engine.map.width / engine.map.cellWidth) * engine.map.cellWidth; bonus.rect.y = Math.round(Math.random() * engine.map.height/ engine.map.cellHeight) * engine.map.cellHeight; engine.spawn(bonus); + gameEventSignal.emit(GameEvent.SPAWN_BONUS(bonus.config)); } inline private function alienTank(team:TeamId):Tank->Bool { diff --git a/src/common/haxe/ru/m/tankz/game/GameEvent.hx b/src/common/haxe/ru/m/tankz/game/GameEvent.hx new file mode 100644 index 0000000..cfad2d4 --- /dev/null +++ b/src/common/haxe/ru/m/tankz/game/GameEvent.hx @@ -0,0 +1,17 @@ +package ru.m.tankz.game; + +import ru.m.tankz.map.Grid; +import ru.m.tankz.config.Config; +import ru.m.tankz.Type; + +enum GameEvent { + SPAWN_BULLET(player:Player); + HIT_TANK(player:Player, target:Player); + DESTROY_TANK(player:Player, target:Player); + DESTROY_EAGLE(player:Player, eagleTeamId:TeamId); + HIT_CELL(player:Player, cell:GridCell); + DESTROY_CELL(player:Player, cell:GridCell); + SPAWN_BONUS(bonus:BonusConfig); + TAKE_BONUS(player:Player, bonus:BonusConfig); + ADD_SCORE(player:Player, score:Int); +}