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

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

View File

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

View File

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

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;
class MobileEntity extends Entity implements IMobileEntity {
class MobileEntity extends Entity {
public var mx(default, default):Float = 0;
public var my(default, default):Float = 0;

View File

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

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;
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 {

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