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