diff --git a/package.json b/package.json index 5bc7d1e..9fbd005 100755 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "tankz", - "version": "0.0.2", + "version": "0.0.3", "private": true, "devDependencies": { "ansi-colors": "^1.0.1", diff --git a/src/client/haxe/ru/m/tankz/render/Render.hx b/src/client/haxe/ru/m/tankz/render/Render.hx index a1e0dd5..5a5c4ea 100755 --- a/src/client/haxe/ru/m/tankz/render/Render.hx +++ b/src/client/haxe/ru/m/tankz/render/Render.hx @@ -91,6 +91,7 @@ class Render extends SpriteView implements IRender { layersForUpdate[upLayer] = true; } } + layersForUpdate[groundLayer] = true; //ToDo: for (entry in game.entities) { if (!states.exists(entry.key)) { states[entry.key] = new EntityState(); @@ -137,7 +138,6 @@ class Render extends SpriteView implements IRender { g = upLayer.graphics; } if (g != null) { - g.beginFill(0x00ff00); g.beginBitmapFill(Assets.getBitmapData('resources/images/map/map_${brick.config.type}.png')); g.drawRect( brick.cellX * game.map.cellWidth, @@ -145,6 +145,19 @@ class Render extends SpriteView implements IRender { game.map.cellWidth, game.map.cellHeight ); + if (brick.config.breakable) { + for (point in brick.breaked.keys()) { + if (brick.breaked.get(point)) { + g.beginFill(0x000000); + g.drawRect( + brick.cellX * game.map.cellWidth + point.x * game.map.cellWidth / 2, + brick.cellY * game.map.cellHeight + point.y * game.map.cellHeight / 2, + game.map.cellWidth / 2, + game.map.cellHeight / 2 + ); + } + } + } g.endFill(); } } diff --git a/src/client/resources/config/config.yaml b/src/client/resources/config/config.yaml index 9359d2a..e3e2336 100644 --- a/src/client/resources/config/config.yaml +++ b/src/client/resources/config/config.yaml @@ -45,6 +45,7 @@ bricks: type: 5 layer: 2 armor: 1 + breakable: yes bullet: &bullet width: 12 diff --git a/src/common/haxe/ru/m/geom/Point.hx b/src/common/haxe/ru/m/geom/Point.hx index 8bdbba9..cc1b3a5 100644 --- a/src/common/haxe/ru/m/geom/Point.hx +++ b/src/common/haxe/ru/m/geom/Point.hx @@ -12,4 +12,12 @@ class Point { public function add(point:Point):Point { return new Point(x + point.x, y + point.y); } + + public function hashCode():Int { + return Std.int(x + 1000 * y); + } + + public function toString():String { + return 'Point{x=$x,y=$y}'; + } } diff --git a/src/common/haxe/ru/m/tankz/config/Config.hx b/src/common/haxe/ru/m/tankz/config/Config.hx index 987ea39..6b9132e 100644 --- a/src/common/haxe/ru/m/tankz/config/Config.hx +++ b/src/common/haxe/ru/m/tankz/config/Config.hx @@ -34,6 +34,7 @@ typedef BrickConfig = { var type:Int; var layer:Int; var armor:Int; + var breakable:Bool; } typedef BulletConfig = { diff --git a/src/common/haxe/ru/m/tankz/engine/Engine.hx b/src/common/haxe/ru/m/tankz/engine/Engine.hx index 3731011..66dabfb 100755 --- a/src/common/haxe/ru/m/tankz/engine/Engine.hx +++ b/src/common/haxe/ru/m/tankz/engine/Engine.hx @@ -191,25 +191,78 @@ class Engine implements IEngine { if (objectType == GameObjectType.BULLET) { var bullet:Bullet = cast ent; + var collision:Bool = false; for (brick in bricks) { if (1 < brick.config.layer && brick.config.layer < 3) { - entities.remove(entity.id); - 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); + collision = true; break; } } - for (brick in bricks) { + var d:Direction = ent.rect.direction; + for (i in 0...bricks.length) { + var brick = bricks[i]; if (brick.config.armor > 0 && brick.config.armor <= bullet.config.piercing) { - brick.config = config.getBrick(0); + if (brick.config.breakable && brick.config.armor == bullet.config.piercing) { + //~~~~~~~~~~ + var p1:Point = new Point(0, 0); + if (d.x > 0 || d.y > 0) { + if (i == 0) { + p1 = p1.add(new Point(0, 0)); + } else { + p1 = p1.add(new Point(d.y, d.x)); + } + } else { + if (i == 0) { + p1 = p1.add(new Point(-d.x, -d.y)); + } else { + p1 = p1.add(new Point(1, 1)); + } + } + //~~~~~~~~~~ + var p2:Point = p1.add(new Point(0, 0)); + if (d.x > 0 || d.y > 0) { + if (i == 0) { + p2 = p2.add(new Point(d.y, d.x)); + } else { + p2 = p2.add(new Point(-d.y, -d.x)); + } + } else { + if (i == 0) { + p2 = p2.add(new Point(-d.y, -d.x)); + } else { + p2 = p2.add(new Point(d.y, d.x)); + } + } + //~~~~~~~~~~ + if (brick.breaked.get(p1)) { + p1 = p1.add(new Point(ent.rect.direction.x, ent.rect.direction.y)); + p2 = p2.add(new Point(ent.rect.direction.x, ent.rect.direction.y)); + } + if (brick.breaked.get(p1)) { + collision = false; + } else { + brick.breaked.set(p1, true); + brick.breaked.set(p2, true); + } + if (brick.destroyed) { + brick.config = config.getBrick(0); + } + } else { + brick.config = config.getBrick(0); + } } } + if (collision) { + entities.remove(entity.id); + 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); + } } } } diff --git a/src/common/haxe/ru/m/tankz/map/Brick.hx b/src/common/haxe/ru/m/tankz/map/Brick.hx index 71867c1..e5eb7aa 100644 --- a/src/common/haxe/ru/m/tankz/map/Brick.hx +++ b/src/common/haxe/ru/m/tankz/map/Brick.hx @@ -1,5 +1,7 @@ package ru.m.tankz.map; +import haxe.ds.HashMap; +import ru.m.geom.Point; import ru.m.tankz.config.Config.BrickConfig; import ru.m.tankz.core.IKey; @@ -9,6 +11,7 @@ class Brick implements IKey { type: -1, layer: 2, armor: 3, + breakable: false, } public var cellX(default, null):Int; @@ -16,10 +19,24 @@ class Brick implements IKey { public var key(get, null):String; public var config(default, default):BrickConfig; + public var breaked:HashMap; + public var destroyed(get, null):Bool; + public function new(config:BrickConfig, cellX:Int, cellY:Int) { this.cellX = cellX; this.cellY = cellY; this.config = config; + this.breaked = new HashMap(); + } + + public function get_destroyed():Bool { + var i = 0; + for (k in breaked.keys()) { + if (++i >=4) { + return true; + } + } + return false; } public function get_key():String {