[render] spawn eagle from GameEvent

This commit is contained in:
2019-05-16 21:25:53 +03:00
parent c1ff14111d
commit efe826110c
8 changed files with 95 additions and 39 deletions

View File

@@ -1,24 +1,58 @@
package ru.m.geom;
class Rectangle {
public var x(default, default):Float;
public var y(default, default):Float;
public var width(default, default):Float;
public var height(default, default):Float;
abstract Rectangle(Array<Float>) {
public var x(get, set):Float;
public var y(get, set):Float;
public var width(get, set):Float;
public var height(get, set):Float;
public var center(get, set):Point;
public var direction(default, set):Direction;
public var left(get, null):Float;
public var right(get, null):Float;
public var top(get, null):Float;
public var bottom(get, null):Float;
public var position(get, null):Position;
public var direction(get, set):Direction;
public var left(get, never):Float;
public var right(get, never):Float;
public var top(get, never):Float;
public var bottom(get, never):Float;
public var position(get, set):Position;
public function new(x:Float, y:Float, width:Float, height:Float) {
this.x = x;
this.y = y;
this.width = width;
this.height = height;
this.direction = Direction.RIGHT;
public function new(x:Float = 0, y:Float = 0, width:Float = 0, height:Float = 0, direction:Direction = null) {
if (direction == null) {
direction = Direction.RIGHT;
}
this = [
x, y, width, height,
direction.x, direction.y,
];
}
private inline function get_x():Float {
return this[0];
}
private inline function set_x(value:Float):Float {
return this[0] = value;
}
private inline function get_y():Float {
return this[1];
}
private inline function set_y(value:Float):Float {
return this[1] = value;
}
private inline function get_width():Float {
return this[2];
}
private inline function set_width(value:Float):Float {
return this[2] = value;
}
private inline function get_height():Float {
return this[3];
}
private inline function set_height(value:Float):Float {
return this[3] = value;
}
private function get_center():Point {
@@ -26,8 +60,8 @@ class Rectangle {
}
private function set_center(value:Point):Point {
this.x = value.x - width / 2;
this.y = value.y - height / 2;
x = value.x - width / 2;
y = value.y - height / 2;
return value;
}
@@ -49,6 +83,10 @@ class Rectangle {
}
}
private function get_direction():Direction {
return [Std.int(this[4]), Std.int(this[5])];
}
public function set_direction(value:Direction):Direction {
if (direction != value) {
if (direction != null && direction.x * value.x == 0 && direction.y * value.y == 0) {
@@ -57,7 +95,8 @@ class Rectangle {
width = h;
height = w;
}
direction = value;
this[4] = value.x;
this[5] = value.y;
}
return direction;
}
@@ -96,23 +135,30 @@ class Rectangle {
return 'Rectangle{x=$x,y=$y,width=$width,height=$height}';
}
function get_left():Float {
private function get_left():Float {
return x;
}
function get_right():Float {
private function get_right():Float {
return x + width;
}
function get_top():Float {
private function get_top():Float {
return y;
}
function get_bottom():Float {
private function get_bottom():Float {
return y + height;
}
private function get_position():Position {
return {x:x, y:y, direction:direction};
}
private function set_position(position:Position):Position {
x = position.x;
y = position.y;
direction = position.direction;
return position;
}
}

View File

@@ -15,8 +15,8 @@ class Eagle extends Entity {
public var score(get, null):Int;
public function new(id:Int, team:TeamId, config:EagleConfig) {
super(id, new Rectangle(0, 0, 44, 44)); // ToDo: hardcode size
public function new(id:Int, rect:Rectangle, team:TeamId, config:EagleConfig) {
super(id, rect);
this.team = team;
this.config = config;
this.death = false;

View File

@@ -1,5 +1,6 @@
package ru.m.tankz.game;
import ru.m.geom.Rectangle;
import ru.m.tankz.config.Config;
import ru.m.tankz.core.Bonus;
import ru.m.tankz.core.Bullet;
@@ -15,10 +16,9 @@ class EntityBuilder {
this.config = config;
}
public function buildEagle(id:Int, teamId:TeamId):Eagle {
public function buildEagle(id:Int, rect:Rectangle, teamId:TeamId):Eagle {
var eageleConfig = config.getTeam(teamId).eagle;
var eaglePoint = config.getPoint(teamId, "eagle");
var eagle = new Eagle(id, teamId, eageleConfig);
var eagle = new Eagle(id, rect, teamId, eageleConfig);
eagle.color = config.getColor(new PlayerId(teamId, -1));
return eagle;
}

View File

@@ -74,9 +74,8 @@ import ru.m.tankz.Type;
case GameEvent.COMPLETE(state, winnerId):
this.state = state;
this.winner = winnerId;
case GameEvent.SPAWN(EAGLE(id, position, teamId)):
var eagle = builder.buildEagle(id, teamId);
applyPosition(eagle, position);
case GameEvent.SPAWN(EAGLE(id, rect, teamId)):
var eagle = builder.buildEagle(id, rect, teamId);
var team = getTeam(teamId);
team.eagleId = eagle.id;
engine.spawn(eagle);

View File

@@ -1,11 +1,12 @@
package ru.m.tankz.game;
import ru.m.geom.Position;
import ru.m.geom.Rectangle;
import ru.m.tankz.control.Control;
import ru.m.tankz.Type;
enum SpawnEvent {
EAGLE(id:Int, position:Position, teamId:TeamId);
EAGLE(id:Int, rect:Rectangle, teamId:TeamId);
TANK(id:Int, position:Position, playerId:PlayerId, type:TankType);
BULLET(id:Int, position:Position, playerId:PlayerId);
BONUS(id:Int, position:Position, type:BonusType);

View File

@@ -1,5 +1,6 @@
package ru.m.tankz.game;
import ru.m.geom.Rectangle;
import ru.m.geom.Direction;
import haxe.ds.Option;
import haxe.Timer;
@@ -78,7 +79,10 @@ class GameRunner implements EngineListener implements GameListener {
}
if (team.config.eagle != null) {
var point = game.config.getPoint(team.id, "eagle");
gameEventSignal.emit(GameEvent.SPAWN(EAGLE(++entityId, pointToPosition(point), team.id)));
var position = pointToPosition(point);
var rect = new Rectangle(0, 0, 44, 44);
rect.center = new Point(position.x, position.y);
gameEventSignal.emit(GameEvent.SPAWN(EAGLE(++entityId, rect, team.id)));
}
}
gameEventSignal.emit(GameEvent.START(state));