diff --git a/src/common/haxe/ru/m/tankz/core/Entity.hx b/src/common/haxe/ru/m/tankz/core/Entity.hx index 8debb0f..04dd395 100755 --- a/src/common/haxe/ru/m/tankz/core/Entity.hx +++ b/src/common/haxe/ru/m/tankz/core/Entity.hx @@ -1,20 +1,32 @@ package ru.m.tankz.core; -class Entity implements IEntity { - public var x(default, default):Float = 0; - public var y(default, default):Float = 0; +class Entity extends Point implements IEntity { - public var width(default, default):Float; - public var height(default, default):Float; - public var key(get, null):String; + public var width(default, default):Float; + public var height(default, default):Float; + public var key(get, null):String; + public var center(get, set):Point; - public function new(x:Float, y:Float) { - this.x = x; - this.y = y; - } + public function new(x:Float, y:Float) { + super(x, y); + } - private function get_key():String { - return '${Type.getClassName(Type.getClass(this))}'; - } + private function get_center():Point { + return new Point(x + width / 2, y + height / 2); + } + + private function set_center(value:Point):Point { + this.x = value.x - width / 2; + this.y = value.y - height / 2; + return value; + } + + public function getSide(direction:Direction):Point { + return center.add(new Point(direction.x * width / 2, direction.y * height / 2)); + } + + private function get_key():String { + return '${Type.getClassName(Type.getClass(this))}'; + } } diff --git a/src/common/haxe/ru/m/tankz/core/IEntity.hx b/src/common/haxe/ru/m/tankz/core/IEntity.hx index ea58e34..92eeee8 100755 --- a/src/common/haxe/ru/m/tankz/core/IEntity.hx +++ b/src/common/haxe/ru/m/tankz/core/IEntity.hx @@ -1,9 +1,11 @@ package ru.m.tankz.core; interface IEntity extends IKey { - public var x(default, default):Float; - public var y(default, default):Float; + public var x(default, default):Float; + public var y(default, default):Float; + public var center(get, set):Point; + public function getSide(direction:Direction):Point; - public var width(default, default):Float; - public var height(default, default):Float; + public var width(default, default):Float; + public var height(default, default):Float; } diff --git a/src/common/haxe/ru/m/tankz/core/Point.hx b/src/common/haxe/ru/m/tankz/core/Point.hx new file mode 100644 index 0000000..681e419 --- /dev/null +++ b/src/common/haxe/ru/m/tankz/core/Point.hx @@ -0,0 +1,15 @@ +package ru.m.tankz.core; + +class Point { + public var x(default, default):Float; + public var y(default, default):Float; + + public function new(x:Float, y:Float) { + this.x = x; + this.y = y; + } + + public function add(point:Point):Point { + return new Point(x + point.x, y + point.y); + } +} diff --git a/src/common/haxe/ru/m/tankz/engine/Engine.hx b/src/common/haxe/ru/m/tankz/engine/Engine.hx index 10b718e..8a5d786 100755 --- a/src/common/haxe/ru/m/tankz/engine/Engine.hx +++ b/src/common/haxe/ru/m/tankz/engine/Engine.hx @@ -1,5 +1,7 @@ package ru.m.tankz.engine; +import ru.m.tankz.core.Point; +import ru.m.tankz.map.Brick.BrickType; import ru.m.tankz.core.Bullet; import ru.m.tankz.proto.game.GameObjectType; import ru.m.tankz.proto.game.GameChangeType; @@ -7,7 +9,6 @@ import ru.m.tankz.proto.game.GameChange; import ru.m.tankz.proto.core.Player; import ru.m.tankz.core.Direction; import ru.m.tankz.core.IMobileEntity; -//import flash.geom.Rectangle; import ru.m.tankz.config.Config; import ru.m.tankz.core.Tank; import ru.m.tankz.map.LevelMap; @@ -15,6 +16,10 @@ import ru.m.tankz.map.ILevelMap; class Engine implements IEngine { + private static var STOP_BRICKS:Array = [ + BrickType.BRICK, BrickType.ARMOR, BrickType.WATER + ]; + public var config(default, default):Config; public var map(default, null):ILevelMap; @@ -143,49 +148,47 @@ class Engine implements IEngine { entiny.x += entiny.mx; entiny.y += entiny.my; - /*var tankR = Map Rectangle(tank.x, tank.y, tank.width, tank.height); - - for (t in tanks) if (t != tank) { - var r = Map Rectangle(t.x, t.y, t.width, t.height); - if (tankR.intersects(r)) { - if (tank.direction.x > 0) { - if (tank.x + tank.width > t.x) tank.x = t.x - tank.width; - } else if (tank.direction.x < 0) { - if (tank.x < t.x + t.width) tank.x = t.x + t.width; - } - if (tank.direction.y > 0) { - if (tank.y + tank.height > t.y) tank.y = t.y - tank.height; - } else if (tank.direction.y < 0) { - if (tank.y < t.y + t.height) tank.y = t.y + t.height; - } - } - }*/ - if (objectType == GameObjectType.TANK) { if (entiny.x < 0) entiny.x = 0; if (entiny.x + entiny.width > x_limit) entiny.x = x_limit - entiny.width; if (entiny.y < 0) entiny.y = 0; if (entiny.y + entiny.height > y_limit) entiny.y = y_limit - entiny.height; + + var target:Point = entiny.getSide(entiny.direction); + + var cellX:Int = Math.floor(target.x / map.cellWidth); + var cellY:Int = Math.floor(target.y / map.cellHeight); + + var brick1 = map.getBrick(cellX, cellY); + var brick2 = map.getBrick(cellX - entiny.direction.y * entiny.direction.y, cellY - entiny.direction.x * entiny.direction.x); + + if (brick1 != null && STOP_BRICKS.indexOf(brick1.type) > -1 || brick2 != null && STOP_BRICKS.indexOf(brick2.type) > -1) { + if (entiny.direction.x != 0) { + entiny.x = brick1.cellX * map.cellWidth + map.cellWidth / 2 - entiny.direction.x * map.cellWidth / 2 - entiny.direction.x * entiny.width / 2 - entiny.width / 2; + } + if (entiny.direction.y != 0) { + entiny.y = brick1.cellY * map.cellHeight + map.cellHeight / 2 - entiny.direction.y * map.cellHeight / 2 - entiny.direction.y * entiny.height / 2 - entiny.height / 2; + } + } } - changes.push(new GameChange() - .setType(GameChangeType.MOVED) - .setObjectType(objectType) - .setObjectId(entiny.id) - .setX(entiny.x) - .setY(entiny.y) + .setType(GameChangeType.MOVED) + .setObjectType(objectType) + .setObjectId(entiny.id) + .setX(entiny.x) + .setY(entiny.y) ); if (objectType == GameObjectType.BULLET) { if (entiny.x < 0 || entiny.x + entiny.width > x_limit || entiny.y < 0 || entiny.y + entiny.height > y_limit) { mobileEntities.remove(entiny.id); - var tank = tanks.get(personId); + var tank:Tank = tanks.get(personId); tank.onDestroyBullet(); changes.push(new GameChange() - .setType(GameChangeType.DESTROED) - .setObjectType(objectType) - .setObjectId(entiny.id) + .setType(GameChangeType.DESTROED) + .setObjectType(objectType) + .setObjectId(entiny.id) ); } } diff --git a/src/common/haxe/ru/m/tankz/map/Brick.hx b/src/common/haxe/ru/m/tankz/map/Brick.hx index d8463b7..d7f6fd2 100644 --- a/src/common/haxe/ru/m/tankz/map/Brick.hx +++ b/src/common/haxe/ru/m/tankz/map/Brick.hx @@ -26,6 +26,10 @@ class Brick implements IKey { } public function get_key():String { - return 'brick:${cellX}:${cellY}'; + return 'brick:$cellX:$cellY'; + } + + public function toString() { + return 'Brick{$type,$cellX:$cellY}'; } } diff --git a/src/common/haxe/ru/m/tankz/map/ILevelMap.hx b/src/common/haxe/ru/m/tankz/map/ILevelMap.hx index 3d38d3a..8afdedd 100755 --- a/src/common/haxe/ru/m/tankz/map/ILevelMap.hx +++ b/src/common/haxe/ru/m/tankz/map/ILevelMap.hx @@ -8,4 +8,6 @@ interface ILevelMap { public var gridHeight(default, null):Int; public var bricks(default, null):Array; + + public function getBrick(cellX:Int, cellY:Int):Brick; } diff --git a/src/common/haxe/ru/m/tankz/map/LevelMap.hx b/src/common/haxe/ru/m/tankz/map/LevelMap.hx index a20d12c..78d29a8 100755 --- a/src/common/haxe/ru/m/tankz/map/LevelMap.hx +++ b/src/common/haxe/ru/m/tankz/map/LevelMap.hx @@ -18,4 +18,9 @@ class LevelMap implements ILevelMap { gridHeight = config.gridHeight; bricks = Lambda.array(Lambda.mapi(config.bricks, function(i, type):Brick return new Brick(type, Std.int(i % gridWidth), Std.int(Math.floor(i / gridHeight))))); } + + public function getBrick(cellX:Int, cellY:Int):Brick { + return bricks[cellY * gridWidth + cellX]; + } + }