[client] PlayerTank to PlayerControl change

This commit is contained in:
2018-01-04 20:40:09 +03:00
parent 4d27f2a55d
commit b2ba693dbf
10 changed files with 328 additions and 292 deletions

View File

@@ -2,7 +2,8 @@ set :npm_path, -> { release_path }
namespace :npm do namespace :npm do
task :prepare do task :prepare do
on roles(:all) do npm_role = fetch(:npm_role)
on roles(npm_role) do
npm_target_path = fetch(:npm_target_path) npm_target_path = fetch(:npm_target_path)
npm_path = fetch(:npm_path) npm_path = fetch(:npm_path)
execute "mkdir -p #{npm_target_path}" execute "mkdir -p #{npm_target_path}"
@@ -10,7 +11,8 @@ namespace :npm do
end end
end end
task :link do task :link do
on roles(:all) do npm_role = fetch(:npm_role)
on roles(npm_role) do
npm_target_path = fetch(:npm_target_path) npm_target_path = fetch(:npm_target_path)
npm_path = fetch(:npm_path) npm_path = fetch(:npm_path)
execute "ln -s #{npm_target_path}/node_modules #{npm_path}/" execute "ln -s #{npm_target_path}/node_modules #{npm_path}/"

View File

@@ -0,0 +1,89 @@
package ru.m.tankz.core;
import flash.events.FocusEvent;
import flash.ui.Keyboard;
import ru.m.tankz.engine.IEngine;
import ru.m.tankz.core.Tank.TankAction;
import flash.events.KeyboardEvent;
import flash.Lib;
class PlayerControl {
private var engine:IEngine;
private var id:Int;
private var keyBinding:Map<Int, TankAction>;
private var moveQueue:Array<Int>;
public function new(id:Int, engine:IEngine, keyBinding:Map<Int, TankAction>) {
this.id = id;
this.engine = engine;
this.keyBinding = keyBinding;
moveQueue = new Array<Int>();
Lib.current.stage.addEventListener(KeyboardEvent.KEY_DOWN, onKeyDown);
Lib.current.stage.addEventListener(KeyboardEvent.KEY_UP, onKeyUp);
Lib.current.stage.addEventListener(FocusEvent.FOCUS_OUT, onFocusOut);
}
private function onKeyDown(event:KeyboardEvent):Void {
if (keyBinding.exists(event.keyCode)) switch (keyBinding.get(event.keyCode)) {
case TankAction.MOVE(direction):
if (moveQueue.indexOf(event.keyCode) == -1) {
moveQueue.unshift(event.keyCode);
updateMove();
}
case TankAction.SHOT:
engine.action(id, TankAction.SHOT);
case _:
}
}
private function onKeyUp(event:KeyboardEvent):Void {
if (keyBinding.exists(event.keyCode)) switch (keyBinding.get(event.keyCode)) {
case TankAction.MOVE(direction):
moveQueue.remove(event.keyCode);
updateMove();
case _:
}
}
private function onFocusOut(event:FocusEvent):Void {
moveQueue = [];
updateMove();
}
private function updateMove():Void {
if (moveQueue.length == 0) {
engine.action(id, TankAction.STOP);
} else {
switch (keyBinding.get(moveQueue[0])) {
case TankAction.MOVE(direction):
engine.action(id, TankAction.MOVE(direction));
case _:
}
}
}
public static function forPlayer(index:Int, tankId:Int, engine:IEngine):PlayerControl {
switch (index) {
case 0:
return new PlayerControl(tankId, engine, [
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(tankId, engine, [
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}';
}
}
}

View File

@@ -1,72 +0,0 @@
package ru.m.tankz.core;
import ru.m.tankz.proto.GameActionType;
import ru.m.tankz.proto.GameActionRequest;
import ru.m.core.connect.IConnection;
import haxework.provider.Provider;
import ru.m.tankz.core.Tank.TankAction;
import flash.events.KeyboardEvent;
import flash.Lib;
class PlayerTank extends Tank {
private var keyBinding:Map<Int, TankAction>;
private var moveQueue:Array<Int>;
public function new(personId:Int, id:Int, x:Float, y:Float, direction:Direction, keyBinding:Map<Int, TankAction>) {
super(personId, id, x, y, direction);
this.keyBinding = keyBinding;
moveQueue = new Array<Int>();
Lib.current.stage.addEventListener(KeyboardEvent.KEY_DOWN, onKeyDown);
Lib.current.stage.addEventListener(KeyboardEvent.KEY_UP, onKeyUp);
}
private function onKeyDown(event:KeyboardEvent):Void {
if (keyBinding.exists(event.keyCode)) switch (keyBinding.get(event.keyCode)) {
case TankAction.MOVE(direction):
if (moveQueue.indexOf(event.keyCode) == -1) {
moveQueue.unshift(event.keyCode);
updateMove();
}
case TankAction.SHOT:
//ToDo:
shot();
/*Provider.get(IConnection).send(
new GameActionRequest()
.setType(GameActionType.SHOT)
);*/
}
}
private function onKeyUp(event:KeyboardEvent):Void {
if (keyBinding.exists(event.keyCode)) switch (keyBinding.get(event.keyCode)) {
case TankAction.MOVE(direction):
moveQueue.remove(event.keyCode);
updateMove();
case _: {};
}
}
private function updateMove():Void {
if (moveQueue.length == 0) {
stop();
/*Provider.get(IConnection).send(
new GameActionRequest()
.setType(GameActionType.STOP)
);*/
} else {
switch (keyBinding.get(moveQueue[0])) {
case TankAction.MOVE(direction):
//ToDo:
move(direction);
/*Provider.get(IConnection).send(
new GameActionRequest()
.setType(GameActionType.MOVE)
.setDirectionX(direction.x)
.setDirectionY(direction.y)
);*/
case _:
}
}
}
}

View File

@@ -7,5 +7,6 @@ import ru.m.tankz.proto.Account;
class GameData { class GameData {
public var account:Account; public var account:Account;
public var person:Person; public var person:Person;
public var players:Array<Person>;
public var game:Game; public var game:Game;
} }

View File

@@ -1,29 +0,0 @@
package ru.m.tankz.engine;
import ru.m.tankz.core.Direction;
import ru.m.tankz.core.Tank;
import flash.ui.Keyboard;
import ru.m.tankz.core.PlayerTank;
class ClientEngine extends Engine {
public var personId(default, default):Int;
public function new() {
super();
}
override private function buildTank(personId:Int, id:Int, x:Float, y:Float, direction:Direction):Tank {
return if (this.personId == personId) {
new PlayerTank(personId, id, x, y, direction, [
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
]);
} else {
super.buildTank(personId, id, x, y, direction);
}
}
}

View File

@@ -1,6 +1,8 @@
package ru.m.tankz.view.frames; package ru.m.tankz.view.frames;
import ru.m.tankz.engine.ClientEngine; import ru.m.tankz.core.PlayerControl;
import ru.m.tankz.engine.Engine;
import ru.m.tankz.engine.IEngine;
import protohx.Message; import protohx.Message;
import ru.m.tankz.proto.GameUpdateResponse; import ru.m.tankz.proto.GameUpdateResponse;
import ru.m.core.connect.IConnection; import ru.m.core.connect.IConnection;
@@ -18,19 +20,23 @@ class GameFrame extends VGroupView implements ViewBuilder implements IPacketHand
public static inline var ID = "game"; public static inline var ID = "game";
private var engine:ClientEngine; private var engine:IEngine;
private var controls:Map<Int, PlayerControl>;
public function init():Void { public function init():Void {
engine = new ClientEngine(); engine = new Engine();
controls = new Map<Int, PlayerControl>();
} }
public function onShow():Void { public function onShow():Void {
var person = Provider.get(GameData).person; var data:GameData = Provider.get(GameData);
var persons = Provider.get(GameData).game.persons;
engine.personId = person.id;
engine.init(DEFAULT.CONFIG); engine.init(DEFAULT.CONFIG);
engine.initTanks(persons); engine.initTanks(data.game.persons); // ToDo:
content.addEventListener(Event.ENTER_FRAME, updateGame); content.addEventListener(Event.ENTER_FRAME, updateGame);
for (index in 0...data.players.length) {
var playerId:Int = data.players[index].id;
controls.set(playerId, PlayerControl.forPlayer(index, playerId, engine));
}
Provider.get(IConnection).packetHandler.addListener(this); Provider.get(IConnection).packetHandler.addListener(this);
render.draw(engine); render.draw(engine);
} }

View File

@@ -16,20 +16,28 @@ class StartFrame extends VGroupView implements ViewBuilder {
public function init() { public function init() {
start_1p.onPress = this; start_1p.onPress = this;
start_2p.onPress = this;
} }
public function onPress(view:ButtonView):Void { public function onPress(view:ButtonView):Void {
switch (view.id) { switch (view.id) {
case "start_1p": case "start_1p":
var player = new Person(); startGame(1);
player.id = 1; case "start_2p":
startGame(2);
}
}
private function startGame(playersCount:Int):Void {
var game = new Game(); var game = new Game();
game.creator = player; for (i in 0...playersCount) {
var player = new Person();
player.id = i;
game.persons.push(player); game.persons.push(player);
}
game.id = 1; game.id = 1;
Provider.get(GameData).person = player; Provider.get(GameData).players = game.persons;
Provider.get(GameData).game = game; Provider.get(GameData).game = game;
Provider.get(IFrameSwitcher).change(GameFrame.ID); Provider.get(IFrameSwitcher).change(GameFrame.ID);
} }
} }
}

View File

@@ -2,6 +2,7 @@ package ru.m.tankz.core;
enum TankAction { enum TankAction {
MOVE(direction:Direction); MOVE(direction:Direction);
STOP;
SHOT; SHOT;
} }

View File

@@ -65,6 +65,35 @@ class Engine implements IEngine {
return changes; return changes;
} }
public function action(tankId:Int, action:TankAction):Void {
var tank:Tank = tanks.get(tankId);
switch (action) {
case TankAction.MOVE(direction):
tank.move(direction);
/*Provider.get(IConnection).send(
new GameActionRequest()
.setType(GameActionType.MOVE)
.setDirectionX(direction.x)
.setDirectionY(direction.y)
);*/
case TankAction.STOP:
tank.stop();
/*Provider.get(IConnection).send(
new GameActionRequest()
.setType(GameActionType.STOP)
);*/
case TankAction.SHOT:
var bullet = tank.shot();
if (bullet != null) {
mobileEntities.set(bullet.id, bullet);
}
/*Provider.get(IConnection).send(
new GameActionRequest()
.setType(GameActionType.SHOT)
);*/
}
}
public function updateFromChanges(changes:Array<GameChange>):Void { public function updateFromChanges(changes:Array<GameChange>):Void {
for (change in changes) { for (change in changes) {
switch (change.type) { switch (change.type) {

View File

@@ -16,6 +16,7 @@ interface IEngine {
public function clear():Void; public function clear():Void;
public function init(config:TankzConfig):Void; public function init(config:TankzConfig):Void;
public function initTanks(persons:Array<Person>):Array<GameChange>; public function initTanks(persons:Array<Person>):Array<GameChange>;
public function action(tankId:Int, action:TankAction):Void;
public function updateFromChanges(changes:Array<GameChange>):Void; public function updateFromChanges(changes:Array<GameChange>):Void;
public function update():Array<GameChange>; public function update():Array<GameChange>;
} }