[core] collisions process
This commit is contained in:
@@ -7,15 +7,17 @@ class Entity implements IKey {
|
||||
private static var idCounter:Int = 0;
|
||||
|
||||
public var id(default, null):Int;
|
||||
public var type(default, null):String;
|
||||
public var key(get, null):String;
|
||||
public var rect(default, null):Rectangle;
|
||||
|
||||
public function new(rect:Rectangle) {
|
||||
this.id = ++idCounter;
|
||||
this.type = Type.getClassName(Type.getClass(this)).split('.').pop();
|
||||
this.rect = rect;
|
||||
}
|
||||
|
||||
private function get_key():String {
|
||||
return '${Type.getClassName(Type.getClass(this))}:$id';
|
||||
return '$type:$id';
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,5 +1,7 @@
|
||||
package ru.m.tankz.engine;
|
||||
|
||||
import haxe.macro.Type.Ref;
|
||||
import haxe.Constraints.Function;
|
||||
import ru.m.geom.Rectangle;
|
||||
import ru.m.geom.Line;
|
||||
import ru.m.tankz.core.MobileEntity;
|
||||
@@ -136,6 +138,46 @@ class Engine {
|
||||
}
|
||||
}*/
|
||||
|
||||
private function collisionTankTank(a:Tank, b:Tank):Bool {
|
||||
a.rect.lean(b.rect);
|
||||
return true;
|
||||
}
|
||||
|
||||
private function collisionBulletTank(a:Bullet, b:Tank):Bool {
|
||||
if (a.tankConfig.group != b.config.group) {
|
||||
removeEntity(a);
|
||||
removeEntity(b);
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
private function collisionTankBullet(a:Tank, b:Bullet):Bool {
|
||||
if (a.config.group != b.tankConfig.group) {
|
||||
removeEntity(a);
|
||||
removeEntity(b);
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
private function collisionBulletBullet(a:Bullet, b:Bullet):Bool {
|
||||
removeEntity(a);
|
||||
removeEntity(b);
|
||||
return true;
|
||||
}
|
||||
|
||||
private function removeEntity(entity:Entity):Void {
|
||||
if (entities.exists(entity.id)) {
|
||||
entities.remove(entity.id);
|
||||
removedEntities.push(entity.key);
|
||||
if (Std.is(entity, Bullet)) {
|
||||
var tank:Tank = cast entities.get(cast(entity, Bullet).tankId);
|
||||
tank.onDestroyBullet();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public function update():Array<GameChange> {
|
||||
var newTime:Float = Date.now().getTime();
|
||||
var d:Float = newTime - time;
|
||||
@@ -146,11 +188,7 @@ class Engine {
|
||||
for (ent in entities) if (Std.is(ent, MobileEntity)) {
|
||||
var entity:MobileEntity = cast ent;
|
||||
|
||||
var objectType = -1;
|
||||
if (Std.is(entity, Tank)) objectType = GameObjectType.TANK;
|
||||
if (Std.is(entity, Bullet)) objectType = GameObjectType.BULLET;
|
||||
|
||||
if (objectType == GameObjectType.TANK) {
|
||||
if (Std.is(entity, Tank)) {
|
||||
if (entity.direction.x != 0) {
|
||||
entity.rect.y = Math.round((entity.rect.y + entity.rect.height / 2) / map.cellHeight) * map.cellHeight - entity.rect.height / 2;
|
||||
}
|
||||
@@ -178,25 +216,16 @@ class Engine {
|
||||
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;
|
||||
var funName = 'collision${ent.type}${other.type}';
|
||||
var fun:Function = Reflect.field(this, funName);
|
||||
if (fun != null) {
|
||||
collision = Reflect.callMethod(this, fun, [ent, other]) || collision;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
changes.push(new GameChange()
|
||||
.setType(GameChangeType.MOVED)
|
||||
.setObjectType(objectType)
|
||||
.setObjectId(entity.id)
|
||||
.setX(entity.rect.x)
|
||||
.setY(entity.rect.y)
|
||||
);
|
||||
|
||||
if (objectType == GameObjectType.BULLET) {
|
||||
if (Std.is(entity, Bullet)) {
|
||||
var bullet:Bullet = cast ent;
|
||||
if (collision) {
|
||||
cells = map.grid.getCells(side.setLength(map.grid.cellWidth * 4));
|
||||
@@ -210,17 +239,17 @@ class Engine {
|
||||
}
|
||||
}
|
||||
}
|
||||
entities.remove(entity.id);
|
||||
var tank:Tank = cast entities.get(bullet.tankId);
|
||||
tank.onDestroyBullet();
|
||||
changes.push(new GameChange()
|
||||
.setType(GameChangeType.DESTROED)
|
||||
removeEntity(entity);
|
||||
}
|
||||
}
|
||||
|
||||
/*changes.push(new GameChange()
|
||||
.setType(GameChangeType.MOVED)
|
||||
.setObjectType(objectType)
|
||||
.setObjectId(entity.id)
|
||||
);
|
||||
removedEntities.push(entity.key);
|
||||
}
|
||||
}
|
||||
.setX(entity.rect.x)
|
||||
.setY(entity.rect.y)
|
||||
);*/
|
||||
}
|
||||
}
|
||||
return changes;
|
||||
|
||||
Reference in New Issue
Block a user