From a7f286dc42d9f48ece926129ae5b1f69fd36ec53 Mon Sep 17 00:00:00 2001 From: shmyga Date: Mon, 6 May 2019 20:40:43 +0300 Subject: [PATCH] [common] build bonus with EntityBuilder --- src/client/haxe/ru/m/tankz/render/Render.hx | 26 ++++--------------- .../haxe/ru/m/tankz/sound/SoundManager.hx | 2 +- src/common/haxe/ru/m/tankz/engine/Engine.hx | 12 ++++++--- .../haxe/ru/m/tankz/game/EntityBuilder.hx | 9 ++++++- src/common/haxe/ru/m/tankz/game/Game.hx | 5 +++- src/common/haxe/ru/m/tankz/game/GameEvent.hx | 2 +- src/common/haxe/ru/m/tankz/game/GameRunner.hx | 13 +++++----- src/common/resources/classic/config.yaml | 2 +- 8 files changed, 35 insertions(+), 36 deletions(-) diff --git a/src/client/haxe/ru/m/tankz/render/Render.hx b/src/client/haxe/ru/m/tankz/render/Render.hx index 29e6ce9..770024f 100755 --- a/src/client/haxe/ru/m/tankz/render/Render.hx +++ b/src/client/haxe/ru/m/tankz/render/Render.hx @@ -115,6 +115,11 @@ class Render extends SpriteView implements GameListener implements EngineListene var item = new BulletItem(bullet); items.set(bullet.key, item); entryLayer.addChild(item.view); + case BONUS(bonus): + var item = new BonusItem(bonus); + items.set(bonus.key, item); + upperLayer.addChild(item.view); + item.update(); case _: } } @@ -127,27 +132,6 @@ class Render extends SpriteView implements GameListener implements EngineListene public function onGameEvent(event:GameEvent):Void { switch event { - /*case SPAWN(TANK(tank)): - var item = new TankItem(tank); - items.set(tank.key, item); - entryLayer.addChild(item.view); - item.update(); - playAnimate(tank.rect.center, AnimateBundle.tankSpawn());*/ - /*case SPAWN(BULLET(bullet)): - var item = new BulletItem(bullet); - items.set(bullet.key, item); - entryLayer.addChild(item.view); - item.update();*/ - /*case SPAWN(EAGLE(eagle)): - var item = new EagleItem(eagle); - items.set(eagle.key, item); - entryLayer.addChild(item.view); - item.update();*/ - case SPAWN(BONUS(bonus)): - var item = new BonusItem(bonus); - items.set(bonus.key, item); - upperLayer.addChild(item.view); - item.update(); case DESTROY(TANK(tank, who, wherewith, score)): if (items.exists(tank.key)) { entryLayer.removeChild(items.get(tank.key).view); diff --git a/src/client/haxe/ru/m/tankz/sound/SoundManager.hx b/src/client/haxe/ru/m/tankz/sound/SoundManager.hx index 1dcd96d..1644bd6 100644 --- a/src/client/haxe/ru/m/tankz/sound/SoundManager.hx +++ b/src/client/haxe/ru/m/tankz/sound/SoundManager.hx @@ -82,7 +82,7 @@ class SoundManager implements GameListener { if (false /* ToDo: human tank */) { play('shot'); } - case SPAWN(BONUS(bonus)): + case SPAWN(BONUS(_, _)): play('bonus_add'); case HIT(TANK(tank, who, wherewith)): play('bullet_hit'); diff --git a/src/common/haxe/ru/m/tankz/engine/Engine.hx b/src/common/haxe/ru/m/tankz/engine/Engine.hx index 46b7a40..66d96cf 100755 --- a/src/common/haxe/ru/m/tankz/engine/Engine.hx +++ b/src/common/haxe/ru/m/tankz/engine/Engine.hx @@ -25,7 +25,7 @@ import ru.m.tankz.map.LevelMap; public function new(config:Config) { this.config = config; map = new LevelMap(config.map); - entities = new Map(); + entities = new Map(); time = Date.now().getTime(); } @@ -62,10 +62,10 @@ import ru.m.tankz.map.LevelMap; var entity:MobileEntity = cast ent; /*if (Std.is(entity, Tank)) { - if (entity.direction.x != 0) { + if (entity.rect.direction.x != 0) { entity.rect.y = Math.round((entity.rect.y + entity.rect.height / 2) / map.cellHeight) * map.cellHeight - entity.rect.height / 2; } - if (entity.direction.y != 0) { + if (entity.rect.direction.y != 0) { entity.rect.x = Math.round((entity.rect.x + entity.rect.width / 2) / map.cellWidth) * map.cellWidth - entity.rect.width / 2; } }*/ @@ -152,7 +152,11 @@ import ru.m.tankz.map.LevelMap; } public function dispose():Void { - entities = new Map(); + entities = null; + map = null; + spawnSignal.dispose(); + collisionSignal.dispose(); + moveSignal.dispose(); } public function iterTanks(filter:Tank->Bool):Iterator { diff --git a/src/common/haxe/ru/m/tankz/game/EntityBuilder.hx b/src/common/haxe/ru/m/tankz/game/EntityBuilder.hx index 2cc2972..df7e998 100644 --- a/src/common/haxe/ru/m/tankz/game/EntityBuilder.hx +++ b/src/common/haxe/ru/m/tankz/game/EntityBuilder.hx @@ -1,7 +1,8 @@ package ru.m.tankz.game; -import ru.m.tankz.core.Bullet; import ru.m.tankz.config.Config; +import ru.m.tankz.core.Bonus; +import ru.m.tankz.core.Bullet; import ru.m.tankz.core.Eagle; import ru.m.tankz.core.Tank; import ru.m.tankz.Type; @@ -35,5 +36,11 @@ class EntityBuilder { var bullet = new Bullet(playerId, tankConfig.bullet); return bullet; } + + public function buildBonus(type:BonusType):Bonus { + var bonusConfig = config.getBonus(type); + var bonus = new Bonus(bonusConfig); + return bonus; + } } diff --git a/src/common/haxe/ru/m/tankz/game/Game.hx b/src/common/haxe/ru/m/tankz/game/Game.hx index 15e3b04..87cec4a 100644 --- a/src/common/haxe/ru/m/tankz/game/Game.hx +++ b/src/common/haxe/ru/m/tankz/game/Game.hx @@ -86,7 +86,10 @@ import ru.m.tankz.Type; bullet.rect.center = rect.center.add(new Point(rect.width / 4 * rect.direction.x, rect.height / 4 * rect.direction.y)); bullet.move(rect.direction); engine.spawn(bullet); - case GameEvent.SPAWN(BONUS(bonus)): + case GameEvent.SPAWN(BONUS(type, point)): + var bonus = builder.buildBonus(type); + bonus.rect.x = point.x * config.map.cellWidth; + bonus.rect.y = point.y * config.map.cellHeight; engine.spawn(bonus); case GameEvent.DESTROY(TANK(tank, who, wherewith, score)): engine.destroy(tank.id); diff --git a/src/common/haxe/ru/m/tankz/game/GameEvent.hx b/src/common/haxe/ru/m/tankz/game/GameEvent.hx index d2e0bcb..9a7c26f 100644 --- a/src/common/haxe/ru/m/tankz/game/GameEvent.hx +++ b/src/common/haxe/ru/m/tankz/game/GameEvent.hx @@ -13,7 +13,7 @@ enum SpawnEvent { EAGLE(teamId:TeamId, point:SpawnPoint); TANK(playerId:PlayerId, type:TankType, point:SpawnPoint); BULLET(playerId:PlayerId); - BONUS(bonus:Bonus); + BONUS(type:BonusType, point:{x:Int, y:Int}); } enum HitEvent { diff --git a/src/common/haxe/ru/m/tankz/game/GameRunner.hx b/src/common/haxe/ru/m/tankz/game/GameRunner.hx index 6f609c4..7278604 100644 --- a/src/common/haxe/ru/m/tankz/game/GameRunner.hx +++ b/src/common/haxe/ru/m/tankz/game/GameRunner.hx @@ -193,12 +193,13 @@ class GameRunner implements EngineListener implements GameListener { } - 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); - bonus.rect.x = Math.round(Math.random() * game.engine.map.width / game.engine.map.cellWidth) * game.engine.map.cellWidth; - bonus.rect.y = Math.round(Math.random() * game.engine.map.height/ game.engine.map.cellHeight) * game.engine.map.cellHeight; - gameEventSignal.emit(GameEvent.SPAWN(BONUS(bonus))); + private function spawnBonus():Void { + var type = game.config.bonuses[Math.floor(Math.random() * game.config.bonuses.length)].type; + var point = { + x: Math.floor(Math.random() * (game.engine.map.gridWidth - 1)), + y: Math.floor(Math.random() * (game.engine.map.gridHeight - 1)), + } + gameEventSignal.emit(GameEvent.SPAWN(BONUS(type, point))); } private inline function alienTank(team:TeamId):Tank->Bool { diff --git a/src/common/resources/classic/config.yaml b/src/common/resources/classic/config.yaml index 14a65ec..17d93dc 100644 --- a/src/common/resources/classic/config.yaml +++ b/src/common/resources/classic/config.yaml @@ -163,7 +163,7 @@ presets: - {<<: *team_human} - id: bot spawnInterval: 3000 - life: 1 + life: 10 players: - {<<: *bot, index: 0, control: bot-stupid} - {<<: *bot, index: 1, control: bot-stupid}