This commit is contained in:
2018-01-12 18:00:32 +03:00
parent b973d0830c
commit 8dc21b41c0
18 changed files with 235 additions and 141 deletions

View File

@@ -4,21 +4,21 @@ import haxe.Timer;
import ru.m.geom.Direction; import ru.m.geom.Direction;
import flash.events.FocusEvent; import flash.events.FocusEvent;
import flash.ui.Keyboard; import flash.ui.Keyboard;
import ru.m.tankz.engine.IEngine; import ru.m.tankz.engine.Engine;
import ru.m.tankz.core.Tank.TankAction; import ru.m.tankz.core.Tank.TankAction;
import flash.events.KeyboardEvent; import flash.events.KeyboardEvent;
import flash.Lib; import flash.Lib;
class PlayerControl { class PlayerControl {
private var engine:IEngine; private var engine:Engine;
private var id:Int; private var id:Int;
private var keyBinding:Map<Int, TankAction>; private var keyBinding:Map<Int, TankAction>;
private var moveQueue:Array<Int>; private var moveQueue:Array<Int>;
private var shotTimer:Timer; private var shotTimer:Timer;
public function new(id:Int, engine:IEngine, keyBinding:Map<Int, TankAction>) { public function new(id:Int, engine:Engine, keyBinding:Map<Int, TankAction>) {
this.id = id; this.id = id;
this.engine = engine; this.engine = engine;
this.keyBinding = keyBinding; this.keyBinding = keyBinding;
@@ -83,7 +83,7 @@ class PlayerControl {
engine.action(id, TankAction.SHOT); 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) { switch (index) {
case 0: case 0:
return new PlayerControl(tankId, engine, [ return new PlayerControl(tankId, engine, [

View File

@@ -1,10 +1,10 @@
package ru.m.tankz.render; package ru.m.tankz.render;
import ru.m.tankz.engine.Engine;
import haxework.gui.IView; import haxework.gui.IView;
import ru.m.tankz.engine.IEngine;
interface IRender extends IView { interface IRender extends IView {
public function draw(game:IEngine):Void; public function draw(game:Engine):Void;
public function reset():Void; public function reset():Void;
} }

View File

@@ -1,6 +1,7 @@
package ru.m.tankz.render; 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.core.Entity;
import ru.m.tankz.map.Brick; import ru.m.tankz.map.Brick;
import ru.m.geom.Direction; import ru.m.geom.Direction;
@@ -11,7 +12,6 @@ import ru.m.tankz.core.Tank;
import flash.display.Sprite; import flash.display.Sprite;
import flash.display.Graphics; import flash.display.Graphics;
import haxework.gui.SpriteView; import haxework.gui.SpriteView;
import ru.m.tankz.engine.IEngine;
interface IState<T> { interface IState<T> {
@@ -81,7 +81,7 @@ class Render extends SpriteView implements IRender {
reset(); reset();
} }
private function invalidateLayers(game:IEngine):Void { private function invalidateLayers(game:Engine):Void {
for (brick in game.map.bricks) { for (brick in game.map.bricks) {
if (!states.exists(brick.key)) { if (!states.exists(brick.key)) {
states[brick.key] = new BrickState(); 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 mapWidth = game.map.gridWidth * game.map.cellWidth;
var mapHeight = game.map.gridHeight * game.map.cellHeight; 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]) { if (layersForUpdate[groundLayer] || layersForUpdate[upLayer]) {
groundLayer.graphics.clear(); groundLayer.graphics.clear();
upLayer.graphics.clear(); upLayer.graphics.clear();
@@ -145,28 +145,36 @@ class Render extends SpriteView implements IRender {
game.map.cellWidth, game.map.cellWidth,
game.map.cellHeight game.map.cellHeight
); );
if (brick.config.breakable) { for (point in brick.cells.keys()) {
for (point in brick.breaked.keys()) { if (brick.cells.get(point).destroyed) {
if (brick.breaked.get(point)) { g.beginFill(0x000000);
g.beginFill(0x000000); g.drawRect(
g.drawRect( brick.cellX * game.map.cellWidth + point.x * game.map.cellWidth / 2,
brick.cellX * game.map.cellWidth + point.x * game.map.cellWidth / 2, brick.cellY * game.map.cellHeight + point.y * game.map.cellHeight / 2,
brick.cellY * game.map.cellHeight + point.y * game.map.cellHeight / 2, game.map.cellWidth / 2,
game.map.cellWidth / 2, game.map.cellHeight / 2
game.map.cellHeight / 2 );
);
}
} }
} }
g.endFill(); 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[groundLayer] = false;
layersForUpdate[upLayer] = false; layersForUpdate[upLayer] = false;
} }
} }
public function drawEntities(game:IEngine):Void { public function drawEntities(game:Engine):Void {
if (layersForUpdate[entryLayer]) { if (layersForUpdate[entryLayer]) {
var g:Graphics = entryLayer.graphics; var g:Graphics = entryLayer.graphics;
g.clear(); 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'; image = 'resources/images/tank/player/tank_p${tank.config.level}_${tank.index}-0.png';
} else if (Std.is(ent, Bullet)) { } else if (Std.is(ent, Bullet)) {
var bullet:Bullet = cast ent; 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 { } else {
image = 'ERROR'; // ToDo: image = 'ERROR'; // ToDo:
} }
var m = new Matrix(); var m = new Matrix();
if (Std.is(ent, IMobileEntity)) { if (Std.is(ent, MobileEntity)) {
m.rotate(calcRotate(cast(ent, IMobileEntity).direction)); m.rotate(calcRotate(cast(ent, MobileEntity).direction));
} }
m.translate(ent.rect.x, ent.rect.y); m.translate(ent.rect.x, ent.rect.y);
g.beginBitmapFill(Assets.getBitmapData(image), m, true, true); 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); invalidateLayers(game);
drawBackground(game); drawBackground(game);
drawMap(game); drawMap(game);

View File

@@ -4,7 +4,6 @@ import ru.m.tankz.proto.core.GameType;
import ru.m.tankz.proto.core.Game; import ru.m.tankz.proto.core.Game;
import ru.m.tankz.core.PlayerControl; import ru.m.tankz.core.PlayerControl;
import ru.m.tankz.engine.Engine; import ru.m.tankz.engine.Engine;
import ru.m.tankz.engine.IEngine;
import protohx.Message; import protohx.Message;
import ru.m.tankz.proto.pack.GameUpdateResponse; import ru.m.tankz.proto.pack.GameUpdateResponse;
import ru.m.connect.IConnection; import ru.m.connect.IConnection;
@@ -21,7 +20,7 @@ class GameFrame extends VGroupView implements ViewBuilder implements IPacketHand
public static inline var ID = "game"; public static inline var ID = "game";
private var engine:IEngine; private var engine:Engine;
private var controls:Map<Int, PlayerControl>; private var controls:Map<Int, PlayerControl>;
public function init():Void { public function init():Void {

View File

@@ -20,7 +20,7 @@ bricks:
-1: &brick-boder -1: &brick-boder
type: -1 type: -1
layer: 2 layer: 2
armor: 3 armor: -1
0: &brick-none 0: &brick-none
type: 0 type: 0
layer: 0 layer: 0
@@ -45,7 +45,6 @@ bricks:
type: 5 type: 5
layer: 2 layer: 2
armor: 1 armor: 1
breakable: yes
bullet: &bullet bullet: &bullet
width: 12 width: 12
@@ -81,7 +80,7 @@ tanks:
bullet: bullet:
<<: *bullet <<: *bullet
speed: 9.0 speed: 9.0
bullets: 2 bullets: 3
3: 3:
level: 3 level: 3
width: 42 width: 42
@@ -90,5 +89,5 @@ tanks:
bullet: bullet:
<<: *bullet <<: *bullet
speed: 9.0 speed: 9.0
piercing: 2 piercing: 3
bullets: 2 bullets: 2

View File

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

View File

@@ -26,8 +26,22 @@ class Rectangle {
return value; return value;
} }
public function getSide(direction:Direction):Point { public function getSide(direction:Direction):Line {
return center.add(new Point(direction.x * width / 2, direction.y * height / 2)); 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 { public function set_direction(value:Direction):Direction {
@@ -43,4 +57,7 @@ class Rectangle {
return value; return value;
} }
public function toString():String {
return 'Rectangle{x=$x,y=$y,width=$width,height=$height}';
}
} }

View File

@@ -34,7 +34,6 @@ typedef BrickConfig = {
var type:Int; var type:Int;
var layer:Int; var layer:Int;
var armor:Int; var armor:Int;
var breakable:Bool;
} }
typedef BulletConfig = { typedef BulletConfig = {
@@ -120,7 +119,9 @@ class ConfigBundle {
var bricks:Array<BrickConfig> = []; var bricks:Array<BrickConfig> = [];
for (line in ~/\s+/g.split(bricksData)) { for (line in ~/\s+/g.split(bricksData)) {
for (c in line.split('')) { 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; source.map.bricks = bricks;

View File

@@ -3,7 +3,7 @@ package ru.m.tankz.core;
import ru.m.geom.Rectangle; import ru.m.geom.Rectangle;
class Entity implements IEntity { 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;

View File

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

View File

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

View File

@@ -4,7 +4,7 @@ import ru.m.geom.Direction;
import ru.m.geom.Rectangle; import ru.m.geom.Rectangle;
class MobileEntity extends Entity implements IMobileEntity { class MobileEntity extends Entity {
public var mx(default, default):Float = 0; public var mx(default, default):Float = 0;
public var my(default, default):Float = 0; public var my(default, default):Float = 0;

View File

@@ -1,8 +1,10 @@
package ru.m.tankz.engine; 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.geom.Direction;
import ru.m.tankz.config.Config.TankConfig; import ru.m.tankz.config.Config.TankConfig;
import ru.m.tankz.core.IEntity;
import ru.m.geom.Point; import ru.m.geom.Point;
import ru.m.tankz.map.Brick; import ru.m.tankz.map.Brick;
import ru.m.tankz.core.Bullet; 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.GameChangeType;
import ru.m.tankz.proto.game.GameChange; import ru.m.tankz.proto.game.GameChange;
import ru.m.tankz.proto.core.Player; import ru.m.tankz.proto.core.Player;
import ru.m.tankz.core.IMobileEntity;
import ru.m.tankz.config.Config; import ru.m.tankz.config.Config;
import ru.m.tankz.core.Tank; import ru.m.tankz.core.Tank;
import ru.m.tankz.map.LevelMap; 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 config(default, default):Config;
public var map(default, null):ILevelMap; public var map(default, null):LevelMap;
public var entities(default, null):Map<Int, IEntity>; public var entities(default, null):Map<Int, Entity>;
public var removedEntities(default, null):Array<String>; public var removedEntities(default, null):Array<String>;
private var playerTanks(default, null):Map<Int, Tank>; private var playerTanks(default, null):Map<Int, Tank>;
@@ -45,7 +45,7 @@ class Engine implements IEngine {
this.config = config; this.config = config;
map = new LevelMap(config.source.map); map = new LevelMap(config.source.map);
playerTanks = new Map<Int, Tank>(); playerTanks = new Map<Int, Tank>();
entities = new Map<Int, IEntity>(); entities = new Map<Int, Entity>();
removedEntities = new Array<String>(); removedEntities = new Array<String>();
time = Date.now().getTime(); time = Date.now().getTime();
} }
@@ -130,15 +130,22 @@ class Engine implements IEngine {
} }
}*/ }*/
private function getBricks(entity:IMobileEntity):Array<Brick> { /*private function getBricks(entity:MobileEntity):Array<Brick> {
var target:Point = entity.rect.getSide(entity.direction); var target:Point = entity.rect.getSide(entity.rect.direction);
var cellX:Int = Math.floor(target.x / map.cellWidth); var cellX:Int = Math.floor(target.x / map.cellWidth);
var cellY:Int = Math.floor(target.y / map.cellHeight); var cellY:Int = Math.floor(target.y / map.cellHeight);
var brick1 = map.getBrick(cellX, cellY); 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]; return [brick1, brick2];
} }
public function checkBrick(entity:MobileEntity, brick:Brick):Null<Point> {
if (0 < brick.config.layer && brick.config.layer < 3) {
return brick.rect.getSide(entity.rect.direction.reverse());
}
return null;
}*/
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,8 +153,8 @@ class Engine implements IEngine {
var changes = new Array<GameChange>(); var changes = new Array<GameChange>();
for (ent in entities) if (Std.is(ent, IMobileEntity)) { for (ent in entities) if (Std.is(ent, MobileEntity)) {
var entity:IMobileEntity = cast ent; var entity:MobileEntity = cast ent;
var objectType = -1; var objectType = -1;
if (Std.is(entity, Tank)) objectType = GameObjectType.TANK; if (Std.is(entity, Tank)) objectType = GameObjectType.TANK;
@@ -165,16 +172,20 @@ class Engine implements IEngine {
if (entity.mx != 0 || entity.my != 0) { if (entity.mx != 0 || entity.my != 0) {
entity.rect.x += entity.mx * (d / 30); entity.rect.x += entity.mx * (d / 30);
entity.rect.y += entity.my * (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<Brick> = [];//getBricks(entity);
if (objectType == GameObjectType.TANK) { if (objectType == GameObjectType.TANK) {
for (brick in bricks) { for (brick in bricks) {
if (0 < brick.config.layer && brick.config.layer < 3) { var point:Point = null;//checkBrick(entity, brick);
if (entity.direction.x != 0) { if (point != null) {
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; 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) { if (entity.rect.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; entity.rect.y = point.y - entity.direction.y * entity.rect.height / 2 - entity.rect.height / 2;
} }
break; break;
} }
@@ -201,8 +212,14 @@ class Engine implements IEngine {
var d:Direction = ent.rect.direction; var d:Direction = ent.rect.direction;
for (i in 0...bricks.length) { for (i in 0...bricks.length) {
var brick = bricks[i]; var brick = bricks[i];
if (brick.config.armor > 0 && brick.config.armor <= bullet.config.piercing) { if (brick.config.armor != 0) {
if (brick.config.breakable && brick.config.armor == bullet.config.piercing) { 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); var p1:Point = new Point(0, 0);
if (d.x > 0 || d.y > 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)); 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)); 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; collision = false;
} else { } else {
brick.breaked.set(p1, true); brick.blocks.set(p1, true);
brick.breaked.set(p2, true); brick.blocks.set(p2, true);
} }
if (brick.destroyed) { if (brick.destroyed) {
brick.config = config.getBrick(0); brick.config = config.getBrick(0);
@@ -250,16 +267,16 @@ class Engine implements IEngine {
} else { } else {
brick.config = config.getBrick(0); brick.config = config.getBrick(0);
} }
} }*/
} }
if (collision) { if (collision) {
entities.remove(entity.id); entities.remove(entity.id);
var tank:Tank = cast entities.get(bullet.tankId); var tank:Tank = cast entities.get(bullet.tankId);
tank.onDestroyBullet(); tank.onDestroyBullet();
changes.push(new GameChange() changes.push(new GameChange()
.setType(GameChangeType.DESTROED) .setType(GameChangeType.DESTROED)
.setObjectType(objectType) .setObjectType(objectType)
.setObjectId(entity.id) .setObjectId(entity.id)
); );
removedEntities.push(entity.key); removedEntities.push(entity.key);
} }

View File

@@ -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<Int, IEntity>;
public var removedEntities(default, null):Array<String>;
public function clear():Void;
public function init(config:Config):Void;
public function initTanks(players:Array<Player>):Array<GameChange>;
public function action(tankId:Int, action:TankAction):Void;
//public function updateFromChanges(changes:Array<GameChange>):Void;
public function update():Array<GameChange>;
}

View File

@@ -1,8 +1,10 @@
package ru.m.tankz.map; package ru.m.tankz.map;
import ru.m.tankz.map.Grid.GridCell;
import ru.m.geom.Rectangle;
import haxe.ds.HashMap; import haxe.ds.HashMap;
import ru.m.geom.Point; import ru.m.geom.Point;
import ru.m.tankz.config.Config.BrickConfig; import ru.m.tankz.config.Config;
import ru.m.tankz.core.IKey; import ru.m.tankz.core.IKey;
@@ -10,33 +12,42 @@ class Brick implements IKey {
public static var BORDER:BrickConfig = { public static var BORDER:BrickConfig = {
type: -1, type: -1,
layer: 2, layer: 2,
armor: 3, armor: -1,
breakable: false,
} }
public var cellX(default, null):Int; public var cellX(default, null):Int;
public var cellY(default, null):Int; public var cellY(default, null):Int;
public var key(get, null):String; public var key(get, null):String;
public var mapConfig(default, null):MapConfig;
public var config(default, default):BrickConfig; public var config(default, default):BrickConfig;
public var breaked:HashMap<Point, Bool>; public var rect(default, null):Rectangle;
public var cells(default, null):HashMap<Point, GridCell>;
public var destroyed(get, null):Bool; 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<Point, GridCell>) {
this.cellX = cellX; this.cellX = cellX;
this.cellY = cellY; this.cellY = cellY;
this.cells = cells;
this.mapConfig = mapConfig;
this.config = config; 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 { public function get_destroyed():Bool {
var i = 0; var i = 0;
for (k in breaked.keys()) { for (c in cells.iterator()) {
if (++i >=4) { if (!c.destroyed) {
return true; return false;
} }
} }
return false; return true;
} }
public function get_key():String { public function get_key():String {

View File

@@ -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<Point, GridCell>;
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<GridCell> {
return [];
}
}

View File

@@ -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<Brick>;
public function getBrick(cellX:Int, cellY:Int):Brick;
}

View File

@@ -1,8 +1,14 @@
package ru.m.tankz.map; 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; 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 cellWidth(default, null):Float;
public var cellHeight(default, null):Float; public var cellHeight(default, null):Float;
@@ -12,19 +18,47 @@ class LevelMap implements ILevelMap {
public var bricks(default, null):Array<Brick>; public var bricks(default, null):Array<Brick>;
public var borders(default, null):Array<Brick>; public var borders(default, null):Array<Brick>;
public var grid(default, null):Grid;
public function new(config:MapConfig) { public function new(config:MapConfig) {
this.config = config;
cellWidth = config.cellWidth; cellWidth = config.cellWidth;
cellHeight = config.cellHeight; cellHeight = config.cellHeight;
gridWidth = config.gridWidth; gridWidth = config.gridWidth;
gridHeight = config.gridHeight; gridHeight = config.gridHeight;
borders = []; 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<Point, GridCell> = 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 { private function getBorderBrick(cellX:Int, cellY:Int):Brick {
var index:Int = cellX + cellY * gridWidth; var index:Int = cellX + cellY * gridWidth;
if (borders[index] == null) { 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]; return borders[index];
} }
@@ -35,5 +69,4 @@ class LevelMap implements ILevelMap {
} }
return bricks[cellY * gridWidth + cellX]; return bricks[cellY * gridWidth + cellX];
} }
} }