[comon] remove change signal from engine

This commit is contained in:
2019-04-19 16:20:01 +03:00
parent a3f3de3af5
commit b86e0ddd20
6 changed files with 62 additions and 123 deletions

View File

@@ -42,9 +42,6 @@ class HumanControl extends Control {
} }
case _: case _:
} }
if (event.keyCode == Keyboard.U) {
action(TankAction.UPGRADE);
}
} }
private function onKeyUp(event:KeyboardEvent):Void { private function onKeyUp(event:KeyboardEvent):Void {

View File

@@ -126,8 +126,6 @@ class Render extends SpriteView {
} }
} }
public function onChange(entity:EntityType, change:EntityChange):Void {}
public function onCollision(entity:EntityType, with:EntityType):Void { public function onCollision(entity:EntityType, with:EntityType):Void {
switch [entity, with] { switch [entity, with] {
case [BULLET(_), EAGLE(eagle)]: case [BULLET(_), EAGLE(eagle)]:

View File

@@ -6,7 +6,6 @@ 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.core.EntityType;
import ru.m.tankz.engine.Engine;
import ru.m.tankz.Type; import ru.m.tankz.Type;
class SoundManager { class SoundManager {
@@ -85,22 +84,12 @@ class SoundManager {
} }
} }
public function onChange(entity:EntityType, change:EntityChange):Void {
switch [entity, change] {
case [TANK(_), HIT]:
play('bullet_hit');
//case [TANK(_), LIVE_UP]:
// play('live');
case [EAGLE(_), DEATH(_)]:
play('boom_player');
case _:
}
}
public function onCollision(entity:EntityType, with:EntityType):Void { public function onCollision(entity:EntityType, with:EntityType):Void {
switch [entity, with] { switch [entity, with] {
case [BULLET(_), CELL(cell)]: case [BULLET(_), CELL(cell)]:
//play('bullet_wall'); //play('bullet_wall');
case [BULLET(_), EAGLE(_)]:
play('boom_player');
case _: case _:
} }
} }

View File

@@ -8,7 +8,6 @@ import ru.m.tankz.Type;
enum TankAction { enum TankAction {
MOVE(direction:Direction); MOVE(direction:Direction);
UPGRADE;
STOP; STOP;
SHOT; SHOT;
} }

View File

@@ -14,20 +14,10 @@ import ru.m.tankz.map.Grid;
import ru.m.tankz.map.LevelMap; import ru.m.tankz.map.LevelMap;
import ru.m.tankz.Type; import ru.m.tankz.Type;
enum EntityChange {
HIT;
TYPE;
DEATH(playerId:PlayerId);
PROTECT;
FREEZING;
}
typedef EngineListener = { typedef EngineListener = {
public function onSpawn(entity:EntityType):Void; public function onSpawn(entity:EntityType):Void;
public function onCollision(entity:EntityType, with:EntityType):Void; public function onCollision(entity:EntityType, with:EntityType):Void;
public function onDestroy(entity:EntityType, ?playerId:PlayerId):Void; public function onDestroy(entity:EntityType, ?playerId:PlayerId):Void;
public function onChange(entity:EntityType, change:EntityChange):Void;
} }
class CollisionProcessor { class CollisionProcessor {
@@ -45,26 +35,6 @@ class CollisionProcessor {
tank.rect.lean(other_tank.rect); tank.rect.lean(other_tank.rect);
case [TANK(tank), EAGLE(eagle)]: case [TANK(tank), EAGLE(eagle)]:
tank.rect.lean(eagle.rect); tank.rect.lean(eagle.rect);
case [TANK(tank), BONUS(bonus)]:
engine.destroy(bonus, tank.playerId);
case [TANK(tank), BULLET(bullet)] |
[BULLET(bullet), TANK(tank)]:
if (bullet.tankId == tank.id || (!engine.config.game.friendlyFire && tank.playerId.team == bullet.playerId.team)) {
// Nothing
} else {
if (!tank.protect.active) {
if (tank.hits > 0) {
tank.hits--;
engine.change(tank, HIT);
} else if (tank.config.downgrade != null) {
tank.config = engine.config.getTank(tank.config.downgrade);
engine.change(tank, TYPE);
} else {
engine.destroy(tank, bullet.tank.playerId);
}
}
engine.destroy(bullet);
}
case [BULLET(bullet), BULLET(other_bullet)]: case [BULLET(bullet), BULLET(other_bullet)]:
engine.destroy(bullet); engine.destroy(bullet);
engine.destroy(other_bullet); engine.destroy(other_bullet);
@@ -72,11 +42,6 @@ class CollisionProcessor {
engine.destroy(bullet); engine.destroy(bullet);
case [BULLET(bullet), EAGLE(eagle)]: case [BULLET(bullet), EAGLE(eagle)]:
engine.destroy(bullet); engine.destroy(bullet);
if (!eagle.protect.active) {
eagle.death = true;
// ToDo: change
engine.change(eagle, DEATH(bullet.playerId));
}
case _: case _:
} }
} }
@@ -94,27 +59,23 @@ class EngineDispatcher {
public var spawnSignal(default, null):Signal1<EntityType>; public var spawnSignal(default, null):Signal1<EntityType>;
public var collisionSignal(default, null):Signal2<EntityType, EntityType>; public var collisionSignal(default, null):Signal2<EntityType, EntityType>;
public var destroySignal(default, null):Signal2<EntityType, PlayerId>; public var destroySignal(default, null):Signal2<EntityType, PlayerId>;
public var changeSignal(default, null):Signal2<EntityType, EntityChange>;
public function new() { public function new() {
spawnSignal = new Signal1<EntityType>(); spawnSignal = new Signal1<EntityType>();
collisionSignal = new Signal2<EntityType, EntityType>(); collisionSignal = new Signal2<EntityType, EntityType>();
destroySignal = new Signal2<EntityType, PlayerId>(); destroySignal = new Signal2<EntityType, PlayerId>();
changeSignal = new Signal2<EntityType, EntityChange>();
} }
public function connect(listener:EngineListener) { public function connect(listener:EngineListener) {
spawnSignal.connect(listener.onSpawn); spawnSignal.connect(listener.onSpawn);
collisionSignal.connect(listener.onCollision); collisionSignal.connect(listener.onCollision);
destroySignal.connect(listener.onDestroy); destroySignal.connect(listener.onDestroy);
changeSignal.connect(listener.onChange);
} }
public function disconnect(listener:EngineListener) { public function disconnect(listener:EngineListener) {
spawnSignal.disconnect(listener.onSpawn); spawnSignal.disconnect(listener.onSpawn);
collisionSignal.disconnect(listener.onCollision); collisionSignal.disconnect(listener.onCollision);
destroySignal.disconnect(listener.onDestroy); destroySignal.disconnect(listener.onDestroy);
changeSignal.disconnect(listener.onChange);
} }
} }
@@ -165,11 +126,6 @@ class Engine extends EngineDispatcher implements ControlHandler {
switch (action) { switch (action) {
case MOVE(direction): case MOVE(direction):
tank.move(direction); tank.move(direction);
case UPGRADE:
if (tank.config.upgrade != null) {
tank.config = config.getTank(tank.config.upgrade);
change(tank, TYPE);
}
case STOP: case STOP:
tank.stop(); tank.stop();
case SHOT: case SHOT:
@@ -180,11 +136,6 @@ class Engine extends EngineDispatcher implements ControlHandler {
} }
} }
public function change(entity:Entity, change:EntityChange):Void {
var type = EntityTypeResolver.of(entity);
changeSignal.emit(type, change);
}
public function update():Void { public function update():Void {
var newTime:Float = Date.now().getTime(); var newTime:Float = Date.now().getTime();
var d:Float = newTime - time; var d:Float = newTime - time;

View File

@@ -1,6 +1,5 @@
package ru.m.tankz.game; package ru.m.tankz.game;
import ru.m.tankz.control.Controller.AController;
import haxe.ds.Option; import haxe.ds.Option;
import haxe.Timer; import haxe.Timer;
import haxework.signal.Signal; import haxework.signal.Signal;
@@ -8,6 +7,7 @@ import ru.m.geom.Point;
import ru.m.tankz.bundle.IConfigBundle; import ru.m.tankz.bundle.IConfigBundle;
import ru.m.tankz.config.Config; import ru.m.tankz.config.Config;
import ru.m.tankz.control.Control; import ru.m.tankz.control.Control;
import ru.m.tankz.control.Controller;
import ru.m.tankz.control.IControlFactory; import ru.m.tankz.control.IControlFactory;
import ru.m.tankz.core.Bonus; import ru.m.tankz.core.Bonus;
import ru.m.tankz.core.Eagle; import ru.m.tankz.core.Eagle;
@@ -49,7 +49,7 @@ class GameDispatcher {
class Game extends GameDispatcher { class Game extends GameDispatcher {
private static var TAG(default, never):String = 'Game'; private static var TAG(default, never):String = "Game";
public var type(default, null):GameType; public var type(default, null):GameType;
public var teams(default, null):Map<TeamId, Team>; public var teams(default, null):Map<TeamId, Team>;
@@ -133,7 +133,7 @@ class Game extends GameDispatcher {
team.spawner.push(player.id, player.state.tank); team.spawner.push(player.id, player.state.tank);
} }
} }
var eaglePoint = team.spawner.getPoint('eagle'); var eaglePoint = team.spawner.getPoint("eagle");
if (eaglePoint != null) { if (eaglePoint != null) {
var eagle = new Eagle(team.id, team.config.eagle); var eagle = new Eagle(team.id, team.config.eagle);
eagle.color = config.getColor(new PlayerId(eagle.team, -1)); eagle.color = config.getColor(new PlayerId(eagle.team, -1));
@@ -202,25 +202,6 @@ class Game extends GameDispatcher {
} }
} }
public function onChange(entity:EntityType, change:EntityChange):Void {
switch [entity, change] {
case [EAGLE(eagle), DEATH(playerId)]:
if (eagle.death) {
getPlayer(playerId).state.score += eagle.score * (eagle.team == playerId.team ? 0 : 1);
checkComplete();
onGameChange.emit(state);
}
case [TANK(tank), HIT]:
if (tank.bonus) {
tank.bonus = false;
spawnBonus();
}
case [TANK(tank), TYPE]:
getPlayer(tank.playerId).state.tank = tank.config.type;
case _:
}
}
public function onCollision(entity:EntityType, with:EntityType):Void { public function onCollision(entity:EntityType, with:EntityType):Void {
switch entity { switch entity {
case EntityType.TANK(tank): case EntityType.TANK(tank):
@@ -231,8 +212,36 @@ class Game extends GameDispatcher {
switch [entity, with] { switch [entity, with] {
case [TANK(tank), BONUS(bonus)]: case [TANK(tank), BONUS(bonus)]:
applyBonus(tank, bonus); applyBonus(tank, bonus);
case [BULLET(bullet), TANK(tank)]: engine.destroy(bonus, tank.playerId);
getPlayer(bullet.playerId).state.hits++; 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
} else {
if (!tank.protect.active) {
if (tank.hits > 0) {
tank.hits--;
if (tank.bonus) {
tank.bonus = false;
spawnBonus();
}
} else if (tank.config.downgrade != null) {
tank.config = engine.config.getTank(tank.config.downgrade);
} else {
engine.destroy(tank, bullet.tank.playerId);
}
}
engine.destroy(bullet);
}
case [BULLET(bullet), EAGLE(eagle)]:
engine.destroy(bullet);
if (!eagle.protect.active) {
eagle.death = true;
if (bullet.playerId.team != eagle.team) {
getPlayer(bullet.playerId).state.score += eagle.score;
}
checkComplete();
onGameChange.emit(state);
}
case _: case _:
} }
} }
@@ -260,11 +269,6 @@ class Game extends GameDispatcher {
getPlayer(playerId).state.score += tank.config.score * (tank.playerId.team == playerId.team ? 0 : 1); getPlayer(playerId).state.score += tank.config.score * (tank.playerId.team == playerId.team ? 0 : 1);
} }
onGameChange.emit(state); onGameChange.emit(state);
case BONUS(bonus):
if (bonus.config.score > 0 && playerId != null) {
getPlayer(playerId).state.score += bonus.config.score;
}
onGameChange.emit(state);
case _: case _:
} }
} }
@@ -310,48 +314,49 @@ class Game extends GameDispatcher {
private function applyBonus(tank:Tank, bonus:Bonus):Void { private function applyBonus(tank:Tank, bonus:Bonus):Void {
switch (bonus.config.type) { switch (bonus.config.type) {
case 'life': case "life":
getPlayer(tank.playerId).state.life++; getPlayer(tank.playerId).state.life++;
case 'star': case "star":
if (tank.config.upgrade != null) { upgradeTank(tank);
tank.config = config.getTank(tank.config.upgrade); case "grenade":
engine.change(tank, EntityChange.TYPE);
} else {
tank.hits++;
engine.change(tank, EntityChange.HIT);
}
case 'grenade':
for (t in engine.iterTanks(alienTank(tank.playerId.team))) { for (t in engine.iterTanks(alienTank(tank.playerId.team))) {
engine.destroy(t); engine.destroy(t);
} }
case 'helmet': case "helmet":
tank.protect.on(bonus.config.duration); tank.protect.on(bonus.config.duration);
engine.change(tank, EntityChange.PROTECT); case "clock":
case 'clock':
for (t in engine.iterTanks(alienTank(tank.playerId.team))) { for (t in engine.iterTanks(alienTank(tank.playerId.team))) {
t.freezing.on(bonus.config.duration); t.freezing.on(bonus.config.duration);
engine.change(t, EntityChange.FREEZING);
} }
case 'shovel': case "shovel":
// ToDo: protect eagle/area // ToDo: protect eagle/area
var team:Team = teams[tank.playerId.team]; var team:Team = teams[tank.playerId.team];
if (team.eagleId > 0) { if (team.eagleId > 0) {
var eagle:Eagle = cast(engine.entities[team.eagleId], Eagle); var eagle:Eagle = cast(engine.entities[team.eagleId], Eagle);
eagle.protect.on(bonus.config.duration); eagle.protect.on(bonus.config.duration);
engine.change(eagle, EntityChange.PROTECT);
}
case 'gun':
if (tank.config.upgrade != null) {
while (tank.config.upgrade != null) {
tank.config = config.getTank(tank.config.upgrade);
}
engine.change(tank, EntityChange.TYPE);
} else {
tank.hits++;
engine.change(tank, EntityChange.HIT);
} }
case "gun":
upgradeTank(tank, 5);
case _: case _:
engine.destroy(tank); // :-D engine.destroy(tank); // :-D
} }
if (bonus.config.score > 0) {
getPlayer(tank.playerId).state.score += bonus.config.score;
}
onGameChange.emit(state);
}
public function upgradeTank(tank:Tank, level:Int = 1):Void {
if (tank.config.upgrade != null) {
while (level-- > 0 && tank.config.upgrade != null) {
tank.config = config.getTank(tank.config.upgrade);
}
} else {
tank.hits++;
}
}
public function hitTank(tank:Tank):Void {
} }
} }