From 0e809812e796be51898c81d8722ec0beb8f02cba Mon Sep 17 00:00:00 2001 From: shmyga Date: Tue, 23 Jan 2018 14:00:50 +0300 Subject: [PATCH] [common] Render implement EngineListener --- src/client/haxe/ru/m/tankz/render/Render.hx | 57 +++++++++++++------ .../haxe/ru/m/tankz/view/frames/GameFrame.hx | 13 ++++- .../haxe/ru/m/tankz/view/frames/StartFrame.hx | 25 ++++---- .../config/{config.yaml => classic.yaml} | 2 +- src/common/haxe/ru/m/tankz/config/Config.hx | 4 +- .../haxe/ru/m/tankz/config/ConfigBundle.hx | 4 +- src/common/haxe/ru/m/tankz/engine/Engine.hx | 12 +++- .../haxe/ru/m/tankz/game/ClassicGame.hx | 2 +- src/common/haxe/ru/m/tankz/game/Game.hx | 4 +- 9 files changed, 79 insertions(+), 44 deletions(-) rename src/client/resources/config/{config.yaml => classic.yaml} (98%) diff --git a/src/client/haxe/ru/m/tankz/render/Render.hx b/src/client/haxe/ru/m/tankz/render/Render.hx index f1f72b6..93122e0 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.tankz.core.EntityType; import flash.display.DisplayObject; import Type.ValueType; import ru.m.tankz.render.RenderItem; @@ -11,7 +12,7 @@ import flash.display.Graphics; import haxework.gui.SpriteView; -class Render extends SpriteView { +class Render extends SpriteView implements EngineListener { private var backgroundLayer:Sprite; private var groundLayer:Sprite; @@ -59,23 +60,6 @@ class Render extends SpriteView { } } } - for (entity in game.entities) { - if (!items.exists(entity.key)) { - items[entity.key] = switch (Type.typeof(entity)) { - case ValueType.TClass(Tank): cast new TankItem(cast entity); - case ValueType.TClass(Bullet): cast new BulletItem(cast entity); - case x: null; - } - entryLayer.addChild(items[entity.key].view); - } - } - /*for (key in game.removedEntities) { - if (items.exists(key)) { - var view:DisplayObject = items[key].view; - view.parent.removeChild(view); - items.remove(key); - } - }*/ for (item in items) { item.update(); } @@ -91,4 +75,41 @@ class Render extends SpriteView { background = null; } } + + + public function onSpawn(entity:EntityType):Void { + switch(entity) { + case EntityType.TANK(tank): + var item = new TankItem(tank); + items.set(tank.key, item); + entryLayer.addChild(item.view); + item.update(); + case EntityType.BULLET(bullet): + var item = new BulletItem(bullet); + items.set(bullet.key, item); + entryLayer.addChild(item.view); + item.update(); + case _: + } + } + + public function onCollision(entity:EntityType, with:EntityType):Void { + + } + + public function onDestroy(entity:EntityType):Void { + switch(entity) { + case EntityType.TANK(tank): + if (items.exists(tank.key)) { + entryLayer.removeChild(items.get(tank.key).view); + items.remove(tank.key); + } + case EntityType.BULLET(bullet): + if (items.exists(bullet.key)) { + entryLayer.removeChild(items.get(bullet.key).view); + items.remove(bullet.key); + } + case _: + } + } } diff --git a/src/client/haxe/ru/m/tankz/view/frames/GameFrame.hx b/src/client/haxe/ru/m/tankz/view/frames/GameFrame.hx index 7f5415f..cc7084e 100755 --- a/src/client/haxe/ru/m/tankz/view/frames/GameFrame.hx +++ b/src/client/haxe/ru/m/tankz/view/frames/GameFrame.hx @@ -1,5 +1,6 @@ package ru.m.tankz.view.frames; +import ru.m.tankz.config.Config; import ru.m.tankz.control.PlayerControl; import flash.events.Event; import haxe.Timer; @@ -28,10 +29,16 @@ class GameFrame extends VGroupView implements ViewBuilder implements IPacketHand } public function onShow():Void { - game = new ClassicGame(ConfigBundle.get(ClassicGame.TYPE, 0)); + var config:Config = Provider.get(Config); + game = switch (config.type) { + case ClassicGame.TYPE: new ClassicGame(config); + case x: throw 'Unsupported game type "${x}"'; + } + game.engine.listeners.push(render); game.start(); - game.setControl({team:'human', index:0}, PlayerControl.forPlayer(0)); - game.setControl({team:'human', index:1}, PlayerControl.forPlayer(1)); + for (index in 0...game.config.getTeam('human').size) { + game.setControl({team:'human', index:index}, PlayerControl.forPlayer(index)); + } content.addEventListener(Event.ENTER_FRAME, redraw); Provider.get(IConnection).packetHandler.addListener(this); render.draw(game.engine); diff --git a/src/client/haxe/ru/m/tankz/view/frames/StartFrame.hx b/src/client/haxe/ru/m/tankz/view/frames/StartFrame.hx index ebccba6..51486b9 100644 --- a/src/client/haxe/ru/m/tankz/view/frames/StartFrame.hx +++ b/src/client/haxe/ru/m/tankz/view/frames/StartFrame.hx @@ -1,8 +1,8 @@ package ru.m.tankz.view.frames; -import ru.m.tankz.proto.core.GameType; -import ru.m.tankz.proto.core.Player; -import ru.m.tankz.proto.core.Game; +import ru.m.tankz.config.Config; +import ru.m.tankz.game.ClassicGame; +import ru.m.tankz.config.ConfigBundle; import haxework.gui.frame.IFrameSwitcher; import haxework.provider.Provider; import haxework.gui.ButtonView; @@ -21,23 +21,18 @@ class StartFrame extends VGroupView implements ViewBuilder { public function onPress(view:ButtonView):Void { switch (view.id) { - case "start_1p": + case 'start_1p': startGame(1); - case "start_2p": + case 'start_2p': startGame(2); } } - private function startGame(playersCount:Int):Void { - var game = new Game(); - game.type = GameType.CLASSIC; - for (i in 0...playersCount) { - var player = new Player(); - player.id = i; - game.players.push(player); - } - game.id = 1; - Provider.set(Game, game); + private function startGame(players:Int):Void { + var config = ConfigBundle.get(ClassicGame.TYPE, 0); + config.getTeam('human').size = players; + config.getTeam('bot').size = 2 + 2 * players; + Provider.set(Config, config); Provider.get(IFrameSwitcher).change(GameFrame.ID); } } diff --git a/src/client/resources/config/config.yaml b/src/client/resources/config/classic.yaml similarity index 98% rename from src/client/resources/config/config.yaml rename to src/client/resources/config/classic.yaml index ab91b0a..5d3048f 100644 --- a/src/client/resources/config/config.yaml +++ b/src/client/resources/config/classic.yaml @@ -57,7 +57,7 @@ teams: direction: top - id: bot size: 6 - spawnInterval: 1000 + spawnInterval: 3000 points: - type: tank index: -1 diff --git a/src/common/haxe/ru/m/tankz/config/Config.hx b/src/common/haxe/ru/m/tankz/config/Config.hx index 72894dd..d0b1a2d 100644 --- a/src/common/haxe/ru/m/tankz/config/Config.hx +++ b/src/common/haxe/ru/m/tankz/config/Config.hx @@ -51,6 +51,7 @@ typedef TeamConfig = { class Config { + public var type(default, null):String; public var map(default, null):MapConfig; public var bricks(default, null):Array; public var tanks(default, null):Array; @@ -60,7 +61,8 @@ class Config { private var tankMap:Map>; private var teamMap:Map; - public function new(map:MapConfig, bricks:Array, teams:Array, tanks:Array) { + public function new(type:String, map:MapConfig, bricks:Array, teams:Array, tanks:Array) { + this.type = type; this.map = map; this.bricks = bricks; this.teams = teams; diff --git a/src/common/haxe/ru/m/tankz/config/ConfigBundle.hx b/src/common/haxe/ru/m/tankz/config/ConfigBundle.hx index d8e8b3e..64827d3 100644 --- a/src/common/haxe/ru/m/tankz/config/ConfigBundle.hx +++ b/src/common/haxe/ru/m/tankz/config/ConfigBundle.hx @@ -23,7 +23,7 @@ class ConfigBundle { public static function get(type:String, level:Int):Config { switch (type) { case ClassicGame.TYPE: - var source:ConfigSource = convert(Yaml.parse(Assets.getText('resources/config/config.yaml'), Parser.options().useObjects())); + var source:ConfigSource = convert(Yaml.parse(Assets.getText('resources/config/${type}.yaml'), Parser.options().useObjects())); var bricksData:String = Assets.getText('resources/levels/level00${level}.txt'); var bricks:Array = []; for (line in ~/\s+/g.split(bricksData)) { @@ -42,7 +42,7 @@ class ConfigBundle { tanks.push(item); } } - return new Config(source.map, source.bricks, source.teams, tanks); + return new Config(type, source.map, source.bricks, source.teams, tanks); case _: return null; } diff --git a/src/common/haxe/ru/m/tankz/engine/Engine.hx b/src/common/haxe/ru/m/tankz/engine/Engine.hx index 1a36dc3..db46851 100755 --- a/src/common/haxe/ru/m/tankz/engine/Engine.hx +++ b/src/common/haxe/ru/m/tankz/engine/Engine.hx @@ -104,6 +104,12 @@ class Engine { public function destroy(entity:Entity):Void { if (entities.exists(entity.id)) { var type = EntityTypeResolver.of(entity); + switch (type) { + case EntityType.BULLET(bullet): + var tank:Tank = cast entities.get(bullet.tankId); + if (tank != null) tank.onDestroyBullet(); + case _: + } for (l in listeners) l.onDestroy(type); entities.remove(entity.id); } @@ -122,7 +128,7 @@ class Engine { case TankAction.SHOT: var bullet = tank.shot(); if (bullet != null) { - entities.set(bullet.id, bullet); + spawn(bullet); } } } @@ -192,4 +198,8 @@ class Engine { } } + public function dispose():Void { + listeners = []; + entities = new Map(); + } } diff --git a/src/common/haxe/ru/m/tankz/game/ClassicGame.hx b/src/common/haxe/ru/m/tankz/game/ClassicGame.hx index 4156886..9a13d7c 100644 --- a/src/common/haxe/ru/m/tankz/game/ClassicGame.hx +++ b/src/common/haxe/ru/m/tankz/game/ClassicGame.hx @@ -7,7 +7,7 @@ import ru.m.tankz.config.Config; class ClassicGame extends Game { - public static var TYPE(default, never):GameType = Type.getClassName(ClassicGame); + public static var TYPE(default, never):GameType = 'classic'; public function new(config:Config) { super(TYPE, config); diff --git a/src/common/haxe/ru/m/tankz/game/Game.hx b/src/common/haxe/ru/m/tankz/game/Game.hx index 0291df4..06ea91f 100644 --- a/src/common/haxe/ru/m/tankz/game/Game.hx +++ b/src/common/haxe/ru/m/tankz/game/Game.hx @@ -138,8 +138,8 @@ class Game implements EngineListener implements ControlListener { private function spawn(task:SpawnTask):Void { var tank = buildTank(task.playerId, config.getTank(task.playerId.team, '0'), task.point); var player:Player = teams.get(task.playerId.team).players[task.playerId.index]; - player.tankId = tank.id; engine.spawn(tank); + player.tankId = tank.id; } public function setControl(playerId:PlayerId, control:Control):Void { @@ -185,6 +185,6 @@ class Game implements EngineListener implements ControlListener { public function dispose():Void { - + engine.dispose(); } }