[common] added control to PlayerState

This commit is contained in:
2018-01-26 12:01:38 +03:00
parent 7ac181ed5a
commit d72d7e8c81
7 changed files with 69 additions and 31 deletions

View File

@@ -6,13 +6,14 @@ import ru.m.geom.Direction;
import haxe.Timer;
class Bot extends Control {
class BotControl extends Control {
public static var TYPE(default, never):ControlType = 'bot';
private var shotTimer:Timer;
private var turnTimer:Timer;
public function new() {
super();
public function new(index:Int) {
super({type:TYPE, index:index});
}
override public function onCollision(with:EntityType):Void {
@@ -24,8 +25,9 @@ class Bot extends Control {
}
override public function start():Void {
var tank = handler.entities.get(tankId);
action(TankAction.MOVE(tank.rect.direction));
//var tank = handler.entities.get(tankId);
//action(TankAction.MOVE(tank.rect.direction));
action(TankAction.MOVE(Direction.BOTTOM)); // ToDo: hardcode bot start direction
if (shotTimer == null) {
shotTimer = new Timer(1000);
shotTimer.run = shot;

View File

@@ -13,11 +13,23 @@ enum TankAction {
}
typedef ControlType = String;
typedef ControlId = {
var type:ControlType;
var index:Int;
}
class Control {
public var id:ControlId;
public var tankId(default, default):Int;
private var handler:ControlHandler;
public function new() {}
public function new(id:ControlId) {
this.id = id;
}
public function bind(handler:ControlHandler):Void {
this.handler = handler;

View File

@@ -1,9 +1,7 @@
package ru.m.tankz.game;
import ru.m.tankz.game.GameState.PlayerState;
import ru.m.tankz.bot.Bot;
import ru.m.tankz.game.Game;
import ru.m.tankz.config.Config;
class ClassicGame extends Game {
@@ -17,13 +15,6 @@ class ClassicGame extends Game {
super(TYPE);
}
override public function start(state:GameState):Void {
super.start(state);
for (player in teams.get(BOT).players) {
setControl(player.id, new Bot());
}
}
public static function buildState(level:Int, humans:Int):GameState {
var state = new GameState();
state.type = TYPE;
@@ -37,13 +28,24 @@ class ClassicGame extends Game {
group: HUMAN,
type: '1'
},
control:{
type: 'human',
index: i
},
life:3,
};
}
for (i in 0...humans*2+2) {
state.players[BOT][i] = {
index:i,
tank:null,
tank:{
group: BOT,
type: '1'
},
control:{
type: 'bot',
index: i
},
life:-1,
};
}

View File

@@ -1,5 +1,7 @@
package ru.m.tankz.game;
import ru.m.tankz.bot.BotControl;
import ru.m.tankz.control.HumanControl;
import ru.m.tankz.config.ConfigBundle;
import ru.m.tankz.config.MapBundle;
import ru.m.tankz.game.Spawner;
@@ -68,6 +70,15 @@ class Game implements EngineListener {
var player = new Player({team:team.id, index:playerState.index});
team.players.push(player);
teams.set(team.id, team);
if (playerState.control != null) {
var control = switch (playerState.control.type) {
case HumanControl.TYPE: new HumanControl(playerState.control.index);
case BotControl.TYPE: new BotControl(playerState.control.index);
case _: throw 'Unsupported control type: "${playerState.control.type}"';
}
player.control = control;
player.control.bind(engine);
}
}
spawners.set(team.id, new Spawner(team.config, spawn));
}

View File

@@ -4,10 +4,20 @@ import ru.m.tankz.game.Game;
import ru.m.tankz.config.Config;
typedef ControlType = String;
typedef ControId = {
var type:ControlType;
var index:Int;
}
typedef PlayerState = {
var index:Int;
var tank:TankType;
var life:Int;
var control:ControId;
}