From b34cb41bff636eb07b55d1417ee55d8e73ea136d Mon Sep 17 00:00:00 2001 From: shmyga Date: Sun, 29 Sep 2019 12:23:58 +0300 Subject: [PATCH] [common] update game rework --- WORK.md | 2 ++ src/client/haxe/ru/m/tankz/local/LocalGame.hx | 20 +++++++++++++++- .../m/tankz/view/common/NetworkStateView.yaml | 1 + src/common/haxe/ru/m/tankz/engine/Ticker.hx | 3 +++ src/common/haxe/ru/m/tankz/game/Game.hx | 3 +++ src/common/haxe/ru/m/tankz/game/GameRunner.hx | 17 ++++---------- src/common/haxe/ru/m/tankz/game/IGame.hx | 2 ++ .../haxe/ru/m/tankz/server/game/ServerGame.hx | 23 +++++++++++++++++++ 8 files changed, 58 insertions(+), 13 deletions(-) diff --git a/WORK.md b/WORK.md index 01f5811..b62400f 100644 --- a/WORK.md +++ b/WORK.md @@ -20,3 +20,5 @@ * game frame layouts * fix: * ice brick fix + * shot delay + * boat in tank state diff --git a/src/client/haxe/ru/m/tankz/local/LocalGame.hx b/src/client/haxe/ru/m/tankz/local/LocalGame.hx index 66af688..f2d41f3 100644 --- a/src/client/haxe/ru/m/tankz/local/LocalGame.hx +++ b/src/client/haxe/ru/m/tankz/local/LocalGame.hx @@ -1,5 +1,7 @@ package ru.m.tankz.local; +import flash.events.Event; +import flash.Lib; import ru.m.tankz.control.HumanControl; import ru.m.tankz.game.GameEvent; import ru.m.tankz.game.GameRunner; @@ -11,15 +13,26 @@ class LocalGame extends GameRunner { @:provide static var gameStorage:GameStorage; + private var updateEvent:String; + public function new(start:Start) { super(start); controlFactory = new LocalControlFactory(); + updateEvent = Event.ENTER_FRAME; + } + + private function onUpdateEvent(_):Void { + update(); } override public function onGameEvent(event:GameEvent):Void { super.onGameEvent(event); switch event { - case COMPLETE(result): updateProgress(result); + case START(_): + Lib.current.stage.addEventListener(updateEvent, onUpdateEvent); + case COMPLETE(result): + Lib.current.stage.removeEventListener(updateEvent, onUpdateEvent); + updateProgress(result); case _: } } @@ -38,4 +51,9 @@ class LocalGame extends GameRunner { gameStorage.set(progress); } } + + override public function dispose():Void { + super.dispose(); + Lib.current.stage.removeEventListener(updateEvent, onUpdateEvent); + } } diff --git a/src/client/haxe/ru/m/tankz/view/common/NetworkStateView.yaml b/src/client/haxe/ru/m/tankz/view/common/NetworkStateView.yaml index 724ca27..e68c42a 100644 --- a/src/client/haxe/ru/m/tankz/view/common/NetworkStateView.yaml +++ b/src/client/haxe/ru/m/tankz/view/common/NetworkStateView.yaml @@ -1,6 +1,7 @@ --- geometry.padding: [5, 2] layout.vAlign: middle +visible: false skin: $type: haxework.view.skin.SpriteSkin border.color: 0x95937D diff --git a/src/common/haxe/ru/m/tankz/engine/Ticker.hx b/src/common/haxe/ru/m/tankz/engine/Ticker.hx index 1a25aea..9857a70 100644 --- a/src/common/haxe/ru/m/tankz/engine/Ticker.hx +++ b/src/common/haxe/ru/m/tankz/engine/Ticker.hx @@ -54,6 +54,9 @@ class Ticker implements ITicker { if (actions.length > 0) { runActions(); } + if (result > 45) { + L.w("Ticker", 'Long tick: ${result}'); + } return result; } diff --git a/src/common/haxe/ru/m/tankz/game/Game.hx b/src/common/haxe/ru/m/tankz/game/Game.hx index 3cf10c5..a846b91 100644 --- a/src/common/haxe/ru/m/tankz/game/Game.hx +++ b/src/common/haxe/ru/m/tankz/game/Game.hx @@ -130,6 +130,9 @@ import ru.m.tankz.Type; public function start():Void { } + public function update():Void { + } + public function dispose():Void { for (control in controls) { control.dispose(); diff --git a/src/common/haxe/ru/m/tankz/game/GameRunner.hx b/src/common/haxe/ru/m/tankz/game/GameRunner.hx index ba6f411..ebd25af 100644 --- a/src/common/haxe/ru/m/tankz/game/GameRunner.hx +++ b/src/common/haxe/ru/m/tankz/game/GameRunner.hx @@ -1,6 +1,5 @@ package ru.m.tankz.game; -import ru.m.tankz.core.Weapon; import ru.m.geom.Line; import ru.m.geom.Point; import ru.m.tankz.bonus.BonusFactory; @@ -10,18 +9,17 @@ import ru.m.tankz.core.Bullet; import ru.m.tankz.core.Eagle; import ru.m.tankz.core.EntityType; import ru.m.tankz.core.Tank; +import ru.m.tankz.core.Weapon; 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.Spawner; import ru.m.tankz.Type; -import ru.m.Timer; using ru.m.tankz.game.GameUtil; class GameRunner extends Game implements EngineListener { - private var timer:Timer; private var builder:EntityBuilder; private var bonuses:BonusFactory; @@ -46,7 +44,8 @@ class GameRunner extends Game implements EngineListener { } } - private function update():Void { + override public function update():Void { + super.update(); engine.update(); } @@ -140,9 +139,9 @@ class GameRunner extends Game implements EngineListener { } private function complete(winner:TeamId):Void { - Timer.delay(function() { + engine.ticker.emit(function() { gameEventSignal.emit(COMPLETE({state: state, level: level, winner: winner})); - }, 3000); + }, 3000, "complete"); } public function onSpawn(entity:EntityType):Void { @@ -276,14 +275,8 @@ class GameRunner extends Game implements EngineListener { switch event { case START(_): engine.ticker.start(); - timer = new Timer(30); - timer.run = update; case COMPLETE(_): engine.ticker.stop(); - if (timer != null) { - timer.stop(); - timer = null; - } case ACTION(tankId, SHOT(index)): var tank:Tank = cast engine.entities.get(tankId); var player = getPlayer(tank.playerId); diff --git a/src/common/haxe/ru/m/tankz/game/IGame.hx b/src/common/haxe/ru/m/tankz/game/IGame.hx index 5708757..3e9f076 100644 --- a/src/common/haxe/ru/m/tankz/game/IGame.hx +++ b/src/common/haxe/ru/m/tankz/game/IGame.hx @@ -32,6 +32,8 @@ interface IGame extends GameListener { public function getPlayer(playerId:PlayerId):Player; public function start():Void; + + public function update():Void; } interface GameListener { diff --git a/src/server/haxe/ru/m/tankz/server/game/ServerGame.hx b/src/server/haxe/ru/m/tankz/server/game/ServerGame.hx index 25a4b9e..e032145 100644 --- a/src/server/haxe/ru/m/tankz/server/game/ServerGame.hx +++ b/src/server/haxe/ru/m/tankz/server/game/ServerGame.hx @@ -21,6 +21,8 @@ class ServerGame extends GameRunner { public var room(default, null):RoomProto; public var id(get, null):Int; + private var timer:Timer; + @:provide static var levelBundle:ILevelBundle; public function new(room:RoomProto) { @@ -36,6 +38,19 @@ class ServerGame extends GameRunner { return room.game.id; } + override public function onGameEvent(event:GameEvent):Void { + switch event { + case START(_): + timer = new Timer(30); + timer.run = update; + case COMPLETE(_): + if (timer != null) { + timer.stop(); + timer = null; + } + } + } + public function contains(user:UserProto):Bool { for (slot in room.slots) { if (slot.hasUser() && slot.user.uuid == user.uuid) { @@ -88,6 +103,14 @@ class ServerGame extends GameRunner { super.start(); } + override public function dispose():Void { + super.dispose(); + if (timer != null) { + timer.stop(); + timer = null; + } + } + public function restore():Array { var result = []; result.push(EventUtil.buildBricksSpawn(engine.map));