From 665fb439f8593c9f5805d56ceb6f1e62cd77b585 Mon Sep 17 00:00:00 2001 From: shmyga Date: Mon, 15 Jan 2018 18:01:16 +0300 Subject: [PATCH] [common] rect intersection --- WORK.md | 30 ++++++++++++----- src/common/haxe/ru/m/geom/Rectangle.hx | 36 +++++++++++++++++++++ src/common/haxe/ru/m/tankz/core/Bullet.hx | 6 ++-- src/common/haxe/ru/m/tankz/core/Tank.hx | 2 +- src/common/haxe/ru/m/tankz/engine/Engine.hx | 21 ++++++++---- 5 files changed, 78 insertions(+), 17 deletions(-) diff --git a/WORK.md b/WORK.md index d7cbe18..82368e4 100644 --- a/WORK.md +++ b/WORK.md @@ -11,15 +11,29 @@ * game frame * engine - * config 50% - * map 50% - * tanks 2% - * bullets 2% - * boxes - * map changes - * bonuses + * config 90% + * map 80% + * tanks 30% + * bullets 30% + * boxes 0% + * map changes 50% + * bonuses 0% + * eagle 0% + +* render + * map 100% + * tanks 100% + * bullet 100% + * calc redraw 20% + * animations + * tank spawn + * bullet boom + * tank boom + * bonuses + * html5 * proto * common - * single game 2% \ No newline at end of file + * bot 0% + * single game 20% \ No newline at end of file diff --git a/src/common/haxe/ru/m/geom/Rectangle.hx b/src/common/haxe/ru/m/geom/Rectangle.hx index 8f17d5b..c8d54e3 100644 --- a/src/common/haxe/ru/m/geom/Rectangle.hx +++ b/src/common/haxe/ru/m/geom/Rectangle.hx @@ -7,6 +7,10 @@ class Rectangle { public var height(default, default):Float; public var center(get, set):Point; 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) { this.x = x; @@ -61,7 +65,39 @@ class Rectangle { 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 { 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; + } } diff --git a/src/common/haxe/ru/m/tankz/core/Bullet.hx b/src/common/haxe/ru/m/tankz/core/Bullet.hx index a8fd582..186ea02 100644 --- a/src/common/haxe/ru/m/tankz/core/Bullet.hx +++ b/src/common/haxe/ru/m/tankz/core/Bullet.hx @@ -1,17 +1,19 @@ 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.Direction; class Bullet extends MobileEntity { public var tankId(default, null):Int; + public var tankConfig(default, null):TankConfig; 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); this.tankId = tankId; + this.tankConfig = tankConfig; this.config = config; this.layer = 2; } diff --git a/src/common/haxe/ru/m/tankz/core/Tank.hx b/src/common/haxe/ru/m/tankz/core/Tank.hx index 75fa104..0927b4b 100755 --- a/src/common/haxe/ru/m/tankz/core/Tank.hx +++ b/src/common/haxe/ru/m/tankz/core/Tank.hx @@ -37,7 +37,7 @@ class Tank extends MobileEntity { public function shot():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.move(direction); bulletsCounter++; diff --git a/src/common/haxe/ru/m/tankz/engine/Engine.hx b/src/common/haxe/ru/m/tankz/engine/Engine.hx index ce12722..b4574a9 100755 --- a/src/common/haxe/ru/m/tankz/engine/Engine.hx +++ b/src/common/haxe/ru/m/tankz/engine/Engine.hx @@ -1,5 +1,6 @@ package ru.m.tankz.engine; +import ru.m.geom.Rectangle; import ru.m.geom.Line; import ru.m.tankz.core.MobileEntity; import ru.m.tankz.core.Entity; @@ -168,17 +169,25 @@ class Engine { var collision:Bool = false; for (cell in cells) { if (cell.layer >= entity.layer && cell.layer < 3) { - var cellSide:Line = cell.rect.getSide(entity.rect.direction); - 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; - } + entity.rect.lean(cell.rect); collision = true; 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() .setType(GameChangeType.MOVED) .setObjectType(objectType)