udpate
This commit is contained in:
@@ -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<Int, TankAction>;
|
||||
private var moveQueue:Array<Int>;
|
||||
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.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, [
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
@@ -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<T> {
|
||||
@@ -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);
|
||||
|
||||
@@ -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<Int, PlayerControl>;
|
||||
|
||||
public function init():Void {
|
||||
|
||||
@@ -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
|
||||
|
||||
16
src/common/haxe/ru/m/geom/Line.hx
Normal file
16
src/common/haxe/ru/m/geom/Line.hx
Normal 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}';
|
||||
}
|
||||
}
|
||||
@@ -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}';
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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<BrickConfig> = [];
|
||||
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;
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
@@ -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;
|
||||
}
|
||||
@@ -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;
|
||||
|
||||
|
||||
@@ -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<Int, IEntity>;
|
||||
public var entities(default, null):Map<Int, Entity>;
|
||||
public var removedEntities(default, null):Array<String>;
|
||||
|
||||
private var playerTanks(default, null):Map<Int, Tank>;
|
||||
@@ -45,7 +45,7 @@ class Engine implements IEngine {
|
||||
this.config = config;
|
||||
map = new LevelMap(config.source.map);
|
||||
playerTanks = new Map<Int, Tank>();
|
||||
entities = new Map<Int, IEntity>();
|
||||
entities = new Map<Int, Entity>();
|
||||
removedEntities = new Array<String>();
|
||||
time = Date.now().getTime();
|
||||
}
|
||||
@@ -130,15 +130,22 @@ class Engine implements IEngine {
|
||||
}
|
||||
}*/
|
||||
|
||||
private function getBricks(entity:IMobileEntity):Array<Brick> {
|
||||
var target:Point = entity.rect.getSide(entity.direction);
|
||||
/*private function getBricks(entity:MobileEntity):Array<Brick> {
|
||||
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<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> {
|
||||
var newTime:Float = Date.now().getTime();
|
||||
var d:Float = newTime - time;
|
||||
@@ -146,8 +153,8 @@ class Engine implements IEngine {
|
||||
|
||||
var changes = new Array<GameChange>();
|
||||
|
||||
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<Brick> = [];//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);
|
||||
}
|
||||
|
||||
@@ -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>;
|
||||
}
|
||||
@@ -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<Point, Bool>;
|
||||
public var rect(default, null):Rectangle;
|
||||
public var cells(default, null):HashMap<Point, GridCell>;
|
||||
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.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 {
|
||||
|
||||
53
src/common/haxe/ru/m/tankz/map/Grid.hx
Normal file
53
src/common/haxe/ru/m/tankz/map/Grid.hx
Normal 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 [];
|
||||
}
|
||||
}
|
||||
@@ -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;
|
||||
}
|
||||
@@ -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<Brick>;
|
||||
public var borders(default, null):Array<Brick>;
|
||||
|
||||
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<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 {
|
||||
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];
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user