game config refactoring

This commit is contained in:
2018-01-06 15:38:27 +03:00
parent 58ed988f75
commit 6ae91246ef
6 changed files with 51 additions and 42 deletions

View File

@@ -9,7 +9,7 @@ import protohx.Message;
import ru.m.tankz.proto.pack.GameUpdateResponse; import ru.m.tankz.proto.pack.GameUpdateResponse;
import ru.m.core.connect.IConnection; import ru.m.core.connect.IConnection;
import haxework.gui.ViewBuilder; import haxework.gui.ViewBuilder;
import ru.m.tankz.config.TankzConfig; import ru.m.tankz.config.Config;
import flash.events.Event; import flash.events.Event;
import haxework.provider.Provider; import haxework.provider.Provider;
import haxework.gui.VGroupView; import haxework.gui.VGroupView;
@@ -31,7 +31,7 @@ class GameFrame extends VGroupView implements ViewBuilder implements IPacketHand
public function onShow():Void { public function onShow():Void {
var game:Game = Provider.get(Game); var game:Game = Provider.get(Game);
engine.init(ConfigData.get(GameType.CLASSIC)); engine.init(ConfigBundle.get(GameType.CLASSIC));
engine.initTanks(game.players); engine.initTanks(game.players);
content.addEventListener(Event.ENTER_FRAME, updateGame); content.addEventListener(Event.ENTER_FRAME, updateGame);
for (index in 0...game.players.length) { for (index in 0...game.players.length) {

View File

@@ -24,20 +24,38 @@ typedef SpawnPoint = {
var direction:Direction; var direction:Direction;
} }
typedef TankzConfig = {
var map:MapConfig; class Config {
var points:Array<SpawnPoint>; public var map(default,null):MapConfig;
public var points(default,null):Array<SpawnPoint>;
public function new(map:MapConfig, points:Array<SpawnPoint>) {
this.map = map;
this.points = points;
} }
class ConfigData { public function getSpawnPoint(type:SpawnPointType, index:Int):SpawnPoint {
public static var CLASSIC:TankzConfig = { for (point in points) {
map: { if (point.type == type && point.index == index) {
return point;
}
}
return null;
}
}
class ConfigBundle {
public static var CLASSIC:Config = new Config(
{
cellWidth: 22, cellWidth: 22,
cellHeight: 22, cellHeight: 22,
gridWidth: 26, gridWidth: 26,
gridHeight: 26 gridHeight: 26
}, },
points: [ [
{ {
type: SpawnPointType.PLAYER, type: SpawnPointType.PLAYER,
index: 0, index: 0,
@@ -53,9 +71,9 @@ class ConfigData {
direction: Direction.TOP direction: Direction.TOP
} }
] ]
}; );
public static function get(type:Int):TankzConfig { public static function get(type:Int):Config {
switch (type) { switch (type) {
case GameType.CLASSIC: case GameType.CLASSIC:
return CLASSIC; return CLASSIC;
@@ -63,13 +81,4 @@ class ConfigData {
return null; return null;
} }
} }
public static function findSpawnPoint(config:TankzConfig, type:SpawnPointType, index:Int):SpawnPoint {
for (point in config.points) {
if (point.type == type && point.index == index) {
return point;
}
}
return null;
}
} }

View File

@@ -8,15 +8,15 @@ import ru.m.tankz.proto.core.Player;
import ru.m.tankz.core.Direction; import ru.m.tankz.core.Direction;
import ru.m.tankz.core.IMobileEntity; import ru.m.tankz.core.IMobileEntity;
//import flash.geom.Rectangle; //import flash.geom.Rectangle;
import ru.m.tankz.config.TankzConfig; import ru.m.tankz.config.Config;
import ru.m.tankz.core.Tank; import ru.m.tankz.core.Tank;
import ru.m.tankz.map.TankzMap; import ru.m.tankz.map.LevelMap;
import ru.m.tankz.map.ITankzMap; import ru.m.tankz.map.ILevelMap;
class Engine implements IEngine { class Engine implements IEngine {
public var config(default, default):TankzConfig; public var config(default, default):Config;
public var map(default, null):ITankzMap; public var map(default, null):ILevelMap;
public var tanks(default, null):Map<Int, Tank>; public var tanks(default, null):Map<Int, Tank>;
public var mobileEntities(default, null):Map<Int, IMobileEntity>; public var mobileEntities(default, null):Map<Int, IMobileEntity>;
@@ -34,9 +34,9 @@ class Engine implements IEngine {
return new Tank(personId, id, cellX * map.cellWidth, cellY * map.cellHeight, direction); return new Tank(personId, id, cellX * map.cellWidth, cellY * map.cellHeight, direction);
} }
public function init(config:TankzConfig):Void { public function init(config:Config):Void {
this.config = config; this.config = config;
map = new TankzMap(config.map); map = new LevelMap(config.map);
tanks = new Map<Int, Tank>(); tanks = new Map<Int, Tank>();
mobileEntities = new Map<Int, IMobileEntity>(); mobileEntities = new Map<Int, IMobileEntity>();
x_limit = map.gridWidth * map.cellWidth; x_limit = map.gridWidth * map.cellWidth;
@@ -47,7 +47,7 @@ class Engine implements IEngine {
var changes = new Array<GameChange>(); var changes = new Array<GameChange>();
for (index in 0...players.length) { for (index in 0...players.length) {
var player:Player = players[index]; var player:Player = players[index];
var point:SpawnPoint = ConfigData.findSpawnPoint(config, SpawnPointType.PLAYER, index); var point:SpawnPoint = config.getSpawnPoint(SpawnPointType.PLAYER, index);
var tank = buildTank(player.id, 0, point.x, point.y, point.direction); var tank = buildTank(player.id, 0, point.x, point.y, point.direction);
this.tanks.set(tank.personId, tank); this.tanks.set(tank.personId, tank);
this.mobileEntities.set(tank.id, tank); this.mobileEntities.set(tank.id, tank);
@@ -70,7 +70,7 @@ class Engine implements IEngine {
case TankAction.MOVE(direction): case TankAction.MOVE(direction):
tank.move(direction); tank.move(direction);
/*Provider.get(IConnection).send( /*Provider.get(IConnection).send(
new GameActionRequest() Map GameActionRequest()
.setType(GameActionType.MOVE) .setType(GameActionType.MOVE)
.setDirectionX(direction.x) .setDirectionX(direction.x)
.setDirectionY(direction.y) .setDirectionY(direction.y)
@@ -78,7 +78,7 @@ class Engine implements IEngine {
case TankAction.STOP: case TankAction.STOP:
tank.stop(); tank.stop();
/*Provider.get(IConnection).send( /*Provider.get(IConnection).send(
new GameActionRequest() Map GameActionRequest()
.setType(GameActionType.STOP) .setType(GameActionType.STOP)
);*/ );*/
case TankAction.SHOT: case TankAction.SHOT:
@@ -87,7 +87,7 @@ class Engine implements IEngine {
mobileEntities.set(bullet.id, bullet); mobileEntities.set(bullet.id, bullet);
} }
/*Provider.get(IConnection).send( /*Provider.get(IConnection).send(
new GameActionRequest() Map GameActionRequest()
.setType(GameActionType.SHOT) .setType(GameActionType.SHOT)
);*/ );*/
} }
@@ -103,7 +103,7 @@ class Engine implements IEngine {
mobileEntities.set(tank.id, tank); mobileEntities.set(tank.id, tank);
tanks.set(tank.personId, tank); tanks.set(tank.personId, tank);
case GameObjectType.BULLET: case GameObjectType.BULLET:
var bullet:Bullet = new Bullet(change.personId, change.objectId, change.x, change.y, 0, Direction.from(change.directionX, change.directionY)); var bullet:Bullet = Map Bullet(change.personId, change.objectId, change.x, change.y, 0, Direction.from(change.directionX, change.directionY));
mobileEntities.set(bullet.id, bullet); mobileEntities.set(bullet.id, bullet);
} }
case GameChangeType.DESTROED: case GameChangeType.DESTROED:
@@ -143,10 +143,10 @@ class Engine implements IEngine {
entiny.x += entiny.mx; entiny.x += entiny.mx;
entiny.y += entiny.my; entiny.y += entiny.my;
/*var tankR = new Rectangle(tank.x, tank.y, tank.width, tank.height); /*var tankR = Map Rectangle(tank.x, tank.y, tank.width, tank.height);
for (t in tanks) if (t != tank) { for (t in tanks) if (t != tank) {
var r = new Rectangle(t.x, t.y, t.width, t.height); var r = Map Rectangle(t.x, t.y, t.width, t.height);
if (tankR.intersects(r)) { if (tankR.intersects(r)) {
if (tank.direction.x > 0) { if (tank.direction.x > 0) {
if (tank.x + tank.width > t.x) tank.x = t.x - tank.width; if (tank.x + tank.width > t.x) tank.x = t.x - tank.width;

View File

@@ -4,17 +4,17 @@ import ru.m.tankz.proto.game.GameChange;
import ru.m.tankz.core.IMobileEntity; import ru.m.tankz.core.IMobileEntity;
import ru.m.tankz.core.Tank; import ru.m.tankz.core.Tank;
import ru.m.tankz.proto.core.Player; import ru.m.tankz.proto.core.Player;
import ru.m.tankz.config.TankzConfig; import ru.m.tankz.config.Config;
import ru.m.tankz.map.ITankzMap; import ru.m.tankz.map.ILevelMap;
interface IEngine { interface IEngine {
public var config(default, default):TankzConfig; public var config(default, default):Config;
public var map(default, null):ITankzMap; public var map(default, null):ILevelMap;
public var tanks(default, null):Map<Int, Tank>; public var tanks(default, null):Map<Int, Tank>;
public var mobileEntities(default, null):Map<Int, IMobileEntity>; public var mobileEntities(default, null):Map<Int, IMobileEntity>;
public function clear():Void; public function clear():Void;
public function init(config:TankzConfig):Void; public function init(config:Config):Void;
public function initTanks(players:Array<Player>):Array<GameChange>; public function initTanks(players:Array<Player>):Array<GameChange>;
public function action(tankId:Int, action:TankAction):Void; public function action(tankId:Int, action:TankAction):Void;
//public function updateFromChanges(changes:Array<GameChange>):Void; //public function updateFromChanges(changes:Array<GameChange>):Void;

View File

@@ -1,6 +1,6 @@
package ru.m.tankz.map; package ru.m.tankz.map;
interface ITankzMap { interface ILevelMap {
public var cellWidth(default, null):Float; public var cellWidth(default, null):Float;
public var cellHeight(default, null):Float; public var cellHeight(default, null):Float;
public var gridWidth(default, null):Int; public var gridWidth(default, null):Int;

View File

@@ -1,8 +1,8 @@
package ru.m.tankz.map; package ru.m.tankz.map;
import ru.m.tankz.config.TankzConfig.MapConfig; import ru.m.tankz.config.Config.MapConfig;
class TankzMap implements ITankzMap { class LevelMap implements ILevelMap {
public var cellWidth(default, null):Float; public var cellWidth(default, null):Float;
public var cellHeight(default, null):Float; public var cellHeight(default, null):Float;