[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

@@ -5,11 +5,12 @@ import flash.media.Sound;
import flash.media.SoundChannel; import flash.media.SoundChannel;
import flash.media.SoundTransform; import flash.media.SoundTransform;
import openfl.utils.Assets; import openfl.utils.Assets;
import ru.m.tankz.core.EntityType; import ru.m.tankz.control.HumanControl;
import ru.m.tankz.engine.Engine; import ru.m.tankz.game.Game;
import ru.m.tankz.Type; import ru.m.tankz.game.GameEvent;
import ru.m.tankz.game.GameState;
class SoundManager implements EngineListener { class SoundManager implements GameListener {
private static var TAG(default, never):String = 'SoundManager'; private static var TAG(default, never):String = 'SoundManager';
#if flash #if flash
@@ -75,32 +76,42 @@ class SoundManager implements EngineListener {
channels.remove(event.currentTarget); channels.remove(event.currentTarget);
} }
public function onSpawn(entity:EntityType):Void { public function onGameStart(state:GameState):Void {
switch entity { play('start');
case BULLET(_.tank.playerId.team => 'human'): }
public function onGameChange(state:GameState):Void {
}
public function onGameComplete(state:GameState):Void {
}
public function onGameEvent(event:GameEvent):Void {
switch event {
case SPAWN_BULLET(player):
// ToDo:
if (Std.is(player.control, HumanControl)) {
play('shot'); play('shot');
case BONUS(_):
play('bonus_add');
case _:
} }
} case HIT_TANK(player, target):
play('bullet_hit');
public function onCollision(entity:EntityType, with:EntityType):Void { case DESTROY_TANK(player, target):
switch [entity, with] { // ToDo:
case [BULLET(_), CELL(cell)]: if (Std.is(target.control, HumanControl)) {
//play('bullet_wall');
case [BULLET(_), EAGLE(_)]:
play('boom_player'); play('boom_player');
case _: } else {
}
}
public function onDestroy(entity:EntityType, ?playerId:PlayerId):Void {
switch entity {
case TANK(_):
play('boom_bot'); play('boom_bot');
case BONUS(_): }
case DESTROY_EAGLE(player, eagleTeamId):
play('boom_player');
case SPAWN_BONUS(bonus):
play('bonus');
case TAKE_BONUS(player, bonus):
if (bonus.type == 'life') {
play('live');
} else {
play('bonus_get'); play('bonus_get');
}
case _: case _:
} }
} }

View File

@@ -1,5 +1,6 @@
package ru.m.tankz.view.classic; package ru.m.tankz.view.classic;
import ru.m.tankz.game.GameEvent;
import haxework.view.LabelView; import haxework.view.LabelView;
import haxework.view.VGroupView; import haxework.view.VGroupView;
import ru.m.tankz.game.GameState; import ru.m.tankz.game.GameState;
@@ -38,4 +39,8 @@ import ru.m.tankz.view.common.LifeView;
public function onGameComplete(state:GameState):Void { public function onGameComplete(state:GameState):Void {
} }
public function onGameEvent(event:GameEvent):Void {
}
} }

View File

