[common] added eagle spawn

This commit is contained in:
2018-01-23 20:49:54 +03:00
parent 0e809812e7
commit bcdefce2c9
6 changed files with 123 additions and 75 deletions

View File

@@ -6,6 +6,6 @@ import ru.m.geom.Rectangle;
class Eagle extends Entity {
public function new() {
super(new Rectangle(0, 0, 10, 10));
super(new Rectangle(0, 0, 44, 44));
}
}

View File

@@ -1,14 +1,17 @@
package ru.m.tankz.game;
import haxe.Timer;
import ru.m.tankz.core.EntityType;
import ru.m.tankz.game.Spawner;
import ru.m.tankz.core.Entity;
import ru.m.tankz.core.Eagle;
import ru.m.geom.Direction;
import ru.m.geom.Point;
import ru.m.tankz.config.Config;
import ru.m.tankz.control.Control;
import ru.m.tankz.core.EntityType;
import ru.m.tankz.core.Tank;
import ru.m.tankz.engine.Engine;
typedef GameType = String;
typedef TeamId = String;
@@ -19,74 +22,6 @@ typedef PlayerId = {
}
typedef SpawnTask = {
var point:SpawnPoint;
var playerId:PlayerId;
}
class Spawner {
private var config:TeamConfig;
private var runner:SpawnTask -> Void;
private var queue:Array<SpawnTask>;
private var timer:Timer;
private var indexedPoints:Map<Int, SpawnPoint>;
private var anyPoints:Array<SpawnPoint>;
private var index:Int;
public function new(config:TeamConfig, runner:SpawnTask -> Void) {
this.config = config;
this.runner = runner;
queue = [];
indexedPoints = new Map();
anyPoints = [];
for (point in config.points) {
if (point.type == 'tank') {
if (point.index > -1) {
indexedPoints.set(point.index, point);
} else {
anyPoints.push(point);
}
}
}
}
public function push(playerId:PlayerId):Void {
var point:SpawnPoint = null;
if (indexedPoints.exists(playerId.index)) {
point = indexedPoints.get(playerId.index);
} else {
point = anyPoints[index++];
if (index >= anyPoints.length) index = 0;
}
if (point != null) {
queue.push({playerId:playerId, point:point});
run();
}
}
private function run():Void {
if (timer == null) {
timer = new Timer(config.spawnInterval);
timer.run = spawn;
}
}
private function spawn():Void {
if (queue.length == 0) {
if (timer != null) {
timer.stop();
timer = null;
}
} else {
runner(queue.shift());
}
}
}
class Game implements EngineListener implements ControlListener {
public var type(default, null):GameType;
@@ -109,11 +44,15 @@ class Game implements EngineListener implements ControlListener {
private function buildTank(playerId:PlayerId, config:TankConfig, point:SpawnPoint):Tank {
var tank = new Tank(playerId, config);
tank.rect.center = new Point((point.x + 1) * engine.map.cellWidth, (point.y + 1) * engine.map.cellHeight);
tank.rect.direction = Direction.fromString(point.direction);
applyPoint(tank, point);
return tank;
}
private function applyPoint(entity:Entity, point:SpawnPoint):Void {
entity.rect.center = new Point((point.x + 1) * engine.map.cellWidth, (point.y + 1) * engine.map.cellHeight);
entity.rect.direction = Direction.fromString(point.direction);
}
public function start():Void {
teams = new Map<TeamId, Team>();
spawners = new Map<TeamId, Spawner>();
@@ -129,9 +68,14 @@ class Game implements EngineListener implements ControlListener {
for (team in teams) {
for (player in team.players) {
var point = team.config.points[0];
spawners.get(team.id).push(player.id);
}
var eaglePoint = spawners.get(team.id).getPoint('eagle');
if (eaglePoint != null) {
var eagle = new Eagle();
applyPoint(eagle, eaglePoint);
engine.spawn(eagle);
}
}
}

View File

@@ -0,0 +1,83 @@
package ru.m.tankz.game;
import haxe.Timer;
import ru.m.tankz.game.Game;
import ru.m.tankz.config.Config;
typedef SpawnTask = {
var point:SpawnPoint;
var playerId:PlayerId;
}
class Spawner {
private var config:TeamConfig;
private var runner:SpawnTask -> Void;
private var queue:Array<SpawnTask>;
private var timer:Timer;
private var indexedPoints:Map<Int, SpawnPoint>;
private var anyPoints:Array<SpawnPoint>;
private var index:Int;
public function new(config:TeamConfig, runner:SpawnTask -> Void) {
this.config = config;
this.runner = runner;
queue = [];
indexedPoints = new Map();
anyPoints = [];
for (point in config.points) {
if (point.type == 'tank') {
if (point.index > -1) {
indexedPoints.set(point.index, point);
} else {
anyPoints.push(point);
}
}
}
}
public function getPoint(type:String, index:Int=-1):Null<SpawnPoint> {
for (point in config.points) {
if (point.type == type && point.index == index) {
return point;
}
}
return null;
}
public function push(playerId:PlayerId):Void {
var point:SpawnPoint = null;
if (indexedPoints.exists(playerId.index)) {
point = indexedPoints.get(playerId.index);
} else {
point = anyPoints[index++];
if (index >= anyPoints.length) index = 0;
}
if (point != null) {
queue.push({playerId:playerId, point:point});
run();
}
}
private function run():Void {
if (timer == null) {
timer = new Timer(config.spawnInterval);
timer.run = spawn;
}
}
private function spawn():Void {
if (queue.length == 0) {
if (timer != null) {
timer.stop();
timer = null;
}
} else {
runner(queue.shift());
}
}
}