[common] breakable bricks

This commit is contained in:
2018-01-12 00:14:09 +03:00
parent 21bf2f9ba1
commit b973d0830c
7 changed files with 106 additions and 13 deletions

View File

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

View File

@@ -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();
}
}

View File

@@ -45,6 +45,7 @@ bricks:
type: 5
layer: 2
armor: 1
breakable: yes
bullet: &bullet
width: 12

View File

@@ -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}';
}
}

View File

@@ -34,6 +34,7 @@ typedef BrickConfig = {
var type:Int;
var layer:Int;
var armor:Int;
var breakable:Bool;
}
typedef BulletConfig = {

View File

@@ -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);
}
}
}
}

View File

@@ -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<Point, Bool>;
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 {