[common] tank with bricks collision
This commit is contained in:
@@ -1,17 +1,29 @@
|
||||
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 center(get, set):Point;
|
||||
|
||||
public function new(x:Float, y:Float) {
|
||||
this.x = x;
|
||||
this.y = y;
|
||||
super(x, y);
|
||||
}
|
||||
|
||||
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 {
|
||||
|
||||
@@ -3,6 +3,8 @@ package ru.m.tankz.core;
|
||||
interface IEntity extends IKey {
|
||||
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;
|
||||
|
||||
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;
|
||||
|
||||
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> = [
|
||||
BrickType.BRICK, BrickType.ARMOR, BrickType.WATER
|
||||
];
|
||||
|
||||
public var config(default, default):Config;
|
||||
public var map(default, null):ILevelMap;
|
||||
|
||||
@@ -143,31 +148,29 @@ 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)
|
||||
@@ -180,7 +183,7 @@ class Engine implements IEngine {
|
||||
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)
|
||||
|
||||
@@ -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}';
|
||||
}
|
||||
}
|
||||
|
||||
@@ -8,4 +8,6 @@ interface ILevelMap {
|
||||
public var gridHeight(default, null):Int;
|
||||
|
||||
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;
|
||||
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