@@ -6,6 +6,7 @@ import haxe.Timer;
import haxework.view.frame.FrameSwitcher; import haxework.view.frame.FrameSwitcher;
import haxework.view.GroupView; import haxework.view.GroupView;
import ru.m.tankz.game.Game; import ru.m.tankz.game.Game;
import ru.m.tankz.game.GameEvent;
import ru.m.tankz.game.GameState; import ru.m.tankz.game.GameState;
import ru.m.tankz.network.NetworkManager; import ru.m.tankz.network.NetworkManager;
import ru.m.tankz.render.Render; import ru.m.tankz.render.Render;
@@ -45,7 +46,7 @@ class GameFrame extends GroupView implements GameListener {
private function start(state:GameState):Void { private function start(state:GameState):Void {
game = new Game(state.type); game = new Game(state.type);
game.engine.connect(render); game.engine.connect(render);
game.engine.connect(soundManager); game.connect(soundManager);
game.connect(this); game.connect(this);
if (panel != null) { if (panel != null) {
game.connect(panel); game.connect(panel);
@@ -55,7 +56,6 @@ class GameFrame extends GroupView implements GameListener {
timer.run = updateEngine; timer.run = updateEngine;
content.addEventListener(Event.ENTER_FRAME, _redraw); content.addEventListener(Event.ENTER_FRAME, _redraw);
render.draw(game.engine); render.draw(game.engine);
soundManager.play('start');
} }
private function stop():Void { private function stop():Void {
@@ -90,6 +90,9 @@ class GameFrame extends GroupView implements GameListener {
switcher.change(ResultFrame.ID); switcher.change(ResultFrame.ID);
} }
public function onGameEvent(event:GameEvent):Void {
}
public function onHide():Void { public function onHide():Void {
stop(); stop();
} }

View File

@@ -1,10 +1,11 @@
package ru.m.tankz.view.death; package ru.m.tankz.view.death;
import ru.m.tankz.game.GameState;
import haxework.view.DataView; import haxework.view.DataView;
import haxework.view.LabelView; import haxework.view.LabelView;
import haxework.view.VGroupView; import haxework.view.VGroupView;
import ru.m.tankz.game.GameEvent;
import ru.m.tankz.game.GameState.PlayerState; import ru.m.tankz.game.GameState.PlayerState;
import ru.m.tankz.game.GameState;
import ru.m.tankz.view.common.IGamePanel; import ru.m.tankz.view.common.IGamePanel;
import ru.m.tankz.view.common.LifeView; import ru.m.tankz.view.common.LifeView;
@@ -27,4 +28,8 @@ import ru.m.tankz.view.common.LifeView;
public function onGameComplete(state:GameState):Void { public function onGameComplete(state:GameState):Void {
} }
public function onGameEvent(event:GameEvent):Void {
}
} }

View File

@@ -1,9 +1,10 @@
package ru.m.tankz.view.dota; package ru.m.tankz.view.dota;
import ru.m.tankz.preset.DotaGame;
import haxework.view.HGroupView; import haxework.view.HGroupView;
import haxework.view.LabelView; import haxework.view.LabelView;
import ru.m.tankz.game.GameEvent;
import ru.m.tankz.game.GameState; import ru.m.tankz.game.GameState;
import ru.m.tankz.preset.DotaGame;
import ru.m.tankz.view.common.IGamePanel; import ru.m.tankz.view.common.IGamePanel;
import ru.m.tankz.view.common.LifeView; import ru.m.tankz.view.common.LifeView;
@@ -27,4 +28,8 @@ import ru.m.tankz.view.common.LifeView;
public function onGameComplete(state:GameState):Void { public function onGameComplete(state:GameState):Void {
} }
public function onGameEvent(event:GameEvent):Void {
}
} }

View File

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

View File

@@ -19,40 +19,6 @@ interface EngineListener {
public function onDestroy(entity:EntityType, ?playerId:PlayerId):Void; 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 { @:yield @:dispatcher(EngineListener) class Engine implements ControlHandler {
public var config(default, default):Config; public var config(default, default):Config;
@@ -60,8 +26,6 @@ class CollisionProcessor {
public var entities(default, null):Map<Int, Entity>; public var entities(default, null):Map<Int, Entity>;
private var collision:CollisionProcessor;
private var time:Float; private var time:Float;
public function new(config:Config) { public function new(config:Config) {
@@ -69,7 +33,6 @@ class CollisionProcessor {
map = new LevelMap(config.map); map = new LevelMap(config.map);
entities = new Map<Int, Entity>(); entities = new Map<Int, Entity>();
time = Date.now().getTime(); time = Date.now().getTime();
collision = new CollisionProcessor(this);
} }
public function spawn(entity:Entity):Void { public function spawn(entity:Entity):Void {
@@ -207,10 +170,6 @@ class CollisionProcessor {
} }
public function dispose():Void { public function dispose():Void {
if (collision != null) {
collision.dispose();
collision = null;
}
entities = new Map(); entities = new Map();
} }

View File

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