[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

@@ -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}';
}
}
}

View File

@@ -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);