proto update
This commit is contained in:
68
src/client/haxe/ru/m/tankz/core/PlayerTank.hx
Executable file
68
src/client/haxe/ru/m/tankz/core/PlayerTank.hx
Executable file
@@ -0,0 +1,68 @@
|
||||
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(id:Int, x:Float, y:Float, keyBinding:Map<Int, TankAction>) {
|
||||
super(id, x, y);
|
||||
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();
|
||||
} 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 _:
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
30
src/client/haxe/ru/m/tankz/game/ClientTankz.hx
Normal file
30
src/client/haxe/ru/m/tankz/game/ClientTankz.hx
Normal file
@@ -0,0 +1,30 @@
|
||||
package ru.m.tankz.game;
|
||||
|
||||
import ru.m.tankz.core.Direction;
|
||||
import ru.m.tankz.core.Tank.TankAction;
|
||||
import flash.ui.Keyboard;
|
||||
import ru.m.tankz.core.PlayerTank;
|
||||
import ru.m.tankz.core.ITank;
|
||||
|
||||
class ClientTankz extends Tankz {
|
||||
|
||||
public var personId(default, default):Int;
|
||||
|
||||
public function new() {
|
||||
super();
|
||||
}
|
||||
|
||||
override private function buildTank(id:Int, x:Float, y:Float):ITank {
|
||||
return if (id == personId) {
|
||||
new PlayerTank(id, x, y, [
|
||||
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(id, x, y);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,48 +1,41 @@
|
||||
package ru.m.tankz.view.frames;
|
||||
|
||||
import ru.m.tankz.game.ClientTankz;
|
||||
import protohx.Message;
|
||||
import ru.m.tankz.proto.GameUpdateResponse;
|
||||
import ru.m.core.connect.IConnection;
|
||||
import haxework.gui.ViewBuilder;
|
||||
import ru.m.tankz.config.TankzConfig;
|
||||
import flash.events.Event;
|
||||
import ru.m.tankz.game.Tankz;
|
||||
import ru.m.tankz.game.ITankz;
|
||||
import haxework.gui.LabelView;
|
||||
import haxework.gui.ButtonView;
|
||||
import haxework.provider.Provider;
|
||||
import ru.m.tankz.data.GameData;
|
||||
import haxework.gui.VGroupView;
|
||||
|
||||
@:template("layout/frames/game.json", "layout/styles.json")
|
||||
class GameFrame extends VGroupView implements ViewBuilder {
|
||||
class GameFrame extends VGroupView implements ViewBuilder implements IPacketHandler {
|
||||
|
||||
private static inline var TAG = "GameFrame";
|
||||
|
||||
public static inline var ID = "game";
|
||||
|
||||
private var game:ITankz;
|
||||
|
||||
private var config:TankzConfig;
|
||||
private var game:ClientTankz;
|
||||
|
||||
public function init():Void {
|
||||
game = new Tankz();
|
||||
config = {
|
||||
map: {
|
||||
cellWidth: 20,
|
||||
cellHeight: 20,
|
||||
gridWidth: 26,
|
||||
gridHeight: 26
|
||||
}
|
||||
};
|
||||
restart.onPress = this;
|
||||
game = new ClientTankz();
|
||||
}
|
||||
|
||||
public function onShow():Void {
|
||||
var person = Provider.get(GameData).person;
|
||||
var persons = Provider.get(GameData).game.persons;
|
||||
name.text = person.name;
|
||||
game.init(config);
|
||||
game.personId = person.id;
|
||||
game.init(persons, DEFAULT.CONFIG);
|
||||
content.addEventListener(Event.ENTER_FRAME, updateGame);
|
||||
Provider.get(IConnection).packetHandler.addListener(this);
|
||||
}
|
||||
|
||||
public function onHide():Void {
|
||||
Provider.get(IConnection).packetHandler.removeListener(this);
|
||||
content.removeEventListener(Event.ENTER_FRAME, updateGame);
|
||||
game.clear();
|
||||
}
|
||||
@@ -52,11 +45,9 @@ class GameFrame extends VGroupView implements ViewBuilder {
|
||||
render.draw(game);
|
||||
}
|
||||
|
||||
public function onPress(view:ButtonView):Void {
|
||||
switch (view.id) {
|
||||
case "restart":
|
||||
game.clear();
|
||||
game.init(config);
|
||||
}
|
||||
public function onGameUpdateResponse(packet:GameUpdateResponse):Void {
|
||||
|
||||
}
|
||||
|
||||
public function onPacket(packet:Message):Void {}
|
||||
}
|
||||
|
||||
@@ -24,7 +24,6 @@ class GameReadyFrame extends VGroupView implements ViewBuilder implements IPacke
|
||||
public static inline var ID = "game_ready";
|
||||
|
||||
public function init() {
|
||||
list = findViewById("list");
|
||||
//list.dispatcher.addListener(this);
|
||||
findViewById("start", ButtonView).onPress = this;
|
||||
findViewById("exit", ButtonView).onPress = this;
|
||||
|
||||
Reference in New Issue
Block a user