[common] added control to PlayerState
This commit is contained in:
@@ -8,15 +8,20 @@ import flash.ui.Keyboard;
|
||||
import flash.events.KeyboardEvent;
|
||||
import flash.Lib;
|
||||
|
||||
class PlayerControl extends Control {
|
||||
|
||||
private var keyBinding:Map<Int, TankAction>;
|
||||
typedef KeyBinding = Map<Int, TankAction>;
|
||||
|
||||
|
||||
class HumanControl extends Control {
|
||||
public static var TYPE(default, never):ControlType = 'human';
|
||||
|
||||
private var keyBinding:KeyBinding;
|
||||
private var moveQueue:Array<Int>;
|
||||
private var shotTimer:Timer;
|
||||
|
||||
public function new(keyBinding:Map<Int, TankAction>) {
|
||||
super();
|
||||
this.keyBinding = keyBinding;
|
||||
public function new(index:Int) {
|
||||
super({type:TYPE, index:index});
|
||||
this.keyBinding = resolve(index);
|
||||
moveQueue = new Array<Int>();
|
||||
Lib.current.stage.addEventListener(KeyboardEvent.KEY_DOWN, onKeyDown);
|
||||
Lib.current.stage.addEventListener(KeyboardEvent.KEY_UP, onKeyUp);
|
||||
@@ -78,26 +83,26 @@ class PlayerControl extends Control {
|
||||
action(TankAction.SHOT);
|
||||
}
|
||||
|
||||
public static function forPlayer(index:Int):PlayerControl {
|
||||
private static function resolve(index:Int):KeyBinding {
|
||||
switch (index) {
|
||||
case 0:
|
||||
return new PlayerControl([
|
||||
return [
|
||||
Keyboard.A => TankAction.MOVE(Direction.LEFT),
|
||||
Keyboard.S => TankAction.MOVE(Direction.BOTTOM),
|
||||
Keyboard.W => TankAction.MOVE(Direction.TOP),
|
||||
Keyboard.D => TankAction.MOVE(Direction.RIGHT),
|
||||
Keyboard.SPACE => TankAction.SHOT
|
||||
]);
|
||||
];
|
||||
case 1:
|
||||
return new PlayerControl([
|
||||
return [
|
||||
Keyboard.LEFT => TankAction.MOVE(Direction.LEFT),
|
||||
Keyboard.DOWN => TankAction.MOVE(Direction.BOTTOM),
|
||||
Keyboard.UP => TankAction.MOVE(Direction.TOP),
|
||||
Keyboard.RIGHT => TankAction.MOVE(Direction.RIGHT),
|
||||
Keyboard.NUMPAD_0 => TankAction.SHOT
|
||||
]);
|
||||
];
|
||||
case _:
|
||||
throw 'Invalid player index ${index}';
|
||||
throw 'Invalid control index ${index}';
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,7 +1,6 @@
|
||||
package ru.m.tankz.view.frames;
|
||||
|
||||
import ru.m.tankz.game.GameState;
|
||||
import ru.m.tankz.control.PlayerControl;
|
||||
import flash.events.Event;
|
||||
import haxe.Timer;
|
||||
import haxework.gui.VGroupView;
|
||||
@@ -34,9 +33,6 @@ class GameFrame extends VGroupView implements ViewBuilder implements IPacketHand
|
||||
}
|
||||
game.engine.listeners.push(render);
|
||||
game.start(state);
|
||||
for (human in state.players['human'].iterator()) {
|
||||
game.setControl({team:'human', index:human.index}, PlayerControl.forPlayer(human.index));
|
||||
}
|
||||
content.addEventListener(Event.ENTER_FRAME, redraw);
|
||||
Provider.get(IConnection).packetHandler.addListener(this);
|
||||
render.draw(game.engine);
|
||||
|
||||
@@ -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;
|
||||
@@ -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;
|
||||
|
||||
@@ -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,
|
||||
};
|
||||
}
|
||||
|
||||
@@ -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));
|
||||
}
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user