[common] rect intersection
This commit is contained in:
@@ -7,6 +7,10 @@ class Rectangle {
|
||||
public var height(default, default):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 function new(x:Float, y:Float, width:Float, height:Float) {
|
||||
this.x = x;
|
||||
@@ -61,7 +65,39 @@ class Rectangle {
|
||||
return point.x >= x && point.y >= y && point.x <= width && point.y <= height;
|
||||
}
|
||||
|
||||
public function intersection(rect:Rectangle):Bool {
|
||||
return !(rect.left > right ||
|
||||
rect.right < left ||
|
||||
rect.top > bottom ||
|
||||
rect.bottom < top);
|
||||
}
|
||||
|
||||
public function lean(rect:Rectangle):Void {
|
||||
var side = rect.getSide(direction);
|
||||
if (direction.x != 0) {
|
||||
x = side.point1.x - width * (direction.x + 1) / 2 - direction.x;
|
||||
} else if (direction.y != 0) {
|
||||
y = side.point1.y - height * (direction.y + 1) / 2 - direction.y;
|
||||
}
|
||||
}
|
||||
|
||||
public function toString():String {
|
||||
return 'Rectangle{x=$x,y=$y,width=$width,height=$height}';
|
||||
}
|
||||
|
||||
function get_left():Float {
|
||||
return x;
|
||||
}
|
||||
|
||||
function get_right():Float {
|
||||
return x + width;
|
||||
}
|
||||
|
||||
function get_top():Float {
|
||||
return y;
|
||||
}
|
||||
|
||||
function get_bottom():Float {
|
||||
return y + height;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,17 +1,19 @@
|
||||
package ru.m.tankz.core;
|
||||
|
||||
import ru.m.tankz.config.Config.BulletConfig;
|
||||
import ru.m.tankz.config.Config;
|
||||
import ru.m.geom.Rectangle;
|
||||
import ru.m.geom.Direction;
|
||||
|
||||
|
||||
class Bullet extends MobileEntity {
|
||||
public var tankId(default, null):Int;
|
||||
public var tankConfig(default, null):TankConfig;
|
||||
public var config(default, null):BulletConfig;
|
||||
|
||||
public function new(tankId:Int, config:BulletConfig) {
|
||||
public function new(tankId:Int, tankConfig:TankConfig, config:BulletConfig) {
|
||||
super(new Rectangle(0, 0, config.width, config.height), config.speed, Direction.RIGHT);
|
||||
this.tankId = tankId;
|
||||
this.tankConfig = tankConfig;
|
||||
this.config = config;
|
||||
this.layer = 2;
|
||||
}
|
||||
|
||||
@@ -37,7 +37,7 @@ class Tank extends MobileEntity {
|
||||
|
||||
public function shot():Null<Bullet> {
|
||||
if (bulletsCounter >= config.bullets) return null;
|
||||
var bullet = new Bullet(id, config.bullet);
|
||||
var bullet = new Bullet(id, config, config.bullet);
|
||||
bullet.rect.center = rect.center.add(new Point(rect.width / 4 * direction.x, rect.height / 4 * direction.y));
|
||||
bullet.move(direction);
|
||||
bulletsCounter++;
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
package ru.m.tankz.engine;
|
||||
|
||||
import ru.m.geom.Rectangle;
|
||||
import ru.m.geom.Line;
|
||||
import ru.m.tankz.core.MobileEntity;
|
||||
import ru.m.tankz.core.Entity;
|
||||
@@ -168,17 +169,25 @@ class Engine {
|
||||
var collision:Bool = false;
|
||||
for (cell in cells) {
|
||||
if (cell.layer >= entity.layer && cell.layer < 3) {
|
||||
var cellSide:Line = cell.rect.getSide(entity.rect.direction);
|
||||
if (entity.rect.direction.x != 0) {
|
||||
entity.rect.x = cellSide.point1.x - entity.rect.width * (entity.rect.direction.x + 1) / 2 - entity.direction.x;
|
||||
} else if (entity.rect.direction.y != 0) {
|
||||
entity.rect.y = cellSide.point1.y - entity.rect.height * (entity.rect.direction.y + 1) / 2 - entity.direction.y;
|
||||
}
|
||||
entity.rect.lean(cell.rect);
|
||||
collision = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
for (other in entities.iterator()) {
|
||||
if (other != ent) {
|
||||
if (ent.rect.intersection(other.rect)) {
|
||||
if (Std.is(ent, Bullet) && Std.is(other, Tank) && (cast(ent, Bullet).tankConfig.group == cast(other, Tank).config.group)) {
|
||||
collision = false;
|
||||
} else {
|
||||
entity.rect.lean(other.rect);
|
||||
collision = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
changes.push(new GameChange()
|
||||
.setType(GameChangeType.MOVED)
|
||||
.setObjectType(objectType)
|
||||
|
||||
Reference in New Issue
Block a user