[engine] update collision detect

This commit is contained in:
2018-02-02 17:30:50 +03:00
parent 6f338584eb
commit f9cb985059
5 changed files with 55 additions and 33 deletions

View File

@@ -1,6 +1,6 @@
{
"name": "tankz",
"version": "0.2.2",
"version": "0.2.3",
"private": true,
"devDependencies": {
"ansi-colors": "^1.0.1",

View File

@@ -131,7 +131,7 @@ class Render extends SpriteView implements EngineListener {
if (items.exists(bullet.key)) {
entryLayer.removeChild(items.get(bullet.key).view);
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):
if (items.exists(eagle.key)) {

View File

@@ -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 {
var center = this.center;
var width = point2.x - point1.x;

View File

@@ -18,6 +18,6 @@ class Point {
}
public function toString():String {
return 'Point{x=$x,y=$y}';
return 'Point{x=${Math.round(x * 100) / 100},y=${Math.round(y * 100) / 100}}';
}
}

View File

@@ -1,5 +1,6 @@
package ru.m.tankz.engine;
import ru.m.geom.Point;
import ru.m.geom.Line;
import ru.m.tankz.config.Config;
import ru.m.tankz.control.Control;
@@ -134,6 +135,8 @@ class Engine implements ControlHandler {
}
}
public function update():Void {
var newTime:Float = Date.now().getTime();
var d:Float = newTime - time;
@@ -153,10 +156,15 @@ class Engine implements ControlHandler {
}*/
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 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 isStop:Bool = false;
while (c-- >= 0) {
side = side.move(step);
var cells = map.grid.getCells(side);
var collision:Bool = false;
@@ -166,6 +174,7 @@ class Engine implements ControlHandler {
collision = true;
var with = EntityTypeResolver.of(cell);
for (l in listeners) l.onCollision(entityType, with);
isStop = true;
break;
}
}
@@ -175,6 +184,7 @@ class Engine implements ControlHandler {
if (other.rect.intersection2(side)) {
var with = EntityTypeResolver.of(other);
for (l in listeners) l.onCollision(entityType, with);
isStop = true;
}
}
}
@@ -195,6 +205,14 @@ class Engine implements ControlHandler {
}
}
}
if (isStop) break;
}
if (!isStop || Std.is(entity, Bullet)) {
entity.rect.x += entity.mx * (d / 30);
entity.rect.y += entity.my * (d / 30);
}
}
}
}