[common] add GameEvent

This commit is contained in:
2019-04-22 17:40:36 +03:00
parent 7ddba8f69b
commit 5ff7b19475
9 changed files with 97 additions and 72 deletions

View File

@@ -13,6 +13,7 @@ enum TankAction {
}
class Control {
public var type:String;
public var playerId(default, null):PlayerId;
public var tankId(default, default):Int;
private var handler:ControlHandler;

View File

@@ -19,40 +19,6 @@ interface EngineListener {
public function onDestroy(entity:EntityType, ?playerId:PlayerId):Void;
}
class CollisionProcessor {
private var engine:Engine;
public function new(engine:Engine) {
this.engine = engine;
engine.collisionSignal.connect(onCollision);
}
public function onCollision(entity:EntityType, with:EntityType):Void {
switch [entity, with] {
case [TANK(tank), TANK(other_tank)]:
tank.rect.lean(other_tank.rect);
case [TANK(tank), EAGLE(eagle)]:
tank.rect.lean(eagle.rect);
case [BULLET(bullet), BULLET(other_bullet)]:
engine.destroy(bullet);
engine.destroy(other_bullet);
case [BULLET(bullet), CELL(cell)]:
engine.destroy(bullet);
case [BULLET(bullet), EAGLE(eagle)]:
engine.destroy(bullet);
case _:
}
}
public function dispose():Void {
if (engine != null) {
engine.collisionSignal.disconnect(onCollision);
engine = null;
}
}
}
@:yield @:dispatcher(EngineListener) class Engine implements ControlHandler {
public var config(default, default):Config;
@@ -60,8 +26,6 @@ class CollisionProcessor {
public var entities(default, null):Map<Int, Entity>;
private var collision:CollisionProcessor;
private var time:Float;
public function new(config:Config) {
@@ -69,7 +33,6 @@ class CollisionProcessor {
map = new LevelMap(config.map);
entities = new Map<Int, Entity>();
time = Date.now().getTime();
collision = new CollisionProcessor(this);
}
public function spawn(entity:Entity):Void {
@@ -207,10 +170,6 @@ class CollisionProcessor {
}
public function dispose():Void {
if (collision != null) {
collision.dispose();
collision = null;
}
entities = new Map();
}

View File

@@ -22,6 +22,7 @@ interface GameListener {
public function onGameStart(state:GameState):Void;
public function onGameChange(state:GameState):Void;
public function onGameComplete(state:GameState):Void;
public function onGameEvent(event:GameEvent):Void;
}
@:dispatcher(GameListener) class Game implements EngineListener {
@@ -174,6 +175,7 @@ interface GameListener {
getPlayer(tank.playerId).control.start();
case BULLET(bullet):
getPlayer(bullet.playerId).state.shots++;
gameEventSignal.emit(GameEvent.SPAWN_BULLET(getPlayer(bullet.playerId)));
case _:
}
}
@@ -186,9 +188,20 @@ interface GameListener {
case _:
}
switch [entity, with] {
case [TANK(tank), TANK(other_tank)]:
tank.rect.lean(other_tank.rect);
case [TANK(tank), EAGLE(eagle)]:
tank.rect.lean(eagle.rect);
case [BULLET(bullet), BULLET(other_bullet)]:
engine.destroy(bullet);
engine.destroy(other_bullet);
case [BULLET(bullet), CELL(cell)]:
engine.destroy(bullet);
gameEventSignal.emit(GameEvent.HIT_CELL(getPlayer(bullet.playerId), cell));
case [TANK(tank), BONUS(bonus)]:
applyBonus(tank, bonus);
engine.destroy(bonus, tank.playerId);
gameEventSignal.emit(GameEvent.TAKE_BONUS(getPlayer(tank.playerId), bonus.config));
case [BULLET(bullet), TANK(tank)]/* | [TANK(tank), BULLET(bullet)]*/:
if (bullet.tankId == tank.id || (!engine.config.game.friendlyFire && tank.playerId.team == bullet.playerId.team)) {
// Nothing
@@ -200,10 +213,13 @@ interface GameListener {
tank.bonus = false;
spawnBonus();
}
gameEventSignal.emit(GameEvent.HIT_TANK(getPlayer(bullet.playerId), getPlayer(tank.playerId)));
} else if (tank.config.downgrade != null) {
tank.config = engine.config.getTank(tank.config.downgrade);
gameEventSignal.emit(GameEvent.HIT_TANK(getPlayer(bullet.playerId), getPlayer(tank.playerId)));
} else {
engine.destroy(tank, bullet.tank.playerId);
gameEventSignal.emit(GameEvent.DESTROY_TANK(getPlayer(bullet.playerId), getPlayer(tank.playerId)));
}
}
engine.destroy(bullet);
@@ -217,6 +233,7 @@ interface GameListener {
}
checkComplete();
gameChangeSignal.emit(state);
gameEventSignal.emit(GameEvent.DESTROY_EAGLE(getPlayer(bullet.tank.playerId), eagle.team));
}
case _:
}
@@ -274,6 +291,7 @@ interface GameListener {
gameStartSignal.dispose();
gameChangeSignal.dispose();
gameCompleteSignal.dispose();
gameEventSignal.dispose();
}
private function spawnBonus(?type:BonusType):Void {
@@ -282,6 +300,7 @@ interface GameListener {
bonus.rect.x = Math.round(Math.random() * engine.map.width / engine.map.cellWidth) * engine.map.cellWidth;
bonus.rect.y = Math.round(Math.random() * engine.map.height/ engine.map.cellHeight) * engine.map.cellHeight;
engine.spawn(bonus);
gameEventSignal.emit(GameEvent.SPAWN_BONUS(bonus.config));
}
inline private function alienTank(team:TeamId):Tank->Bool {

View File

@@ -0,0 +1,17 @@
package ru.m.tankz.game;
import ru.m.tankz.map.Grid;
import ru.m.tankz.config.Config;
import ru.m.tankz.Type;
enum GameEvent {
SPAWN_BULLET(player:Player);
HIT_TANK(player:Player, target:Player);
DESTROY_TANK(player:Player, target:Player);
DESTROY_EAGLE(player:Player, eagleTeamId:TeamId);
HIT_CELL(player:Player, cell:GridCell);
DESTROY_CELL(player:Player, cell:GridCell);
SPAWN_BONUS(bonus:BonusConfig);
TAKE_BONUS(player:Player, bonus:BonusConfig);
ADD_SCORE(player:Player, score:Int);
}