From d11d0ac53f735d23b49eadfdbf3628180aacb3d1 Mon Sep 17 00:00:00 2001 From: shmyga Date: Sun, 5 May 2019 21:18:36 +0300 Subject: [PATCH] [common] add engine interface --- src/client/haxe/ru/m/tankz/render/Render.hx | 14 ++++---- .../haxe/ru/m/tankz/sound/SoundManager.hx | 2 +- .../haxe/ru/m/tankz/view/common/IGamePanel.hx | 2 +- src/common/haxe/ru/m/tankz/engine/Engine.hx | 8 ++--- src/common/haxe/ru/m/tankz/engine/IEngine.hx | 36 +++++++++++++++++++ src/common/haxe/ru/m/tankz/game/Game.hx | 8 ++--- src/common/haxe/ru/m/tankz/game/GameRunner.hx | 8 +++-- src/common/haxe/ru/m/tankz/game/IGame.hx | 10 ++++-- .../ru/m/tankz/game/record/GameRecorder.hx | 2 +- 9 files changed, 64 insertions(+), 26 deletions(-) create mode 100644 src/common/haxe/ru/m/tankz/engine/IEngine.hx diff --git a/src/client/haxe/ru/m/tankz/render/Render.hx b/src/client/haxe/ru/m/tankz/render/Render.hx index bd1787f..dd096f8 100755 --- a/src/client/haxe/ru/m/tankz/render/Render.hx +++ b/src/client/haxe/ru/m/tankz/render/Render.hx @@ -10,9 +10,9 @@ import promhx.Promise; import ru.m.animate.Animate; import ru.m.animate.OnceAnimate; import ru.m.geom.Point; -import ru.m.tankz.engine.Engine; -import ru.m.tankz.game.Game.GameListener; +import ru.m.tankz.engine.IEngine; import ru.m.tankz.game.GameEvent; +import ru.m.tankz.game.IGame; import ru.m.tankz.render.RenderItem; class Render extends SpriteView implements GameListener { @@ -42,18 +42,16 @@ class Render extends SpriteView implements GameListener { reset(); } - private function drawBackground(game:Engine):Void { - var mapWidth = game.map.gridWidth * game.map.cellWidth; - var mapHeight = game.map.gridHeight * game.map.cellHeight; + private function drawBackground(engine:IEngine):Void { var g:Graphics = backgroundLayer.graphics; g.clear(); g.beginFill(0x000000); - g.drawRect(0, 0, mapWidth, mapHeight); + g.drawRect(0, 0, engine.map.width, engine.map.height); g.endFill(); - setContentSize(mapWidth, mapHeight); + setContentSize(engine.map.width, engine.map.height); } - public function draw(game:Engine):Void { + public function draw(game:IEngine):Void { for (brick in game.map.bricks) if (brick.config.index > 0) { if (!items.exists(brick.key)) { var item:RenderItem = switch(brick.config.type) { diff --git a/src/client/haxe/ru/m/tankz/sound/SoundManager.hx b/src/client/haxe/ru/m/tankz/sound/SoundManager.hx index f4aa115..e289d7f 100644 --- a/src/client/haxe/ru/m/tankz/sound/SoundManager.hx +++ b/src/client/haxe/ru/m/tankz/sound/SoundManager.hx @@ -5,8 +5,8 @@ import flash.media.Sound; import flash.media.SoundChannel; import flash.media.SoundTransform; import openfl.utils.Assets; -import ru.m.tankz.game.Game; import ru.m.tankz.game.GameEvent; +import ru.m.tankz.game.IGame; class SoundManager implements GameListener { private static var TAG(default, never):String = 'SoundManager'; diff --git a/src/client/haxe/ru/m/tankz/view/common/IGamePanel.hx b/src/client/haxe/ru/m/tankz/view/common/IGamePanel.hx index 4d71e53..d4eb4d1 100644 --- a/src/client/haxe/ru/m/tankz/view/common/IGamePanel.hx +++ b/src/client/haxe/ru/m/tankz/view/common/IGamePanel.hx @@ -1,7 +1,7 @@ package ru.m.tankz.view.common; import haxework.view.IView; -import ru.m.tankz.game.Game.GameListener; +import ru.m.tankz.game.IGame; interface IGamePanel extends IView extends GameListener { diff --git a/src/common/haxe/ru/m/tankz/engine/Engine.hx b/src/common/haxe/ru/m/tankz/engine/Engine.hx index 2345f3a..ef5268f 100755 --- a/src/common/haxe/ru/m/tankz/engine/Engine.hx +++ b/src/common/haxe/ru/m/tankz/engine/Engine.hx @@ -8,14 +8,11 @@ import ru.m.tankz.core.Entity; import ru.m.tankz.core.EntityType; import ru.m.tankz.core.MobileEntity; import ru.m.tankz.core.Tank; +import ru.m.tankz.engine.IEngine; import ru.m.tankz.map.Grid; import ru.m.tankz.map.LevelMap; -interface EngineListener { - public function onCollision(entity:EntityType, with:EntityType):Void; -} - -@:yield @:dispatcher(EngineListener) class Engine { +@:yield @:dispatcher(EngineListener) class Engine implements IEngine { public var config(default, default):Config; public var map(default, null):LevelMap; @@ -134,6 +131,7 @@ interface EngineListener { if (!isStop || Std.is(entity, Bullet)) { entity.rect.x += entity.mx * (d / 30); entity.rect.y += entity.my * (d / 30); + moveSignal.emit(entityType); } } } diff --git a/src/common/haxe/ru/m/tankz/engine/IEngine.hx b/src/common/haxe/ru/m/tankz/engine/IEngine.hx new file mode 100644 index 0000000..4718d92 --- /dev/null +++ b/src/common/haxe/ru/m/tankz/engine/IEngine.hx @@ -0,0 +1,36 @@ +package ru.m.tankz.engine; + +import haxework.signal.Signal; +import ru.m.tankz.config.Config; +import ru.m.tankz.core.Entity; +import ru.m.tankz.core.EntityType; +import ru.m.tankz.core.Tank; +import ru.m.tankz.map.LevelMap; + +interface IEngine { + public var entities(default, null):Map; + public var config(default, default):Config; + public var map(default, null):LevelMap; + + public var collisionSignal(default, null):Signal2; + public var moveSignal(default, null):Signal1; + + public function spawn(entity:Entity):Void; + + public function destroy(entity:Entity):Void; + + public function update():Void; + + public function iterTanks(filter:Tank->Bool):Iterator; + + public function connect(listener:EngineListener):Void; + + public function disconnect(listener:EngineListener):Void; + + public function dispose():Void; +} + +interface EngineListener { + public function onCollision(entity:EntityType, with:EntityType):Void; + public function onMove(entity:EntityType):Void; +} \ No newline at end of file diff --git a/src/common/haxe/ru/m/tankz/game/Game.hx b/src/common/haxe/ru/m/tankz/game/Game.hx index 4dc560d..9209cc3 100644 --- a/src/common/haxe/ru/m/tankz/game/Game.hx +++ b/src/common/haxe/ru/m/tankz/game/Game.hx @@ -4,13 +4,11 @@ import ru.m.tankz.bundle.IConfigBundle; import ru.m.tankz.config.Config; import ru.m.tankz.core.EntityType; import ru.m.tankz.engine.Engine; +import ru.m.tankz.engine.IEngine; import ru.m.tankz.game.GameState; +import ru.m.tankz.game.IGame; import ru.m.tankz.Type; -interface GameListener { - public function onGameEvent(event:GameEvent):Void; -} - @:dispatcher(GameListener) class Game implements IGame implements GameListener { private static var TAG(default, never):String = "Game"; @@ -18,7 +16,7 @@ interface GameListener { public var type(default, null):GameType; public var teams(default, null):Map; public var config(default, null):Config; - public var engine(default, null):Engine; + public var engine(default, null):IEngine; public var winner(default, null):Null; public var state(default, null):GameState; diff --git a/src/common/haxe/ru/m/tankz/game/GameRunner.hx b/src/common/haxe/ru/m/tankz/game/GameRunner.hx index 559fab1..e9221fe 100644 --- a/src/common/haxe/ru/m/tankz/game/GameRunner.hx +++ b/src/common/haxe/ru/m/tankz/game/GameRunner.hx @@ -13,8 +13,8 @@ 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.Engine; -import ru.m.tankz.game.Game; +import ru.m.tankz.engine.IEngine; +import ru.m.tankz.game.IGame; import ru.m.tankz.game.Spawner; import ru.m.tankz.Type; @@ -232,6 +232,10 @@ class GameRunner implements EngineListener implements GameListener { } } + public function onMove(entity:EntityType):Void { + + } + private function spawnBonus(?type:BonusType):Void { var bonusConfig:BonusConfig = type != null ? game.config.getBonus(type) : game.config.bonuses[Math.floor(Math.random() * game.config.bonuses.length)]; var bonus = new Bonus(bonusConfig); diff --git a/src/common/haxe/ru/m/tankz/game/IGame.hx b/src/common/haxe/ru/m/tankz/game/IGame.hx index c92f9ca..abbf7d6 100644 --- a/src/common/haxe/ru/m/tankz/game/IGame.hx +++ b/src/common/haxe/ru/m/tankz/game/IGame.hx @@ -2,17 +2,17 @@ package ru.m.tankz.game; import haxework.signal.Signal; import ru.m.tankz.config.Config; -import ru.m.tankz.engine.Engine; -import ru.m.tankz.game.Game; +import ru.m.tankz.engine.IEngine; import ru.m.tankz.Type; interface IGame { public var type(default, null):GameType; public var teams(default, null):Map; public var config(default, null):Config; - public var engine(default, null):Engine; + public var engine(default, null):IEngine; public var winner(default, null):Null; public var state(default, null):GameState; + public var gameEventSignal(default, null):Signal; public function connect(listener:GameListener):Void; @@ -25,3 +25,7 @@ interface IGame { public function getPlayer(playerId:PlayerId):Player; } + +interface GameListener { + public function onGameEvent(event:GameEvent):Void; +} diff --git a/src/common/haxe/ru/m/tankz/game/record/GameRecorder.hx b/src/common/haxe/ru/m/tankz/game/record/GameRecorder.hx index 2f3a860..2c76104 100644 --- a/src/common/haxe/ru/m/tankz/game/record/GameRecorder.hx +++ b/src/common/haxe/ru/m/tankz/game/record/GameRecorder.hx @@ -2,7 +2,7 @@ package ru.m.tankz.game.record; import flash.events.Event; import flash.Lib; -import ru.m.tankz.game.Game.GameListener; +import ru.m.tankz.game.IGame; class GameRecorder implements GameListener {