[common] build bonus with EntityBuilder

This commit is contained in:
2019-05-06 20:40:43 +03:00
parent 20b60009e3
commit a7f286dc42
8 changed files with 35 additions and 36 deletions

View File

@@ -115,6 +115,11 @@ class Render extends SpriteView implements GameListener implements EngineListene
var item = new BulletItem(bullet); var item = new BulletItem(bullet);
items.set(bullet.key, item); items.set(bullet.key, item);
entryLayer.addChild(item.view); entryLayer.addChild(item.view);
case BONUS(bonus):
var item = new BonusItem(bonus);
items.set(bonus.key, item);
upperLayer.addChild(item.view);
item.update();
case _: case _:
} }
} }
@@ -127,27 +132,6 @@ class Render extends SpriteView implements GameListener implements EngineListene
public function onGameEvent(event:GameEvent):Void { public function onGameEvent(event:GameEvent):Void {
switch event { 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)): case DESTROY(TANK(tank, who, wherewith, score)):
if (items.exists(tank.key)) { if (items.exists(tank.key)) {
entryLayer.removeChild(items.get(tank.key).view); entryLayer.removeChild(items.get(tank.key).view);

View File

@@ -82,7 +82,7 @@ class SoundManager implements GameListener {
if (false /* ToDo: human tank */) { if (false /* ToDo: human tank */) {
play('shot'); play('shot');
} }
case SPAWN(BONUS(bonus)): case SPAWN(BONUS(_, _)):
play('bonus_add'); play('bonus_add');
case HIT(TANK(tank, who, wherewith)): case HIT(TANK(tank, who, wherewith)):
play('bullet_hit'); play('bullet_hit');

View File

@@ -25,7 +25,7 @@ import ru.m.tankz.map.LevelMap;
public function new(config:Config) { public function new(config:Config) {
this.config = config; this.config = config;
map = new LevelMap(config.map); map = new LevelMap(config.map);
entities = new Map<Int, Entity>(); entities = new Map();
time = Date.now().getTime(); time = Date.now().getTime();
} }
@@ -62,10 +62,10 @@ import ru.m.tankz.map.LevelMap;
var entity:MobileEntity = cast ent; var entity:MobileEntity = cast ent;
/*if (Std.is(entity, Tank)) { /*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; 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; 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 { public function dispose():Void {
entities = new Map(); entities = null;
map = null;
spawnSignal.dispose();
collisionSignal.dispose();
moveSignal.dispose();
} }
public function iterTanks(filter:Tank->Bool):Iterator<Tank> { public function iterTanks(filter:Tank->Bool):Iterator<Tank> {

View File

@@ -1,7 +1,8 @@
package ru.m.tankz.game; package ru.m.tankz.game;
import ru.m.tankz.core.Bullet;
import ru.m.tankz.config.Config; 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.Eagle;
import ru.m.tankz.core.Tank; import ru.m.tankz.core.Tank;
import ru.m.tankz.Type; import ru.m.tankz.Type;
@@ -35,5 +36,11 @@ class EntityBuilder {
var bullet = new Bullet(playerId, tankConfig.bullet); var bullet = new Bullet(playerId, tankConfig.bullet);
return bullet; return bullet;
} }
public function buildBonus(type:BonusType):Bonus {
var bonusConfig = config.getBonus(type);
var bonus = new Bonus(bonusConfig);
return bonus;
}
} }

View File

@@ -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.rect.center = rect.center.add(new Point(rect.width / 4 * rect.direction.x, rect.height / 4 * rect.direction.y));
bullet.move(rect.direction); bullet.move(rect.direction);
engine.spawn(bullet); 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); engine.spawn(bonus);
case GameEvent.DESTROY(TANK(tank, who, wherewith, score)): case GameEvent.DESTROY(TANK(tank, who, wherewith, score)):
engine.destroy(tank.id); engine.destroy(tank.id);

View File

@@ -13,7 +13,7 @@ enum SpawnEvent {
EAGLE(teamId:TeamId, point:SpawnPoint); EAGLE(teamId:TeamId, point:SpawnPoint);
TANK(playerId:PlayerId, type:TankType, point:SpawnPoint); TANK(playerId:PlayerId, type:TankType, point:SpawnPoint);
BULLET(playerId:PlayerId); BULLET(playerId:PlayerId);
BONUS(bonus:Bonus); BONUS(type:BonusType, point:{x:Int, y:Int});
} }
enum HitEvent { enum HitEvent {

View File

@@ -193,12 +193,13 @@ class GameRunner implements EngineListener implements GameListener {
} }
private function spawnBonus(?type:BonusType):Void { private function spawnBonus():Void {
var bonusConfig:BonusConfig = type != null ? game.config.getBonus(type) : game.config.bonuses[Math.floor(Math.random() * game.config.bonuses.length)]; var type = game.config.bonuses[Math.floor(Math.random() * game.config.bonuses.length)].type;
var bonus = new Bonus(bonusConfig); var point = {
bonus.rect.x = Math.round(Math.random() * game.engine.map.width / game.engine.map.cellWidth) * game.engine.map.cellWidth; x: Math.floor(Math.random() * (game.engine.map.gridWidth - 1)),
bonus.rect.y = Math.round(Math.random() * game.engine.map.height/ game.engine.map.cellHeight) * game.engine.map.cellHeight; y: Math.floor(Math.random() * (game.engine.map.gridHeight - 1)),
gameEventSignal.emit(GameEvent.SPAWN(BONUS(bonus))); }
gameEventSignal.emit(GameEvent.SPAWN(BONUS(type, point)));
} }
private inline function alienTank(team:TeamId):Tank->Bool { private inline function alienTank(team:TeamId):Tank->Bool {

View File

@@ -163,7 +163,7 @@ presets:
- {<<: *team_human} - {<<: *team_human}
- id: bot - id: bot
spawnInterval: 3000 spawnInterval: 3000
life: 1 life: 10
players: players:
- {<<: *bot, index: 0, control: bot-stupid} - {<<: *bot, index: 0, control: bot-stupid}
- {<<: *bot, index: 1, control: bot-stupid} - {<<: *bot, index: 1, control: bot-stupid}