[common] tank with bricks collision
This commit is contained in:
@@ -1,20 +1,32 @@
|
|||||||
package ru.m.tankz.core;
|
package ru.m.tankz.core;
|
||||||
|
|
||||||
class Entity implements IEntity {
|
|
||||||
|
|
||||||
public var x(default, default):Float = 0;
|
class Entity extends Point implements IEntity {
|
||||||
public var y(default, default):Float = 0;
|
|
||||||
|
|
||||||
public var width(default, default):Float;
|
public var width(default, default):Float;
|
||||||
public var height(default, default):Float;
|
public var height(default, default):Float;
|
||||||
public var key(get, null):String;
|
public var key(get, null):String;
|
||||||
|
public var center(get, set):Point;
|
||||||
|
|
||||||
public function new(x:Float, y:Float) {
|
public function new(x:Float, y:Float) {
|
||||||
this.x = x;
|
super(x, y);
|
||||||
this.y = y;
|
}
|
||||||
}
|
|
||||||
|
|
||||||
private function get_key():String {
|
private function get_center():Point {
|
||||||
return '${Type.getClassName(Type.getClass(this))}';
|
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))}';
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,9 +1,11 @@
|
|||||||
package ru.m.tankz.core;
|
package ru.m.tankz.core;
|
||||||
|
|
||||||
interface IEntity extends IKey {
|
interface IEntity extends IKey {
|
||||||
public var x(default, default):Float;
|
public var x(default, default):Float;
|
||||||
public var y(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 width(default, default):Float;
|
||||||
public var height(default, default):Float;
|
public var height(default, default):Float;
|
||||||
}
|
}
|
||||||
|
|||||||
15
src/common/haxe/ru/m/tankz/core/Point.hx
Normal file
15
src/common/haxe/ru/m/tankz/core/Point.hx
Normal file
@@ -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);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,5 +1,7 @@
|
|||||||
package ru.m.tankz.engine;
|
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.core.Bullet;
|
||||||
import ru.m.tankz.proto.game.GameObjectType;
|
import ru.m.tankz.proto.game.GameObjectType;
|
||||||
import ru.m.tankz.proto.game.GameChangeType;
|
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.proto.core.Player;
|
||||||
import ru.m.tankz.core.Direction;
|
import ru.m.tankz.core.Direction;
|
||||||
import ru.m.tankz.core.IMobileEntity;
|
import ru.m.tankz.core.IMobileEntity;
|
||||||
//import flash.geom.Rectangle;
|
|
||||||
import ru.m.tankz.config.Config;
|
import ru.m.tankz.config.Config;
|
||||||
import ru.m.tankz.core.Tank;
|
import ru.m.tankz.core.Tank;
|
||||||
import ru.m.tankz.map.LevelMap;
|
import ru.m.tankz.map.LevelMap;
|
||||||
@@ -15,6 +16,10 @@ import ru.m.tankz.map.ILevelMap;
|
|||||||
|
|
||||||
class Engine implements IEngine {
|
class Engine implements IEngine {
|
||||||
|
|
||||||
|
private static var STOP_BRICKS:Array<BrickType> = [
|
||||||
|
BrickType.BRICK, BrickType.ARMOR, BrickType.WATER
|
||||||
|
];
|
||||||
|
|
||||||
public var config(default, default):Config;
|
public var config(default, default):Config;
|
||||||
public var map(default, null):ILevelMap;
|
public var map(default, null):ILevelMap;
|
||||||
|
|
||||||
@@ -143,49 +148,47 @@ class Engine implements IEngine {
|
|||||||
entiny.x += entiny.mx;
|
entiny.x += entiny.mx;
|
||||||
entiny.y += entiny.my;
|
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 (objectType == GameObjectType.TANK) {
|
||||||
if (entiny.x < 0) entiny.x = 0;
|
if (entiny.x < 0) entiny.x = 0;
|
||||||
if (entiny.x + entiny.width > x_limit) entiny.x = x_limit - entiny.width;
|
if (entiny.x + entiny.width > x_limit) entiny.x = x_limit - entiny.width;
|
||||||
if (entiny.y < 0) entiny.y = 0;
|
if (entiny.y < 0) entiny.y = 0;
|
||||||
if (entiny.y + entiny.height > y_limit) entiny.y = y_limit - entiny.height;
|
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()
|
changes.push(new GameChange()
|
||||||
.setType(GameChangeType.MOVED)
|
.setType(GameChangeType.MOVED)
|
||||||
.setObjectType(objectType)
|
.setObjectType(objectType)
|
||||||
.setObjectId(entiny.id)
|
.setObjectId(entiny.id)
|
||||||
.setX(entiny.x)
|
.setX(entiny.x)
|
||||||
.setY(entiny.y)
|
.setY(entiny.y)
|
||||||
);
|
);
|
||||||
|
|
||||||
if (objectType == GameObjectType.BULLET) {
|
if (objectType == GameObjectType.BULLET) {
|
||||||
if (entiny.x < 0 || entiny.x + entiny.width > x_limit || entiny.y < 0 || entiny.y + entiny.height > y_limit) {
|
if (entiny.x < 0 || entiny.x + entiny.width > x_limit || entiny.y < 0 || entiny.y + entiny.height > y_limit) {
|
||||||
mobileEntities.remove(entiny.id);
|
mobileEntities.remove(entiny.id);
|
||||||
var tank = tanks.get(personId);
|
var tank:Tank = tanks.get(personId);
|
||||||
tank.onDestroyBullet();
|
tank.onDestroyBullet();
|
||||||
changes.push(new GameChange()
|
changes.push(new GameChange()
|
||||||
.setType(GameChangeType.DESTROED)
|
.setType(GameChangeType.DESTROED)
|
||||||
.setObjectType(objectType)
|
.setObjectType(objectType)
|
||||||
.setObjectId(entiny.id)
|
.setObjectId(entiny.id)
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -26,6 +26,10 @@ class Brick implements IKey {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public function get_key():String {
|
public function get_key():String {
|
||||||
return 'brick:${cellX}:${cellY}';
|
return 'brick:$cellX:$cellY';
|
||||||
|
}
|
||||||
|
|
||||||
|
public function toString() {
|
||||||
|
return 'Brick{$type,$cellX:$cellY}';
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -8,4 +8,6 @@ interface ILevelMap {
|
|||||||
public var gridHeight(default, null):Int;
|
public var gridHeight(default, null):Int;
|
||||||
|
|
||||||
public var bricks(default, null):Array<Brick>;
|
public var bricks(default, null):Array<Brick>;
|
||||||
|
|
||||||
|
public function getBrick(cellX:Int, cellY:Int):Brick;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -18,4 +18,9 @@ class LevelMap implements ILevelMap {
|
|||||||
gridHeight = config.gridHeight;
|
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)))));
|
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];
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user