[common] added eagle spawn
This commit is contained in:
@@ -89,6 +89,11 @@ class Render extends SpriteView implements EngineListener {
|
|||||||
items.set(bullet.key, item);
|
items.set(bullet.key, item);
|
||||||
entryLayer.addChild(item.view);
|
entryLayer.addChild(item.view);
|
||||||
item.update();
|
item.update();
|
||||||
|
case EntityType.EAGLE(eagle):
|
||||||
|
var item = new EagleItem(eagle);
|
||||||
|
items.set(eagle.key, item);
|
||||||
|
entryLayer.addChild(item.view);
|
||||||
|
item.update();
|
||||||
case _:
|
case _:
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -109,6 +114,11 @@ class Render extends SpriteView implements EngineListener {
|
|||||||
entryLayer.removeChild(items.get(bullet.key).view);
|
entryLayer.removeChild(items.get(bullet.key).view);
|
||||||
items.remove(bullet.key);
|
items.remove(bullet.key);
|
||||||
}
|
}
|
||||||
|
case EntityType.EAGLE(eagle):
|
||||||
|
if (items.exists(eagle.key)) {
|
||||||
|
cast(items.get(eagle.key), EagleItem).destoyed = true;
|
||||||
|
items.get(eagle.key).redraw();
|
||||||
|
}
|
||||||
case _:
|
case _:
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
package ru.m.tankz.render;
|
package ru.m.tankz.render;
|
||||||
|
|
||||||
|
import ru.m.tankz.core.Eagle;
|
||||||
import flash.display.DisplayObject;
|
import flash.display.DisplayObject;
|
||||||
import flash.display.Shape;
|
import flash.display.Shape;
|
||||||
import ru.m.geom.Direction;
|
import ru.m.geom.Direction;
|
||||||
@@ -133,3 +134,13 @@ class BulletItem extends RenderItem<Bullet> {
|
|||||||
return 'resources/images/bullet/bullet_${value.config.piercing > 1 ? 1 : 0}.png';
|
return 'resources/images/bullet/bullet_${value.config.piercing > 1 ? 1 : 0}.png';
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
class EagleItem extends RenderItem<Eagle> {
|
||||||
|
|
||||||
|
public var destoyed(default, default):Bool;
|
||||||
|
|
||||||
|
override private function getImage():String {
|
||||||
|
return 'resources/images/eagle/eagle-${destoyed ? 1 : 0}.png';
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
@@ -44,7 +44,7 @@ teams:
|
|||||||
index: -1
|
index: -1
|
||||||
x: 12
|
x: 12
|
||||||
y: 24
|
y: 24
|
||||||
direction: top
|
direction: right
|
||||||
- type: tank
|
- type: tank
|
||||||
index: 0
|
index: 0
|
||||||
x: 8
|
x: 8
|
||||||
|
|||||||
@@ -6,6 +6,6 @@ import ru.m.geom.Rectangle;
|
|||||||
class Eagle extends Entity {
|
class Eagle extends Entity {
|
||||||
|
|
||||||
public function new() {
|
public function new() {
|
||||||
super(new Rectangle(0, 0, 10, 10));
|
super(new Rectangle(0, 0, 44, 44));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,14 +1,17 @@
|
|||||||
package ru.m.tankz.game;
|
package ru.m.tankz.game;
|
||||||
|
|
||||||
import haxe.Timer;
|
import ru.m.tankz.game.Spawner;
|
||||||
import ru.m.tankz.core.EntityType;
|
import ru.m.tankz.core.Entity;
|
||||||
|
import ru.m.tankz.core.Eagle;
|
||||||
import ru.m.geom.Direction;
|
import ru.m.geom.Direction;
|
||||||
import ru.m.geom.Point;
|
import ru.m.geom.Point;
|
||||||
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.core.EntityType;
|
||||||
import ru.m.tankz.core.Tank;
|
import ru.m.tankz.core.Tank;
|
||||||
import ru.m.tankz.engine.Engine;
|
import ru.m.tankz.engine.Engine;
|
||||||
|
|
||||||
|
|
||||||
typedef GameType = String;
|
typedef GameType = String;
|
||||||
|
|
||||||
typedef TeamId = 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 {
|
class Game implements EngineListener implements ControlListener {
|
||||||
|
|
||||||
public var type(default, null):GameType;
|
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 {
|
private function buildTank(playerId:PlayerId, config:TankConfig, point:SpawnPoint):Tank {
|
||||||
var tank = new Tank(playerId, config);
|
var tank = new Tank(playerId, config);
|
||||||
tank.rect.center = new Point((point.x + 1) * engine.map.cellWidth, (point.y + 1) * engine.map.cellHeight);
|
applyPoint(tank, point);
|
||||||
tank.rect.direction = Direction.fromString(point.direction);
|
|
||||||
return tank;
|
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 {
|
public function start():Void {
|
||||||
teams = new Map<TeamId, Team>();
|
teams = new Map<TeamId, Team>();
|
||||||
spawners = new Map<TeamId, Spawner>();
|
spawners = new Map<TeamId, Spawner>();
|
||||||
@@ -129,9 +68,14 @@ class Game implements EngineListener implements ControlListener {
|
|||||||
|
|
||||||
for (team in teams) {
|
for (team in teams) {
|
||||||
for (player in team.players) {
|
for (player in team.players) {
|
||||||
var point = team.config.points[0];
|
|
||||||
spawners.get(team.id).push(player.id);
|
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);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
83
src/common/haxe/ru/m/tankz/game/Spawner.hx
Normal file
83
src/common/haxe/ru/m/tankz/game/Spawner.hx
Normal 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());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user