[editor] refactor MapEditView

This commit is contained in:
2019-05-20 13:00:04 +03:00
parent 09b188cf24
commit bbe2aeb7fb
11 changed files with 197 additions and 255 deletions

View File

@@ -19,12 +19,14 @@ typedef EntityPoint = {
class EntityBuilder {
private var config:Config;
private var entityId:Int;
public function new(config:Config) {
this.config = config;
this.entityId = 0;
}
private function buildRect(point:EntityPoint, width:Float, height:Float):Rectangle {
public function buildRect(point:EntityPoint, width:Float, height:Float):Rectangle {
return new Rectangle(
(point.x + 1) * config.map.cellWidth - width / 2,
(point.y + 1) * config.map.cellHeight - height / 2,
@@ -34,35 +36,37 @@ class EntityBuilder {
);
}
public function buildEagle(id:Int, point:EntityPoint, teamId:TeamId):Eagle {
public function buildEagle(point:EntityPoint, teamId:TeamId):Eagle {
var eageleConfig = config.getTeam(teamId).eagle;
var eagle = new Eagle(id, buildRect(point, config.map.cellWidth * 2, config.map.cellHeight * 2), teamId, eageleConfig);
var eagle = new Eagle(++entityId, buildRect(point, config.map.cellWidth * 2, config.map.cellHeight * 2), teamId, eageleConfig);
eagle.color = config.getColor(new PlayerId(teamId, -1));
return eagle;
}
public function buildTank(id:Int, point:EntityPoint, playerId:PlayerId, type:TankType):Tank {
public function buildTank(point:EntityPoint, playerId:PlayerId, type:TankType, bonusOff:Bool = false):Tank {
var playerConfig = config.getPlayer(playerId);
var tankConfig = config.getTank(type);
var tank = new Tank(id, buildRect(point, tankConfig.width, tankConfig.height), playerId, tankConfig);
var tank = new Tank(++entityId, buildRect(point, tankConfig.width, tankConfig.height), playerId, tankConfig);
tank.color = config.getColor(playerId);
tank.bonus = Math.random() < playerConfig.bonus;
if (!bonusOff) {
tank.bonus = Math.random() < playerConfig.bonus;
}
if (playerConfig.protect > 0) {
tank.protect.on(playerConfig.protect);
}
return tank;
}
public function buildBullet(id:Int, point:Point, direction:Direction, playerId:PlayerId, type:TankType):Bullet {
public function buildBullet(point:Point, direction:Direction, playerId:PlayerId, type:TankType):Bullet {
var tankConfig = config.getTank(type);
var bulletConfig = tankConfig.bullet;
var bullet = new Bullet(id, new Rectangle(point.x - bulletConfig.width / 2, point.y - bulletConfig.height / 2, bulletConfig.width, bulletConfig.height, direction), playerId, bulletConfig);
var bullet = new Bullet(++entityId, new Rectangle(point.x - bulletConfig.width / 2, point.y - bulletConfig.height / 2, bulletConfig.width, bulletConfig.height, direction), playerId, bulletConfig);
return bullet;
}
public function buildBonus(id:Int, point:EntityPoint, type:BonusType):Bonus {
public function buildBonus(point:EntityPoint, type:BonusType):Bonus {
var bonusConfig = config.getBonus(type);
var bonus = new Bonus(id, buildRect(point, config.map.cellWidth * 2, config.map.cellHeight * 2), bonusConfig);
var bonus = new Bonus(++entityId, buildRect(point, config.map.cellWidth * 2, config.map.cellHeight * 2), bonusConfig);
return bonus;
}
}

View File

@@ -0,0 +1,50 @@
package ru.m.tankz.game;
import ru.m.tankz.core.EntityType.EntityTypeResolver;
import ru.m.tankz.core.Entity;
import ru.m.tankz.core.Eagle;
import ru.m.tankz.core.Tank;
import ru.m.tankz.map.Brick;
import ru.m.tankz.game.GameEvent;
import ru.m.tankz.map.LevelMap;
class EventUtil {
public static function buildBricksSpawn(map:LevelMap):GameEvent {
var bricks = map.bricks.map(function(item:Brick):BrickInfo {
return {
id: item.id,
x: item.cellY,
y: item.cellY,
rect: item.rect,
type: item.config.type,
}
});
return GameEvent.SPAWN(BRICK(bricks));
}
public static function buildEagleSpawn(eagle:Eagle):GameEvent {
return GameEvent.SPAWN(EAGLE(eagle.id, eagle.rect, eagle.team));
}
public static function buildTankSpawn(tank:Tank):GameEvent {
return GameEvent.SPAWN(TANK(tank.id, tank.rect.clone(), tank.playerId, {
type:tank.config.type,
hits:tank.hits,
bonus:tank.bonus
}));
}
public static function buildMove(entity:Entity):GameEvent {
return switch EntityTypeResolver.of(entity) {
case EAGLE(eagle):
GameEvent.MOVE(EAGLE(entity.id, entity.rect.position));
case TANK(tank):
GameEvent.MOVE(TANK(entity.id, entity.rect.position));
case BULLET(bullet):
GameEvent.MOVE(BULLET(entity.id, entity.rect.position));
case _:
null;
}
}
}

View File

@@ -48,6 +48,7 @@ enum DestroyEvent {
}
enum MoveEvent {
EAGLE(id:Int, position:Position);
TANK(id:Int, position:Position);
BULLET(id:Int, position:Position);
}
@@ -65,6 +66,7 @@ enum ChangeEvent {
PLAYER_LIFE(playerId:PlayerId, value:Int);
TEAM_SCORE(teamId:TeamId, value:Int);
TEAM_LIFE(teamId:TeamId, value:Int);
BRICK(id:Int, type:BrickType);
}
enum GameEvent {

View File

@@ -3,10 +3,8 @@ package ru.m.tankz.game;
import haxe.ds.Option;
import haxe.Timer;
import haxework.signal.Signal;
import ru.m.geom.Direction;
import ru.m.geom.Line;
import ru.m.geom.Point;
import ru.m.geom.Position;
import ru.m.tankz.control.Control;
import ru.m.tankz.control.Controller;
import ru.m.tankz.control.IControlFactory;
@@ -19,7 +17,6 @@ import ru.m.tankz.engine.IEngine;
import ru.m.tankz.game.GameEvent;
import ru.m.tankz.game.IGame;
import ru.m.tankz.game.Spawner;
import ru.m.tankz.map.Brick;
import ru.m.tankz.Type;
class GameRunner implements EngineListener implements GameListener {
@@ -27,7 +24,6 @@ class GameRunner implements EngineListener implements GameListener {
private var game(default, null):IGame;
private var gameEventSignal(get, null):Signal<GameEvent>;
private var entityId:Int;
private var timer:Timer;
private var builder:EntityBuilder;
@@ -36,7 +32,6 @@ class GameRunner implements EngineListener implements GameListener {
this.builder = new EntityBuilder(this.game.config);
this.game.connect(this);
this.game.engine.connect(this);
this.entityId = 0;
}
private inline function get_gameEventSignal():Signal<GameEvent> {
@@ -56,14 +51,6 @@ class GameRunner implements EngineListener implements GameListener {
game.engine.disconnect(this);
}
private function pointToPosition(point:{x:Int, y:Int, direction:String}):Position {
return {
x: (point.x + 1) * game.config.map.cellWidth,
y: (point.y + 1) * game.config.map.cellHeight,
direction: Direction.fromString(point.direction),
}
}
public function start(state:GameState):Void {
for (team in game.teams.iterator()) {
for (player in team.players.iterator()) {
@@ -80,23 +67,14 @@ class GameRunner implements EngineListener implements GameListener {
}
}
if (team.config.eagle != null) {
var point = game.config.getPoint(team.id, "eagle");
var eagle = builder.buildEagle(++entityId, point, team.id);
var point = team.spawner.getPoint("eagle");
var eagle = builder.buildEagle(point, team.id);
game.engine.spawn(eagle);
gameEventSignal.emit(GameEvent.SPAWN(EAGLE(eagle.id, eagle.rect, eagle.team)));
gameEventSignal.emit(EventUtil.buildEagleSpawn(eagle));
eagle.protect.connect(onEagleProtectChange);
}
}
var bricks = game.engine.map.bricks.map(function(item:Brick):BrickInfo {
return {
id: item.id,
x: item.cellY,
y: item.cellY,
rect: item.rect,
type: item.config.type,
}
});
gameEventSignal.emit(GameEvent.SPAWN(BRICK(bricks)));
gameEventSignal.emit(EventUtil.buildBricksSpawn(game.engine.map));
gameEventSignal.emit(GameEvent.START(state));
}
@@ -112,9 +90,9 @@ class GameRunner implements EngineListener implements GameListener {
}
private function spawn(task:SpawnTask):Void {
var tank = builder.buildTank(++entityId, task.point, task.playerId, task.tankType);
var tank = builder.buildTank(task.point, task.playerId, task.tankType);
game.engine.spawn(tank);
gameEventSignal.emit(GameEvent.SPAWN(TANK(tank.id, tank.rect.clone(), tank.playerId, {type:tank.config.type, hits:tank.hits, bonus:tank.bonus})));
gameEventSignal.emit(EventUtil.buildTankSpawn(tank));
tank.protect.connect(onTankProtectChange);
tank.freezing.connect(onTankFreezingChange);
}
@@ -265,7 +243,7 @@ class GameRunner implements EngineListener implements GameListener {
y: Math.floor(Math.random() * (game.engine.map.gridHeight - 1)),
direction: "right",
}
var bonus = builder.buildBonus(++entityId, point, type);
var bonus = builder.buildBonus(point, type);
game.engine.spawn(bonus);
gameEventSignal.emit(GameEvent.SPAWN(BONUS(bonus.id, bonus.rect.clone(), bonus.config.type)));
}
@@ -353,7 +331,7 @@ class GameRunner implements EngineListener implements GameListener {
if (!tank.freezing.active && player.bullets < tank.config.bullets) {
var rect = tank.rect;
var point = rect.center.add(new Point(rect.width / 4 * rect.direction.x, rect.height / 4 * rect.direction.y));
var bullet = builder.buildBullet(++entityId, point, rect.direction, player.id, tank.config.type);
var bullet = builder.buildBullet(point, rect.direction, player.id, tank.config.type);
bullet.tank = tank;
bullet.move(bullet.rect.direction);
game.engine.spawn(bullet);