[engine] update collision detect
This commit is contained in:
@@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "tankz",
|
"name": "tankz",
|
||||||
"version": "0.2.2",
|
"version": "0.2.3",
|
||||||
"private": true,
|
"private": true,
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
"ansi-colors": "^1.0.1",
|
"ansi-colors": "^1.0.1",
|
||||||
|
|||||||
@@ -131,7 +131,7 @@ class Render extends SpriteView implements EngineListener {
|
|||||||
if (items.exists(bullet.key)) {
|
if (items.exists(bullet.key)) {
|
||||||
entryLayer.removeChild(items.get(bullet.key).view);
|
entryLayer.removeChild(items.get(bullet.key).view);
|
||||||
items.remove(bullet.key);
|
items.remove(bullet.key);
|
||||||
playBulletBoom(bullet.rect.center);
|
playBulletBoom(bullet.rect.center.add(new Point(bullet.rect.width * bullet.rect.direction.x, bullet.rect.height * bullet.rect.direction.y)));
|
||||||
}
|
}
|
||||||
case EntityType.EAGLE(eagle):
|
case EntityType.EAGLE(eagle):
|
||||||
if (items.exists(eagle.key)) {
|
if (items.exists(eagle.key)) {
|
||||||
|
|||||||
@@ -18,6 +18,10 @@ class Line {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function move(point:Point):Line {
|
||||||
|
return new Line(point1.add(point), point2.add(point));
|
||||||
|
}
|
||||||
|
|
||||||
public function setLength(value:Float):Line {
|
public function setLength(value:Float):Line {
|
||||||
var center = this.center;
|
var center = this.center;
|
||||||
var width = point2.x - point1.x;
|
var width = point2.x - point1.x;
|
||||||
|
|||||||
@@ -18,6 +18,6 @@ class Point {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public function toString():String {
|
public function toString():String {
|
||||||
return 'Point{x=$x,y=$y}';
|
return 'Point{x=${Math.round(x * 100) / 100},y=${Math.round(y * 100) / 100}}';
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
package ru.m.tankz.engine;
|
package ru.m.tankz.engine;
|
||||||
|
|
||||||
|
import ru.m.geom.Point;
|
||||||
import ru.m.geom.Line;
|
import ru.m.geom.Line;
|
||||||
import ru.m.tankz.config.Config;
|
import ru.m.tankz.config.Config;
|
||||||
import ru.m.tankz.control.Control;
|
import ru.m.tankz.control.Control;
|
||||||
@@ -134,6 +135,8 @@ class Engine implements ControlHandler {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
public function update():Void {
|
public function update():Void {
|
||||||
var newTime:Float = Date.now().getTime();
|
var newTime:Float = Date.now().getTime();
|
||||||
var d:Float = newTime - time;
|
var d:Float = newTime - time;
|
||||||
@@ -153,47 +156,62 @@ class Engine implements ControlHandler {
|
|||||||
}*/
|
}*/
|
||||||
|
|
||||||
if (entity.mx != 0 || entity.my != 0) {
|
if (entity.mx != 0 || entity.my != 0) {
|
||||||
entity.rect.x += entity.mx * (d / 30);
|
|
||||||
entity.rect.y += entity.my * (d / 30);
|
|
||||||
|
|
||||||
var side:Line = entity.rect.getSide(entity.rect.direction.reverse());
|
var side:Line = entity.rect.getSide(entity.rect.direction.reverse());
|
||||||
var cells = map.grid.getCells(side);
|
var step:Point = new Point(entity.rect.direction.x * map.cellWidth / 4, entity.rect.direction.y * map.cellHeight / 4);
|
||||||
|
var end:Point = side.center.add(new Point(entity.mx * (d / 30), entity.my * (d / 30)));
|
||||||
|
var c:Int = Math.floor(Math.abs(((end.x - side.center.x) / (map.cellWidth / 4) + (end.y - side.center.y) / (map.cellHeight / 4))));
|
||||||
|
|
||||||
var collision:Bool = false;
|
var isStop:Bool = false;
|
||||||
for (cell in cells) {
|
|
||||||
if (cell.layer >= entity.layer && cell.layer < 3) {
|
|
||||||
entity.rect.lean(cell.rect);
|
|
||||||
collision = true;
|
|
||||||
var with = EntityTypeResolver.of(cell);
|
|
||||||
for (l in listeners) l.onCollision(entityType, with);
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
for (other in entities.iterator()) {
|
while (c-- >= 0) {
|
||||||
if (other != ent && other != null) {
|
side = side.move(step);
|
||||||
if (other.rect.intersection2(side)) {
|
var cells = map.grid.getCells(side);
|
||||||
var with = EntityTypeResolver.of(other);
|
|
||||||
|
var collision:Bool = false;
|
||||||
|
for (cell in cells) {
|
||||||
|
if (cell.layer >= entity.layer && cell.layer < 3) {
|
||||||
|
entity.rect.lean(cell.rect);
|
||||||
|
collision = true;
|
||||||
|
var with = EntityTypeResolver.of(cell);
|
||||||
for (l in listeners) l.onCollision(entityType, with);
|
for (l in listeners) l.onCollision(entityType, with);
|
||||||
|
isStop = true;
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
if (Std.is(entity, Bullet)) {
|
for (other in entities.iterator()) {
|
||||||
var bullet:Bullet = cast ent;
|
if (other != ent && other != null) {
|
||||||
if (collision) {
|
if (other.rect.intersection2(side)) {
|
||||||
cells = map.grid.getCells(side.setLength(map.grid.cellWidth * 3));
|
var with = EntityTypeResolver.of(other);
|
||||||
for (cell in cells) {
|
for (l in listeners) l.onCollision(entityType, with);
|
||||||
if (cell.armor > 0) {
|
isStop = true;
|
||||||
if (cell.armor == bullet.config.piercing) {
|
}
|
||||||
cell.destroyed = true;
|
}
|
||||||
} else if (cell.armor < bullet.config.piercing) {
|
}
|
||||||
var brick = map.getBrick(cell);
|
|
||||||
brick.destroyed = true;
|
if (Std.is(entity, Bullet)) {
|
||||||
|
var bullet:Bullet = cast ent;
|
||||||
|
if (collision) {
|
||||||
|
cells = map.grid.getCells(side.setLength(map.grid.cellWidth * 3));
|
||||||
|
for (cell in cells) {
|
||||||
|
if (cell.armor > 0) {
|
||||||
|
if (cell.armor == bullet.config.piercing) {
|
||||||
|
cell.destroyed = true;
|
||||||
|
} else if (cell.armor < bullet.config.piercing) {
|
||||||
|
var brick = map.getBrick(cell);
|
||||||
|
brick.destroyed = true;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (isStop) break;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!isStop || Std.is(entity, Bullet)) {
|
||||||
|
entity.rect.x += entity.mx * (d / 30);
|
||||||
|
entity.rect.y += entity.my * (d / 30);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user