[common] added control
This commit is contained in:
@@ -1,26 +1,21 @@
|
||||
package ru.m.tankz.core;
|
||||
package ru.m.tankz.control;
|
||||
|
||||
import ru.m.tankz.control.Control;
|
||||
import haxe.Timer;
|
||||
import ru.m.geom.Direction;
|
||||
import flash.events.FocusEvent;
|
||||
import flash.ui.Keyboard;
|
||||
import ru.m.tankz.engine.Engine;
|
||||
import ru.m.tankz.core.Tank.TankAction;
|
||||
import flash.events.KeyboardEvent;
|
||||
import flash.Lib;
|
||||
|
||||
class PlayerControl {
|
||||
|
||||
private var engine:Engine;
|
||||
private var tankId:Int;
|
||||
class PlayerControl extends Control {
|
||||
|
||||
private var keyBinding:Map<Int, TankAction>;
|
||||
private var moveQueue:Array<Int>;
|
||||
private var shotTimer:Timer;
|
||||
|
||||
public function new(tankId:Int, engine:Engine, keyBinding:Map<Int, TankAction>) {
|
||||
this.tankId = tankId;
|
||||
this.engine = engine;
|
||||
public function new(tankId:Int, keyBinding:Map<Int, TankAction>) {
|
||||
super(tankId);
|
||||
this.keyBinding = keyBinding;
|
||||
moveQueue = new Array<Int>();
|
||||
Lib.current.stage.addEventListener(KeyboardEvent.KEY_DOWN, onKeyDown);
|
||||
@@ -44,7 +39,7 @@ class PlayerControl {
|
||||
case _:
|
||||
}
|
||||
if (event.keyCode == Keyboard.U) {
|
||||
engine.action(tankId, TankAction.LEVEL_UP(1));
|
||||
action(TankAction.LEVEL_UP(1));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -69,24 +64,24 @@ class PlayerControl {
|
||||
|
||||
private function updateMove():Void {
|
||||
if (moveQueue.length == 0) {
|
||||
engine.action(tankId, TankAction.STOP);
|
||||
action(TankAction.STOP);
|
||||
} else {
|
||||
switch (keyBinding.get(moveQueue[0])) {
|
||||
case TankAction.MOVE(direction):
|
||||
engine.action(tankId, TankAction.MOVE(direction));
|
||||
action(TankAction.MOVE(direction));
|
||||
case _:
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private function shot():Void {
|
||||
engine.action(tankId, TankAction.SHOT);
|
||||
action(TankAction.SHOT);
|
||||
}
|
||||
|
||||
public static function forPlayer(index:Int, tankId:Int, engine:Engine):PlayerControl {
|
||||
public static function forPlayer(index:Int, tankId:Int):PlayerControl {
|
||||
switch (index) {
|
||||
case 0:
|
||||
return new PlayerControl(tankId, engine, [
|
||||
return new PlayerControl(tankId, [
|
||||
Keyboard.A => TankAction.MOVE(Direction.LEFT),
|
||||
Keyboard.S => TankAction.MOVE(Direction.BOTTOM),
|
||||
Keyboard.W => TankAction.MOVE(Direction.TOP),
|
||||
@@ -94,7 +89,7 @@ class PlayerControl {
|
||||
Keyboard.SPACE => TankAction.SHOT
|
||||
]);
|
||||
case 1:
|
||||
return new PlayerControl(tankId, engine, [
|
||||
return new PlayerControl(tankId, [
|
||||
Keyboard.LEFT => TankAction.MOVE(Direction.LEFT),
|
||||
Keyboard.DOWN => TankAction.MOVE(Direction.BOTTOM),
|
||||
Keyboard.UP => TankAction.MOVE(Direction.TOP),
|
||||
@@ -4,7 +4,7 @@ import haxe.Timer;
|
||||
import ru.m.tankz.config.ConfigBundle;
|
||||
import ru.m.tankz.proto.core.GameType;
|
||||
import ru.m.tankz.proto.core.Game;
|
||||
import ru.m.tankz.core.PlayerControl;
|
||||
import ru.m.tankz.control.PlayerControl;
|
||||
import ru.m.tankz.engine.Engine;
|
||||
import protohx.Message;
|
||||
import ru.m.tankz.proto.pack.GameUpdateResponse;
|
||||
@@ -23,12 +23,10 @@ class GameFrame extends VGroupView implements ViewBuilder implements IPacketHand
|
||||
public static inline var ID = "game";
|
||||
|
||||
private var engine:Engine;
|
||||
private var controls:Map<Int, PlayerControl>;
|
||||
private var timer:Timer;
|
||||
|
||||
public function init():Void {
|
||||
engine = new Engine();
|
||||
controls = new Map<Int, PlayerControl>();
|
||||
}
|
||||
|
||||
public function onShow():Void {
|
||||
@@ -39,7 +37,7 @@ class GameFrame extends VGroupView implements ViewBuilder implements IPacketHand
|
||||
for (index in 0...game.players.length) {
|
||||
var playerId:Int = game.players[index].id;
|
||||
var tankId:Int = engine.playerTanks.get(playerId).id;
|
||||
controls.set(tankId, PlayerControl.forPlayer(index, tankId, engine));
|
||||
engine.bindControl(PlayerControl.forPlayer(index, tankId));
|
||||
}
|
||||
Provider.get(IConnection).packetHandler.addListener(this);
|
||||
render.draw(engine);
|
||||
|
||||
Reference in New Issue
Block a user