From 8dc21b41c0f722b0be4f437bb36588e1705af589 Mon Sep 17 00:00:00 2001 From: shmyga Date: Fri, 12 Jan 2018 18:00:32 +0300 Subject: [PATCH] udpate --- .../haxe/ru/m/tankz/core/PlayerControl.hx | 8 +- src/client/haxe/ru/m/tankz/render/IRender.hx | 4 +- src/client/haxe/ru/m/tankz/render/Render.hx | 50 +++++++------ .../haxe/ru/m/tankz/view/frames/GameFrame.hx | 3 +- src/client/resources/config/config.yaml | 7 +- src/common/haxe/ru/m/geom/Line.hx | 16 ++++ src/common/haxe/ru/m/geom/Rectangle.hx | 21 +++++- src/common/haxe/ru/m/tankz/config/Config.hx | 5 +- src/common/haxe/ru/m/tankz/core/Entity.hx | 2 +- src/common/haxe/ru/m/tankz/core/IEntity.hx | 9 --- .../haxe/ru/m/tankz/core/IMobileEntity.hx | 15 ---- .../haxe/ru/m/tankz/core/MobileEntity.hx | 2 +- src/common/haxe/ru/m/tankz/engine/Engine.hx | 73 ++++++++++++------- src/common/haxe/ru/m/tankz/engine/IEngine.hx | 23 ------ src/common/haxe/ru/m/tankz/map/Brick.hx | 31 +++++--- src/common/haxe/ru/m/tankz/map/Grid.hx | 53 ++++++++++++++ src/common/haxe/ru/m/tankz/map/ILevelMap.hx | 13 ---- src/common/haxe/ru/m/tankz/map/LevelMap.hx | 41 ++++++++++- 18 files changed, 235 insertions(+), 141 deletions(-) create mode 100644 src/common/haxe/ru/m/geom/Line.hx delete mode 100755 src/common/haxe/ru/m/tankz/core/IEntity.hx delete mode 100755 src/common/haxe/ru/m/tankz/core/IMobileEntity.hx delete mode 100755 src/common/haxe/ru/m/tankz/engine/IEngine.hx create mode 100644 src/common/haxe/ru/m/tankz/map/Grid.hx delete mode 100755 src/common/haxe/ru/m/tankz/map/ILevelMap.hx diff --git a/src/client/haxe/ru/m/tankz/core/PlayerControl.hx b/src/client/haxe/ru/m/tankz/core/PlayerControl.hx index 3a9a22b..cb5be6d 100644 --- a/src/client/haxe/ru/m/tankz/core/PlayerControl.hx +++ b/src/client/haxe/ru/m/tankz/core/PlayerControl.hx @@ -4,21 +4,21 @@ import haxe.Timer; import ru.m.geom.Direction; import flash.events.FocusEvent; import flash.ui.Keyboard; -import ru.m.tankz.engine.IEngine; +import ru.m.tankz.engine.Engine; import ru.m.tankz.core.Tank.TankAction; import flash.events.KeyboardEvent; import flash.Lib; class PlayerControl { - private var engine:IEngine; + private var engine:Engine; private var id:Int; private var keyBinding:Map; private var moveQueue:Array; private var shotTimer:Timer; - public function new(id:Int, engine:IEngine, keyBinding:Map) { + public function new(id:Int, engine:Engine, keyBinding:Map) { this.id = id; this.engine = engine; this.keyBinding = keyBinding; @@ -83,7 +83,7 @@ class PlayerControl { engine.action(id, TankAction.SHOT); } - public static function forPlayer(index:Int, tankId:Int, engine:IEngine):PlayerControl { + public static function forPlayer(index:Int, tankId:Int, engine:Engine):PlayerControl { switch (index) { case 0: return new PlayerControl(tankId, engine, [ diff --git a/src/client/haxe/ru/m/tankz/render/IRender.hx b/src/client/haxe/ru/m/tankz/render/IRender.hx index eed0729..d8a3810 100755 --- a/src/client/haxe/ru/m/tankz/render/IRender.hx +++ b/src/client/haxe/ru/m/tankz/render/IRender.hx @@ -1,10 +1,10 @@ package ru.m.tankz.render; +import ru.m.tankz.engine.Engine; import haxework.gui.IView; -import ru.m.tankz.engine.IEngine; interface IRender extends IView { - public function draw(game:IEngine):Void; + public function draw(game:Engine):Void; public function reset():Void; } diff --git a/src/client/haxe/ru/m/tankz/render/Render.hx b/src/client/haxe/ru/m/tankz/render/Render.hx index 5a5c4ea..fcd26c0 100755 --- a/src/client/haxe/ru/m/tankz/render/Render.hx +++ b/src/client/haxe/ru/m/tankz/render/Render.hx @@ -1,6 +1,7 @@ package ru.m.tankz.render; -import ru.m.tankz.core.IMobileEntity; +import ru.m.tankz.core.MobileEntity; +import ru.m.tankz.engine.Engine; import ru.m.tankz.core.Entity; import ru.m.tankz.map.Brick; import ru.m.geom.Direction; @@ -11,7 +12,6 @@ import ru.m.tankz.core.Tank; import flash.display.Sprite; import flash.display.Graphics; import haxework.gui.SpriteView; -import ru.m.tankz.engine.IEngine; interface IState { @@ -81,7 +81,7 @@ class Render extends SpriteView implements IRender { reset(); } - private function invalidateLayers(game:IEngine):Void { + private function invalidateLayers(game:Engine):Void { for (brick in game.map.bricks) { if (!states.exists(brick.key)) { states[brick.key] = new BrickState(); @@ -108,7 +108,7 @@ class Render extends SpriteView implements IRender { } } - private function drawBackground(game:IEngine):Void { + private function drawBackground(game:Engine):Void { var mapWidth = game.map.gridWidth * game.map.cellWidth; var mapHeight = game.map.gridHeight * game.map.cellHeight; @@ -126,7 +126,7 @@ class Render extends SpriteView implements IRender { } } - private function drawMap(game:IEngine):Void { + private function drawMap(game:Engine):Void { if (layersForUpdate[groundLayer] || layersForUpdate[upLayer]) { groundLayer.graphics.clear(); upLayer.graphics.clear(); @@ -145,28 +145,36 @@ 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 - ); - } + for (point in brick.cells.keys()) { + if (brick.cells.get(point).destroyed) { + 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(); } } + /*var g = groundLayer.graphics; + for (c in game.map.grid.cells.iterator()) { + var color:Int = 0x000000; + if (c.armor > 0) { + color = 0x00ff00; + } + g.beginFill(color); + g.drawRect(c.rect.x, c.rect.y, c.rect.width, c.rect.height); + g.endFill(); + }*/ layersForUpdate[groundLayer] = false; layersForUpdate[upLayer] = false; } } - public function drawEntities(game:IEngine):Void { + public function drawEntities(game:Engine):Void { if (layersForUpdate[entryLayer]) { var g:Graphics = entryLayer.graphics; g.clear(); @@ -177,13 +185,13 @@ class Render extends SpriteView implements IRender { image = 'resources/images/tank/player/tank_p${tank.config.level}_${tank.index}-0.png'; } else if (Std.is(ent, Bullet)) { var bullet:Bullet = cast ent; - image = 'resources/images/bullet/bullet_${bullet.config.piercing-1}.png'; + image = 'resources/images/bullet/bullet_${bullet.config.piercing > 1 ? 1 : 0}.png'; } else { image = 'ERROR'; // ToDo: } var m = new Matrix(); - if (Std.is(ent, IMobileEntity)) { - m.rotate(calcRotate(cast(ent, IMobileEntity).direction)); + if (Std.is(ent, MobileEntity)) { + m.rotate(calcRotate(cast(ent, MobileEntity).direction)); } m.translate(ent.rect.x, ent.rect.y); g.beginBitmapFill(Assets.getBitmapData(image), m, true, true); @@ -194,7 +202,7 @@ class Render extends SpriteView implements IRender { } } - public function draw(game:IEngine):Void { + public function draw(game:Engine):Void { invalidateLayers(game); drawBackground(game); drawMap(game); diff --git a/src/client/haxe/ru/m/tankz/view/frames/GameFrame.hx b/src/client/haxe/ru/m/tankz/view/frames/GameFrame.hx index 1f2853c..11000c3 100755 --- a/src/client/haxe/ru/m/tankz/view/frames/GameFrame.hx +++ b/src/client/haxe/ru/m/tankz/view/frames/GameFrame.hx @@ -4,7 +4,6 @@ import ru.m.tankz.proto.core.GameType; import ru.m.tankz.proto.core.Game; import ru.m.tankz.core.PlayerControl; import ru.m.tankz.engine.Engine; -import ru.m.tankz.engine.IEngine; import protohx.Message; import ru.m.tankz.proto.pack.GameUpdateResponse; import ru.m.connect.IConnection; @@ -21,7 +20,7 @@ class GameFrame extends VGroupView implements ViewBuilder implements IPacketHand public static inline var ID = "game"; - private var engine:IEngine; + private var engine:Engine; private var controls:Map; public function init():Void { diff --git a/src/client/resources/config/config.yaml b/src/client/resources/config/config.yaml index e3e2336..fc1ab52 100644 --- a/src/client/resources/config/config.yaml +++ b/src/client/resources/config/config.yaml @@ -20,7 +20,7 @@ bricks: -1: &brick-boder type: -1 layer: 2 - armor: 3 + armor: -1 0: &brick-none type: 0 layer: 0 @@ -45,7 +45,6 @@ bricks: type: 5 layer: 2 armor: 1 - breakable: yes bullet: &bullet width: 12 @@ -81,7 +80,7 @@ tanks: bullet: <<: *bullet speed: 9.0 - bullets: 2 + bullets: 3 3: level: 3 width: 42 @@ -90,5 +89,5 @@ tanks: bullet: <<: *bullet speed: 9.0 - piercing: 2 + piercing: 3 bullets: 2 diff --git a/src/common/haxe/ru/m/geom/Line.hx b/src/common/haxe/ru/m/geom/Line.hx new file mode 100644 index 0000000..c13679c --- /dev/null +++ b/src/common/haxe/ru/m/geom/Line.hx @@ -0,0 +1,16 @@ +package ru.m.geom; + +class Line { + + public var point1(default, null):Point; + public var point2(default, null):Point; + + public function new(point1:Point, point2:Point) { + this.point1 = point1; + this.point2 = point2; + } + + public function toString():String { + return 'Line{point1=$point1,point2=$point2}'; + } +} diff --git a/src/common/haxe/ru/m/geom/Rectangle.hx b/src/common/haxe/ru/m/geom/Rectangle.hx index a39764c..bd0f195 100644 --- a/src/common/haxe/ru/m/geom/Rectangle.hx +++ b/src/common/haxe/ru/m/geom/Rectangle.hx @@ -26,8 +26,22 @@ class Rectangle { return value; } - public function getSide(direction:Direction):Point { - return center.add(new Point(direction.x * width / 2, direction.y * height / 2)); + public function getSide(direction:Direction):Line { + if (direction.x != 0) { + var x = x + (direction.x * -1 + 1) / 2 * width; + return new Line( + new Point(x, y), + new Point(x, y + height) + ); + } else if (direction.y != 0) { + var y = y + (direction.y * -1 + 1) / 2 * height; + return new Line( + new Point(x, y), + new Point(x + width, y) + ); + } else { + return null; + } } public function set_direction(value:Direction):Direction { @@ -43,4 +57,7 @@ class Rectangle { return value; } + public function toString():String { + return 'Rectangle{x=$x,y=$y,width=$width,height=$height}'; + } } diff --git a/src/common/haxe/ru/m/tankz/config/Config.hx b/src/common/haxe/ru/m/tankz/config/Config.hx index 6b9132e..a9ff213 100644 --- a/src/common/haxe/ru/m/tankz/config/Config.hx +++ b/src/common/haxe/ru/m/tankz/config/Config.hx @@ -34,7 +34,6 @@ typedef BrickConfig = { var type:Int; var layer:Int; var armor:Int; - var breakable:Bool; } typedef BulletConfig = { @@ -120,7 +119,9 @@ class ConfigBundle { var bricks:Array = []; for (line in ~/\s+/g.split(bricksData)) { for (c in line.split('')) { - bricks.push(source.bricks.get(Std.parseInt(c))); + if (c.length > 0) { + bricks.push(source.bricks.get(Std.parseInt(c))); + } } } source.map.bricks = bricks; diff --git a/src/common/haxe/ru/m/tankz/core/Entity.hx b/src/common/haxe/ru/m/tankz/core/Entity.hx index 9d9c7a3..8d1143b 100755 --- a/src/common/haxe/ru/m/tankz/core/Entity.hx +++ b/src/common/haxe/ru/m/tankz/core/Entity.hx @@ -3,7 +3,7 @@ package ru.m.tankz.core; import ru.m.geom.Rectangle; -class Entity implements IEntity { +class Entity implements IKey { private static var idCounter:Int = 0; public var id(default, null):Int; diff --git a/src/common/haxe/ru/m/tankz/core/IEntity.hx b/src/common/haxe/ru/m/tankz/core/IEntity.hx deleted file mode 100755 index 7c07f27..0000000 --- a/src/common/haxe/ru/m/tankz/core/IEntity.hx +++ /dev/null @@ -1,9 +0,0 @@ -package ru.m.tankz.core; - -import ru.m.geom.Rectangle; - - -interface IEntity extends IKey { - public var id(default, null):Int; - public var rect(default, null):Rectangle; -} diff --git a/src/common/haxe/ru/m/tankz/core/IMobileEntity.hx b/src/common/haxe/ru/m/tankz/core/IMobileEntity.hx deleted file mode 100755 index eb4fc80..0000000 --- a/src/common/haxe/ru/m/tankz/core/IMobileEntity.hx +++ /dev/null @@ -1,15 +0,0 @@ -package ru.m.tankz.core; - -import ru.m.geom.Direction; - - -interface IMobileEntity extends IEntity { - public var mx(default, default):Float; - public var my(default, default):Float; - - public var speed(default, null):Float; - public var direction(default, default):Direction; - - public function move(direction:Direction):Void; - public function stop():Void; -} diff --git a/src/common/haxe/ru/m/tankz/core/MobileEntity.hx b/src/common/haxe/ru/m/tankz/core/MobileEntity.hx index 1e3cb27..01a3f4a 100755 --- a/src/common/haxe/ru/m/tankz/core/MobileEntity.hx +++ b/src/common/haxe/ru/m/tankz/core/MobileEntity.hx @@ -4,7 +4,7 @@ import ru.m.geom.Direction; import ru.m.geom.Rectangle; -class MobileEntity extends Entity implements IMobileEntity { +class MobileEntity extends Entity { public var mx(default, default):Float = 0; public var my(default, default):Float = 0; diff --git a/src/common/haxe/ru/m/tankz/engine/Engine.hx b/src/common/haxe/ru/m/tankz/engine/Engine.hx index 66dabfb..2ec9fa2 100755 --- a/src/common/haxe/ru/m/tankz/engine/Engine.hx +++ b/src/common/haxe/ru/m/tankz/engine/Engine.hx @@ -1,8 +1,10 @@ package ru.m.tankz.engine; +import ru.m.geom.Line; +import ru.m.tankz.core.MobileEntity; +import ru.m.tankz.core.Entity; import ru.m.geom.Direction; import ru.m.tankz.config.Config.TankConfig; -import ru.m.tankz.core.IEntity; import ru.m.geom.Point; import ru.m.tankz.map.Brick; import ru.m.tankz.core.Bullet; @@ -10,19 +12,17 @@ import ru.m.tankz.proto.game.GameObjectType; import ru.m.tankz.proto.game.GameChangeType; import ru.m.tankz.proto.game.GameChange; import ru.m.tankz.proto.core.Player; -import ru.m.tankz.core.IMobileEntity; import ru.m.tankz.config.Config; import ru.m.tankz.core.Tank; import ru.m.tankz.map.LevelMap; -import ru.m.tankz.map.ILevelMap; -class Engine implements IEngine { +class Engine { public var config(default, default):Config; - public var map(default, null):ILevelMap; + public var map(default, null):LevelMap; - public var entities(default, null):Map; + public var entities(default, null):Map; public var removedEntities(default, null):Array; private var playerTanks(default, null):Map; @@ -45,7 +45,7 @@ class Engine implements IEngine { this.config = config; map = new LevelMap(config.source.map); playerTanks = new Map(); - entities = new Map(); + entities = new Map(); removedEntities = new Array(); time = Date.now().getTime(); } @@ -130,15 +130,22 @@ class Engine implements IEngine { } }*/ - private function getBricks(entity:IMobileEntity):Array { - var target:Point = entity.rect.getSide(entity.direction); + /*private function getBricks(entity:MobileEntity):Array { + var target:Point = entity.rect.getSide(entity.rect.direction); var cellX:Int = Math.floor(target.x / map.cellWidth); var cellY:Int = Math.floor(target.y / map.cellHeight); var brick1 = map.getBrick(cellX, cellY); - var brick2 = map.getBrick(cellX - entity.direction.y * entity.direction.y, cellY - entity.direction.x * entity.direction.x); + var brick2 = map.getBrick(cellX - entity.rect.direction.y * entity.rect.direction.y, cellY - entity.rect.direction.x * entity.rect.direction.x); return [brick1, brick2]; } + public function checkBrick(entity:MobileEntity, brick:Brick):Null { + if (0 < brick.config.layer && brick.config.layer < 3) { + return brick.rect.getSide(entity.rect.direction.reverse()); + } + return null; + }*/ + public function update():Array { var newTime:Float = Date.now().getTime(); var d:Float = newTime - time; @@ -146,8 +153,8 @@ class Engine implements IEngine { var changes = new Array(); - for (ent in entities) if (Std.is(ent, IMobileEntity)) { - var entity:IMobileEntity = cast ent; + 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; @@ -165,16 +172,20 @@ class Engine implements IEngine { if (entity.mx != 0 || entity.my != 0) { entity.rect.x += entity.mx * (d / 30); entity.rect.y += entity.my * (d / 30); - var bricks = getBricks(entity); + + var side:Line = entity.rect.getSide(entity.rect.direction); + L.w('TEST', 'side') + var bricks:Array = [];//getBricks(entity); if (objectType == GameObjectType.TANK) { for (brick in bricks) { - if (0 < brick.config.layer && brick.config.layer < 3) { - if (entity.direction.x != 0) { - entity.rect.x = brick.cellX * map.cellWidth + map.cellWidth / 2 - entity.direction.x * map.cellWidth / 2 - entity.direction.x * entity.rect.width / 2 - entity.rect.width / 2; + var point:Point = null;//checkBrick(entity, brick); + if (point != null) { + if (entity.rect.direction.x != 0) { + entity.rect.x = point.x - entity.direction.x * entity.rect.width / 2 - entity.rect.width / 2; } - if (entity.direction.y != 0) { - entity.rect.y = brick.cellY * map.cellHeight + map.cellHeight / 2 - entity.direction.y * map.cellHeight / 2 - entity.direction.y * entity.rect.height / 2 - entity.rect.height / 2; + if (entity.rect.direction.y != 0) { + entity.rect.y = point.y - entity.direction.y * entity.rect.height / 2 - entity.rect.height / 2; } break; } @@ -201,8 +212,14 @@ class Engine implements IEngine { 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) { - if (brick.config.breakable && brick.config.armor == bullet.config.piercing) { + if (brick.config.armor != 0) { + if (brick.config.armor > 0 && brick.config.armor <= bullet.config.piercing) { + brick.config = config.getBrick(0); + } + } + + /*if (brick.config.armor > 0 && brick.config.armor <= bullet.config.piercing) { + if (brick.config.armor == bullet.config.piercing) { //~~~~~~~~~~ var p1:Point = new Point(0, 0); if (d.x > 0 || d.y > 0) { @@ -234,15 +251,15 @@ class Engine implements IEngine { } } //~~~~~~~~~~ - if (brick.breaked.get(p1)) { + if (brick.blocks.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)) { + if (brick.blocks.get(p1)) { collision = false; } else { - brick.breaked.set(p1, true); - brick.breaked.set(p2, true); + brick.blocks.set(p1, true); + brick.blocks.set(p2, true); } if (brick.destroyed) { brick.config = config.getBrick(0); @@ -250,16 +267,16 @@ class Engine implements IEngine { } 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) + .setType(GameChangeType.DESTROED) + .setObjectType(objectType) + .setObjectId(entity.id) ); removedEntities.push(entity.key); } diff --git a/src/common/haxe/ru/m/tankz/engine/IEngine.hx b/src/common/haxe/ru/m/tankz/engine/IEngine.hx deleted file mode 100755 index 81b64d9..0000000 --- a/src/common/haxe/ru/m/tankz/engine/IEngine.hx +++ /dev/null @@ -1,23 +0,0 @@ -package ru.m.tankz.engine; - -import ru.m.tankz.core.IEntity; -import ru.m.tankz.proto.game.GameChange; -import ru.m.tankz.core.Tank; -import ru.m.tankz.proto.core.Player; -import ru.m.tankz.config.Config; -import ru.m.tankz.map.ILevelMap; - -interface IEngine { - public var config(default, default):Config; - public var map(default, null):ILevelMap; - - public var entities(default, null):Map; - public var removedEntities(default, null):Array; - - public function clear():Void; - public function init(config:Config):Void; - public function initTanks(players:Array):Array; - public function action(tankId:Int, action:TankAction):Void; - //public function updateFromChanges(changes:Array):Void; - public function update():Array; -} diff --git a/src/common/haxe/ru/m/tankz/map/Brick.hx b/src/common/haxe/ru/m/tankz/map/Brick.hx index e5eb7aa..e2af9ed 100644 --- a/src/common/haxe/ru/m/tankz/map/Brick.hx +++ b/src/common/haxe/ru/m/tankz/map/Brick.hx @@ -1,8 +1,10 @@ package ru.m.tankz.map; +import ru.m.tankz.map.Grid.GridCell; +import ru.m.geom.Rectangle; import haxe.ds.HashMap; import ru.m.geom.Point; -import ru.m.tankz.config.Config.BrickConfig; +import ru.m.tankz.config.Config; import ru.m.tankz.core.IKey; @@ -10,33 +12,42 @@ class Brick implements IKey { public static var BORDER:BrickConfig = { type: -1, layer: 2, - armor: 3, - breakable: false, + armor: -1, } public var cellX(default, null):Int; public var cellY(default, null):Int; public var key(get, null):String; + + public var mapConfig(default, null):MapConfig; public var config(default, default):BrickConfig; - public var breaked:HashMap; + public var rect(default, null):Rectangle; + public var cells(default, null):HashMap; public var destroyed(get, null):Bool; - public function new(config:BrickConfig, cellX:Int, cellY:Int) { + public function new(mapConfig:MapConfig, config:BrickConfig, cellX:Int, cellY:Int, cells:HashMap) { this.cellX = cellX; this.cellY = cellY; + this.cells = cells; + this.mapConfig = mapConfig; this.config = config; - this.breaked = new HashMap(); + this.rect = new Rectangle( + cellX * mapConfig.cellWidth, + cellY * mapConfig.cellHeight, + mapConfig.cellWidth, + mapConfig.cellHeight + ); } public function get_destroyed():Bool { var i = 0; - for (k in breaked.keys()) { - if (++i >=4) { - return true; + for (c in cells.iterator()) { + if (!c.destroyed) { + return false; } } - return false; + return true; } public function get_key():String { diff --git a/src/common/haxe/ru/m/tankz/map/Grid.hx b/src/common/haxe/ru/m/tankz/map/Grid.hx new file mode 100644 index 0000000..cbfdbc0 --- /dev/null +++ b/src/common/haxe/ru/m/tankz/map/Grid.hx @@ -0,0 +1,53 @@ +package ru.m.tankz.map; + +import ru.m.geom.Point; +import haxe.ds.HashMap; +import ru.m.geom.Rectangle; + + +class GridCell { + public var rect(default, null):Rectangle; + public var layer:Int; + public var armor:Int; + public var destroyed:Bool; + + public function new(rect:Rectangle, layer:Int, armor:Int) { + this.rect = rect; + this.layer = layer; + this.armor = armor; + this.destroyed = false; + } +} + + +class Grid { + private var cellWidth:Int; + private var cellHeight:Int; + private var width:Int; + private var height:Int; + + public var cells(default, null):HashMap; + + public function new(cellWidth:Int, cellHeight:Int, width:Int, height:Int) { + this.cellWidth = cellWidth; + this.cellHeight = cellHeight; + this.width = width; + this.height = height; + cells = new HashMap(); + for (x in 0...Math.floor(width / cellWidth)) { + for (y in 0...Math.floor(width / cellWidth)) { + var rect = new Rectangle( + x * cellWidth, + y * cellHeight, + cellWidth, + cellHeight + ); + cells.set(new Point(x, y), new GridCell(rect, 0, 0)); + } + } + } + + public function getCells(rect:Rectangle):Array { + return []; + } +} \ No newline at end of file diff --git a/src/common/haxe/ru/m/tankz/map/ILevelMap.hx b/src/common/haxe/ru/m/tankz/map/ILevelMap.hx deleted file mode 100755 index 8afdedd..0000000 --- a/src/common/haxe/ru/m/tankz/map/ILevelMap.hx +++ /dev/null @@ -1,13 +0,0 @@ -package ru.m.tankz.map; - - -interface ILevelMap { - public var cellWidth(default, null):Float; - public var cellHeight(default, null):Float; - public var gridWidth(default, null):Int; - public var gridHeight(default, null):Int; - - public var bricks(default, null):Array; - - public function getBrick(cellX:Int, cellY:Int):Brick; -} diff --git a/src/common/haxe/ru/m/tankz/map/LevelMap.hx b/src/common/haxe/ru/m/tankz/map/LevelMap.hx index 9091129..404fa77 100755 --- a/src/common/haxe/ru/m/tankz/map/LevelMap.hx +++ b/src/common/haxe/ru/m/tankz/map/LevelMap.hx @@ -1,8 +1,14 @@ package ru.m.tankz.map; +import haxe.ds.HashMap; +import ru.m.tankz.map.Grid; +import ru.m.geom.Point; import ru.m.tankz.config.Config; -class LevelMap implements ILevelMap { + +class LevelMap { + + public var config(default, null):MapConfig; public var cellWidth(default, null):Float; public var cellHeight(default, null):Float; @@ -12,19 +18,47 @@ class LevelMap implements ILevelMap { public var bricks(default, null):Array; public var borders(default, null):Array; + public var grid(default, null):Grid; + public function new(config:MapConfig) { + this.config = config; cellWidth = config.cellWidth; cellHeight = config.cellHeight; gridWidth = config.gridWidth; gridHeight = config.gridHeight; borders = []; - bricks = Lambda.array(Lambda.mapi(config.bricks, function(i, type):Brick return new Brick(type, Std.int(i % gridWidth), Std.int(Math.floor(i / gridHeight))))); + grid = new Grid( + Std.int(cellWidth / 2), + Std.int(cellHeight / 2), + Std.int(cellWidth * gridWidth), + Std.int(cellHeight * gridHeight) + ); + bricks = Lambda.array(Lambda.mapi(config.bricks, function(i:Int, brickConfig:BrickConfig):Brick { + var cellX = Std.int(i % gridWidth); + var cellY = Std.int(Math.floor(i / gridHeight)); + var cells:HashMap = new HashMap(); + var point:Point = new Point(cellX * 2, cellY * 2); + for (x in 0...2) for (y in 0...2) { + var cell:GridCell = grid.cells.get(point); + cell.layer = brickConfig.layer; + cell.armor = brickConfig.armor; + } + return new Brick(config, brickConfig, cellX, cellY, cells); + })); + for (brick in bricks) { + var point:Point = new Point(brick.cellX * 2, brick.cellY * 2); + for (x in 0...2) for (y in 0...2) { + var cell:GridCell = grid.cells.get(point); + cell.layer = brick.config.layer; + cell.armor = brick.config.armor; + } + } } private function getBorderBrick(cellX:Int, cellY:Int):Brick { var index:Int = cellX + cellY * gridWidth; if (borders[index] == null) { - borders[index] = new Brick(Brick.BORDER, cellX, cellY); + borders[index] = new Brick(config, Brick.BORDER, cellX, cellY, null); } return borders[index]; } @@ -35,5 +69,4 @@ class LevelMap implements ILevelMap { } return bricks[cellY * gridWidth + cellX]; } - }