[common] abstract Direction

This commit is contained in:
2019-03-05 16:39:03 +03:00
parent d2383fc4f0
commit d4a9c720d6
11 changed files with 77 additions and 164 deletions

View File

@@ -1,47 +1,72 @@
package ru.m.geom;
import haxe.ds.IntMap;
abstract Direction(Array<Int>) {
private static var directions:Map<Int, Direction> = new Map();
class Direction {
private static var directions:IntMap<Direction> = new IntMap<Direction>();
public static var LEFT(default, never) = Direction.from(-1, 0);
public static var TOP(default, never) = Direction.from(0, -1);
public static var RIGHT(default, never) = Direction.from(1, 0);
public static var BOTTOM(default, never) = Direction.from(0, 1);
public static var LEFT(default, never) = new Direction(-1, 0);
public static var TOP(default, never) = new Direction(0, -1);
public static var RIGHT(default, never) = new Direction(1, 0);
public static var BOTTOM(default, never) = new Direction(0, 1);
public var x(get, never):Int;
public var y(get, never):Int;
public var angle(get, never):Int;
public var x(default, null):Int;
public var y(default, null):Int;
private function new(x, y) {
this.x = x;
this.y = y;
directions.set(x + y * 10, this);
inline public function new(value:Array<Int>) {
this = value;
}
public function reverse():Direction {
return from(-x, -y);
inline private function get_x():Int return this[0];
inline private function get_y():Int return this[1];
inline private function get_angle():Int {
return switch this {
case [-1, 0]: 180;
case [0, -1]: 270;
case [1, 0]: 0;
case [0, 1]: 90;
case _: 0;
}
}
public function equals(other:Direction):Bool {
return other.x == x && other.y == y;
}
public static function from(x:Int, y:Int):Direction {
return directions.get(x + y * 10);
public function reverse():Direction {
return from(-x, -y);
}
@:from static public function fromString(s:String):Direction {
return switch(s) {
case 'left': LEFT;
case 'top': TOP;
case 'right': RIGHT;
case 'bottom': BOTTOM;
@:from inline static public function fromArray(value:Array<Int>):Direction {
return new Direction(value);
}
@:from inline static public function fromString(value:String):Direction {
return switch value.toUpperCase() {
case "LEFT": LEFT;
case "TOP": TOP;
case "RIGHT": RIGHT;
case "BOTTOM": BOTTOM;
case _: null;
}
}
public function toString():String {
return 'Direction{x=$x,y=$y}';
@:to inline public function toString():String {
return (switch this {
case [-1, 0]: "LEFT";
case [0, -1]: "TOP";
case [1, 0]: "RIGHT";
case [0, 1]: "BOTTOM";
case _: null;
});
}
inline static public function from(x:Int, y:Int):Direction {
var key = x + y * 10;
if (!directions.exists(key)) {
directions.set(key, new Direction([x, y]));
}
return directions.get(key);
}
}

View File

@@ -5,7 +5,6 @@ import ru.m.tankz.core.Entity;
import ru.m.tankz.core.EntityType;
import ru.m.tankz.Type;
enum TankAction {
MOVE(direction:Direction);
UPGRADE;

View File

@@ -78,7 +78,7 @@ class Game {
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);
entity.rect.direction = point.direction;
}
public function start(state:GameState):Stream<GameState> {