[common] added game package

This commit is contained in:
2018-01-21 21:09:55 +03:00
parent f1bb6514ad
commit a4558aa2f2
13 changed files with 272 additions and 234 deletions

View File

@@ -14,8 +14,8 @@ class PlayerControl extends Control {
private var moveQueue:Array<Int>;
private var shotTimer:Timer;
public function new(tankId:Int, keyBinding:Map<Int, TankAction>) {
super(tankId);
public function new(keyBinding:Map<Int, TankAction>) {
super();
this.keyBinding = keyBinding;
moveQueue = new Array<Int>();
Lib.current.stage.addEventListener(KeyboardEvent.KEY_DOWN, onKeyDown);
@@ -78,10 +78,10 @@ class PlayerControl extends Control {
action(TankAction.SHOT);
}
public static function forPlayer(index:Int, tankId:Int):PlayerControl {
public static function forPlayer(index:Int):PlayerControl {
switch (index) {
case 0:
return new PlayerControl(tankId, [
return new PlayerControl([
Keyboard.A => TankAction.MOVE(Direction.LEFT),
Keyboard.S => TankAction.MOVE(Direction.BOTTOM),
Keyboard.W => TankAction.MOVE(Direction.TOP),
@@ -89,7 +89,7 @@ class PlayerControl extends Control {
Keyboard.SPACE => TankAction.SHOT
]);
case 1:
return new PlayerControl(tankId, [
return new PlayerControl([
Keyboard.LEFT => TankAction.MOVE(Direction.LEFT),
Keyboard.DOWN => TankAction.MOVE(Direction.BOTTOM),
Keyboard.UP => TankAction.MOVE(Direction.TOP),

View File

@@ -69,13 +69,13 @@ class Render extends SpriteView {
entryLayer.addChild(items[entity.key].view);
}
}
for (key in game.removedEntities) {
/*for (key in game.removedEntities) {
if (items.exists(key)) {
var view:DisplayObject = items[key].view;
view.parent.removeChild(view);
items.remove(key);
}
}
}*/
for (item in items) {
item.update();
}

View File

@@ -1,18 +1,19 @@
package ru.m.tankz.view.frames;
import flash.events.Event;
import haxe.Timer;
import haxework.gui.VGroupView;
import haxework.gui.ViewBuilder;
import haxework.provider.Provider;
import protohx.Message;
import ru.m.connect.IConnection;
import ru.m.tankz.config.ConfigBundle;
import ru.m.tankz.proto.core.GameType;
import ru.m.tankz.proto.core.Game;
import ru.m.tankz.control.PlayerControl;
import ru.m.tankz.engine.Engine;
import protohx.Message;
import ru.m.tankz.game.ClassicGame;
import ru.m.tankz.game.Game;
import ru.m.tankz.player.Player;
import ru.m.tankz.proto.pack.GameUpdateResponse;
import ru.m.connect.IConnection;
import haxework.gui.ViewBuilder;
import flash.events.Event;
import haxework.provider.Provider;
import haxework.gui.VGroupView;
@:template("layout/frames/game.json", "layout/styles.json")
@@ -22,25 +23,20 @@ class GameFrame extends VGroupView implements ViewBuilder implements IPacketHand
public static inline var ID = "game";
private var engine:Engine;
private var game:Game;
private var timer:Timer;
public function init():Void {
engine = new Engine();
}
public function onShow():Void {
var game:Game = Provider.get(Game);
engine.init(ConfigBundle.get(GameType.CLASSIC, 0));
engine.initTanks(game.players);
game = new ClassicGame(ConfigBundle.get(ClassicGame.TYPE, 0));
game.start([
new Player(1)
]);
content.addEventListener(Event.ENTER_FRAME, redraw);
for (index in 0...game.players.length) {
var playerId:Int = game.players[index].id;
var tankId:Int = engine.playerTanks.get(playerId).id;
engine.bindControl(PlayerControl.forPlayer(index, tankId));
}
Provider.get(IConnection).packetHandler.addListener(this);
render.draw(engine);
render.draw(game.engine);
timer = new Timer(10);
timer.run = updateEngine;
}
@@ -52,21 +48,21 @@ class GameFrame extends VGroupView implements ViewBuilder implements IPacketHand
timer = null;
}
content.removeEventListener(Event.ENTER_FRAME, redraw);
engine.clear();
game.dispose();
render.reset();
}
private function updateEngine():Void {
engine.update();
game.engine.update();
}
private function redraw(_):Void {
render.draw(engine);
render.draw(game.engine);
}
public function onGameUpdateResponse(packet:GameUpdateResponse):Void {
//engine.updateFromChanges(packet.changes);
render.draw(engine);
render.draw(game.engine);
}
public function onPacket(packet:Message):Void {}