[common] add GameEvent.MOVE
This commit is contained in:
@@ -114,6 +114,7 @@ import ru.m.tankz.map.LevelMap;
|
||||
var d = entity.rect.center.x - cell.rect.center.x;
|
||||
entity.rect.x += d / Math.abs(d);
|
||||
}
|
||||
moveSignal.emit(entityType);
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
@@ -1,6 +1,5 @@
|
||||
package ru.m.tankz.game;
|
||||
|
||||
import haxe.Timer;
|
||||
import ru.m.geom.Point;
|
||||
import ru.m.tankz.bundle.IConfigBundle;
|
||||
import ru.m.tankz.config.Config;
|
||||
@@ -28,7 +27,6 @@ import ru.m.tankz.Type;
|
||||
public var state(default, null):GameState;
|
||||
|
||||
private var builder:EntityBuilder;
|
||||
private var timer:Timer;
|
||||
|
||||
@:provide var configBundle:IConfigBundle;
|
||||
|
||||
@@ -53,10 +51,6 @@ import ru.m.tankz.Type;
|
||||
}
|
||||
}
|
||||
|
||||
private function update():Void {
|
||||
engine.update();
|
||||
}
|
||||
|
||||
private function applyPosition(entity:Entity, position:Position):Void {
|
||||
entity.rect.center = new Point(position.x, position.y);
|
||||
if (position.direction != null) {
|
||||
@@ -76,15 +70,9 @@ import ru.m.tankz.Type;
|
||||
switch event {
|
||||
case GameEvent.START(state):
|
||||
this.state = state;
|
||||
timer = new Timer(10);
|
||||
timer.run = update;
|
||||
case GameEvent.COMPLETE(state, winnerId):
|
||||
this.state = state;
|
||||
this.winner = winnerId;
|
||||
if (timer != null) {
|
||||
timer.stop();
|
||||
timer = null;
|
||||
}
|
||||
case GameEvent.SPAWN(EAGLE(id, position, teamId)):
|
||||
var eagle = builder.buildEagle(id, teamId);
|
||||
applyPosition(eagle, position);
|
||||
@@ -133,10 +121,6 @@ import ru.m.tankz.Type;
|
||||
}
|
||||
|
||||
public function dispose():Void {
|
||||
if (timer != null) {
|
||||
timer.stop();
|
||||
timer = null;
|
||||
}
|
||||
gameEventSignal.dispose();
|
||||
engine.dispose();
|
||||
}
|
||||
|
||||
@@ -36,6 +36,11 @@ enum DestroyEvent {
|
||||
CELL(cellX:Int, cellY:Int, shot:Shot);
|
||||
}
|
||||
|
||||
enum MoveEvent {
|
||||
TANK(id:Int, position:Position);
|
||||
BULLET(id:Int, position:Position);
|
||||
}
|
||||
|
||||
enum ChangeEvent {
|
||||
PLAYER_SCORE(playerId:PlayerId, value:Int);
|
||||
PLAYER_LIFE(playerId:PlayerId, value:Int);
|
||||
@@ -46,6 +51,7 @@ enum ChangeEvent {
|
||||
enum GameEvent {
|
||||
START(state:GameState);
|
||||
SPAWN(event:SpawnEvent);
|
||||
MOVE(event:MoveEvent);
|
||||
HIT(event:HitEvent);
|
||||
DESTROY(event:DestroyEvent);
|
||||
CHANGE(event:ChangeEvent);
|
||||
|
||||
@@ -5,7 +5,6 @@ import haxe.Timer;
|
||||
import haxework.signal.Signal;
|
||||
import ru.m.geom.Line;
|
||||
import ru.m.geom.Point;
|
||||
import ru.m.tankz.config.Config;
|
||||
import ru.m.tankz.control.Control;
|
||||
import ru.m.tankz.control.Controller;
|
||||
import ru.m.tankz.control.IControlFactory;
|
||||
@@ -26,6 +25,7 @@ class GameRunner implements EngineListener implements GameListener {
|
||||
private var game(default, null):IGame;
|
||||
private var gameEventSignal(get, null):Signal<GameEvent>;
|
||||
private var entityId:Int;
|
||||
private var timer:Timer;
|
||||
|
||||
public function new(game:IGame) {
|
||||
this.game = game;
|
||||
@@ -38,7 +38,15 @@ class GameRunner implements EngineListener implements GameListener {
|
||||
return game.gameEventSignal;
|
||||
}
|
||||
|
||||
private function update():Void {
|
||||
game.engine.update();
|
||||
}
|
||||
|
||||
public function dispose():Void {
|
||||
if (timer != null) {
|
||||
timer.stop();
|
||||
timer = null;
|
||||
}
|
||||
game.disconnect(this);
|
||||
game.engine.disconnect(this);
|
||||
}
|
||||
@@ -188,6 +196,13 @@ class GameRunner implements EngineListener implements GameListener {
|
||||
}
|
||||
|
||||
public function onMove(entity:EntityType):Void {
|
||||
switch entity {
|
||||
case TANK(tank):
|
||||
gameEventSignal.emit(GameEvent.MOVE(TANK(tank.id, {x:tank.rect.x, y:tank.rect.y, direction:tank.rect.direction})));
|
||||
case BULLET(bullet):
|
||||
gameEventSignal.emit(GameEvent.MOVE(BULLET(bullet.id, {x:bullet.rect.x, y:bullet.rect.y, direction:bullet.rect.direction})));
|
||||
case _:
|
||||
}
|
||||
}
|
||||
|
||||
public function onDestroy(entity:EntityType):Void {
|
||||
@@ -270,9 +285,13 @@ class GameRunner implements EngineListener implements GameListener {
|
||||
public function onGameEvent(event:GameEvent):Void {
|
||||
switch event {
|
||||
case GameEvent.START(_):
|
||||
|
||||
timer = new Timer(10);
|
||||
timer.run = update;
|
||||
case GameEvent.COMPLETE(_, _):
|
||||
|
||||
if (timer != null) {
|
||||
timer.stop();
|
||||
timer = null;
|
||||
}
|
||||
case GameEvent.ACTION(tankId, SHOT):
|
||||
var tank:Tank = cast game.engine.entities.get(tankId);
|
||||
var player = game.getPlayer(tank.playerId);
|
||||
|
||||
Reference in New Issue
Block a user