[common] implement GameEvent.DESTORY.CELL

This commit is contained in:
2019-05-13 21:56:28 +03:00
parent cbb28f4158
commit 03b461f082
5 changed files with 52 additions and 38 deletions

View File

@@ -130,6 +130,9 @@ class Render extends SpriteView implements IRender {
public function onMove(entity:EntityType):Void {
}
public function onDestroy(entity:EntityType):Void {
}
public function onGameEvent(event:GameEvent):Void {
switch event {
case DESTROY(TANK(tank, who, wherewith, score)):

View File

@@ -36,10 +36,17 @@ import ru.m.tankz.map.LevelMap;
public function destroy(entityId:Int):Void {
if (entities.exists(entityId)) {
entities.remove(entityId);
var entity = entities.remove(entityId);
destroySignal.emit(EntityTypeResolver.of(entity));
}
}
public function destroyCell(x:Int, y:Int):Void {
var cell = map.grid.cells.get(new Point(x, y));
cell.destroyed = true;
destroySignal.emit(EntityTypeResolver.of(cell));
}
public function move(entityId:Int, direction:Direction):Void {
if (entities.exists(entityId)) {
cast(entities.get(entityId), MobileEntity).move(direction);
@@ -71,15 +78,6 @@ import ru.m.tankz.map.LevelMap;
}*/
if (entity.mx != 0 || entity.my != 0) {
var asTank:Tank = EntityTypeResolver.as(entity, Tank);
var asBullet:Bullet = EntityTypeResolver.as(entity, Bullet);
if (asTank != null) {
if (asTank.freezing.active) {
continue;
}
}
var side:Line = entity.rect.getSide(entity.rect.direction.reverse());
var step:Point = new Point(entity.rect.direction.x * map.cellWidth / 4, entity.rect.direction.y * map.cellHeight / 4);
var end:Point = side.center.add(new Point(entity.mx * (d / 30), entity.my * (d / 30)));
@@ -123,22 +121,6 @@ import ru.m.tankz.map.LevelMap;
}
}
if (asBullet != null) {
if (collision) {
cells = map.grid.getCells(side.setLength(map.grid.cellWidth * 3));
for (cell in cells) {
if (cell.armor > 0) {
if (cell.armor == asBullet.config.piercing) {
cell.destroyed = true;
} else if (cell.armor < asBullet.config.piercing) {
var brick = map.getBrick(cell);
brick.destroyed = true;
}
}
}
}
}
if (isStop) break;
}

View File

@@ -16,6 +16,7 @@ interface IEngine {
public var spawnSignal(default, null):Signal1<EntityType>;
public var collisionSignal(default, null):Signal2<EntityType, EntityType>;
public var moveSignal(default, null):Signal1<EntityType>;
public var destroySignal(default, null):Signal1<EntityType>;
public function spawn(entity:Entity):Void;
@@ -25,6 +26,8 @@ interface IEngine {
public function destroy(entityId:Int):Void;
public function destroyCell(x:Int, y:Int):Void;
public function update():Void;
public function iterTanks(filter:Tank->Bool):Iterator<Tank>;
@@ -40,4 +43,5 @@ interface EngineListener {
public function onSpawn(entity:EntityType):Void;
public function onCollision(entity:EntityType, with:EntityType):Void;
public function onMove(entity:EntityType):Void;
public function onDestroy(entity:EntityType):Void;
}

View File

@@ -1,5 +1,6 @@
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;
@@ -26,6 +27,7 @@ import ru.m.tankz.Type;
public var state(default, null):GameState;
private var builder:EntityBuilder;
private var timer:Timer;
@:provide var configBundle:IConfigBundle;
@@ -50,6 +52,10 @@ import ru.m.tankz.Type;
}
}
private function update():Void {
engine.update();
}
private function applyPoint(entity:Entity, point:SpawnPoint):Void {
entity.rect.center = new Point((point.x + 1) * config.map.cellWidth, (point.y + 1) * config.map.cellHeight);
entity.rect.direction = point.direction;
@@ -67,9 +73,15 @@ 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, team):
this.state = state;
this.winner = team.id;
if (timer != null) {
timer.stop();
timer = null;
}
case GameEvent.SPAWN(entityId, EAGLE(teamId, point)):
var eagle = builder.buildEagle(entityId, teamId);
applyPoint(eagle, point);
@@ -109,6 +121,8 @@ import ru.m.tankz.Type;
engine.destroy(bonus.id);
case GameEvent.DESTROY(BULLET(bullet)):
engine.destroy(bullet.id);
case GameEvent.DESTROY(CELL(cell, tank, bullet)):
engine.destroyCell(cell.cellX, cell.cellY);
case GameEvent.ACTION(tankId, MOVE(direction)):
engine.move(tankId, direction);
case GameEvent.ACTION(tankId, STOP):
@@ -118,6 +132,10 @@ import ru.m.tankz.Type;
}
public function dispose():Void {
if (timer != null) {
timer.stop();
timer = null;
}
gameEventSignal.dispose();
engine.dispose();
}

View File

@@ -1,8 +1,9 @@
package ru.m.tankz.game;
import haxe.ds.Option;
import haxe.Timer;
import haxe.ds.Option;
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;
@@ -24,7 +25,6 @@ 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;
@@ -42,10 +42,6 @@ class GameRunner implements EngineListener implements GameListener {
game.engine.disconnect(this);
}
private function updateEngine():Void {
game.engine.update();
}
private function applyPoint(entity:Entity, point:SpawnPoint):Void {
entity.rect.center = new Point((point.x + 1) * game.engine.map.cellWidth, (point.y + 1) * game.engine.map.cellHeight);
entity.rect.direction = point.direction;
@@ -72,8 +68,6 @@ class GameRunner implements EngineListener implements GameListener {
}
}
gameEventSignal.emit(GameEvent.START(state));
timer = new Timer(10);
timer.run = updateEngine;
}
public function next():Option<GameState> {
@@ -182,7 +176,9 @@ class GameRunner implements EngineListener implements GameListener {
}
public function onMove(entity:EntityType):Void {
}
public function onDestroy(entity:EntityType):Void {
}
private function spawnBonus():Void {
@@ -263,10 +259,7 @@ class GameRunner implements EngineListener implements GameListener {
case GameEvent.START(_):
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);
@@ -315,6 +308,20 @@ class GameRunner implements EngineListener implements GameListener {
case GameEvent.DESTROY(BULLET(bullet)):
var player = game.getPlayer(bullet.playerId);
player.bullets--;
var side:Line = bullet.rect.getSide(bullet.rect.direction.reverse()).move(new Point(bullet.mx, bullet.my));
var cells = game.engine.map.grid.getCells(side.setLength(game.engine.map.grid.cellWidth * 3));
for (cell in cells) {
if (cell.armor > 0) {
if (cell.armor == bullet.config.piercing) {
gameEventSignal.emit(GameEvent.DESTROY(CELL(cell, bullet.tank, bullet)));
} else if (cell.armor < bullet.config.piercing) {
var brick = game.engine.map.getBrick(cell);
for (cell in brick.cells) {
gameEventSignal.emit(GameEvent.DESTROY(CELL(cell, bullet.tank, bullet)));
}
}
}
}
case _:
}
}