[common] rect intersection

This commit is contained in:
2018-01-15 18:01:16 +03:00
parent e77573803e
commit 665fb439f8
5 changed files with 78 additions and 17 deletions

30
WORK.md
View File

@@ -11,15 +11,29 @@
* game frame * game frame
* engine * engine
* config 50% * config 90%
* map 50% * map 80%
* tanks 2% * tanks 30%
* bullets 2% * bullets 30%
* boxes * boxes 0%
* map changes * map changes 50%
* bonuses * bonuses 0%
* eagle 0%
* render
* map 100%
* tanks 100%
* bullet 100%
* calc redraw 20%
* animations
* tank spawn
* bullet boom
* tank boom
* bonuses
* html5
* proto * proto
* common * common
* single game 2% * bot 0%
* single game 20%

View File

@@ -7,6 +7,10 @@ class Rectangle {
public var height(default, default):Float; public var height(default, default):Float;
public var center(get, set):Point; public var center(get, set):Point;
public var direction(default, set):Direction; 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) { public function new(x:Float, y:Float, width:Float, height:Float) {
this.x = x; this.x = x;
@@ -61,7 +65,39 @@ class Rectangle {
return point.x >= x && point.y >= y && point.x <= width && point.y <= height; 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 { public function toString():String {
return 'Rectangle{x=$x,y=$y,width=$width,height=$height}'; 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;
}
} }

View File

@@ -1,17 +1,19 @@
package ru.m.tankz.core; 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.Rectangle;
import ru.m.geom.Direction; import ru.m.geom.Direction;
class Bullet extends MobileEntity { class Bullet extends MobileEntity {
public var tankId(default, null):Int; public var tankId(default, null):Int;
public var tankConfig(default, null):TankConfig;
public var config(default, null):BulletConfig; 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); super(new Rectangle(0, 0, config.width, config.height), config.speed, Direction.RIGHT);
this.tankId = tankId; this.tankId = tankId;
this.tankConfig = tankConfig;
this.config = config; this.config = config;
this.layer = 2; this.layer = 2;
} }

View File

@@ -37,7 +37,7 @@ class Tank extends MobileEntity {
public function shot():Null<Bullet> { public function shot():Null<Bullet> {
if (bulletsCounter >= config.bullets) return null; 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.rect.center = rect.center.add(new Point(rect.width / 4 * direction.x, rect.height / 4 * direction.y));
bullet.move(direction); bullet.move(direction);
bulletsCounter++; bulletsCounter++;

View File

@@ -1,5 +1,6 @@
package ru.m.tankz.engine; package ru.m.tankz.engine;
import ru.m.geom.Rectangle;
import ru.m.geom.Line; import ru.m.geom.Line;
import ru.m.tankz.core.MobileEntity; import ru.m.tankz.core.MobileEntity;
import ru.m.tankz.core.Entity; import ru.m.tankz.core.Entity;
@@ -168,17 +169,25 @@ class Engine {
var collision:Bool = false; var collision:Bool = false;
for (cell in cells) { for (cell in cells) {
if (cell.layer >= entity.layer && cell.layer < 3) { if (cell.layer >= entity.layer && cell.layer < 3) {
var cellSide:Line = cell.rect.getSide(entity.rect.direction); entity.rect.lean(cell.rect);
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;
}
collision = true; collision = true;
break; 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() changes.push(new GameChange()
.setType(GameChangeType.MOVED) .setType(GameChangeType.MOVED)
.setObjectType(objectType) .setObjectType(objectType)