From 937ebd594570a366fb8e6f5b85a224258b8f033a Mon Sep 17 00:00:00 2001 From: shmyga Date: Mon, 25 Aug 2014 16:28:35 +0400 Subject: [PATCH] - --- res/layout/main.json | 2 +- .../haxe/ru/m/tankz/config/TankzConfig.hx | 12 +++++++ src/client/haxe/ru/m/tankz/core/Direction.hx | 8 +++++ .../haxe/ru/m/tankz/core/IMobileEntity.hx | 3 ++ .../haxe/ru/m/tankz/core/MobileEntity.hx | 7 +++- src/client/haxe/ru/m/tankz/core/Tank.hx | 16 ++++------ src/client/haxe/ru/m/tankz/game/ITankz.hx | 4 ++- src/client/haxe/ru/m/tankz/game/Tankz.hx | 32 +++++++++++-------- src/client/haxe/ru/m/tankz/map/ITankzMap.hx | 4 +-- src/client/haxe/ru/m/tankz/map/TankzMap.hx | 16 ++++++---- src/client/haxe/ru/m/tankz/render/Render.hx | 29 ++++++++++++++++- .../haxe/ru/m/tankz/view/frames/GameFrame.hx | 12 ++++++- 12 files changed, 107 insertions(+), 38 deletions(-) create mode 100644 src/client/haxe/ru/m/tankz/config/TankzConfig.hx diff --git a/res/layout/main.json b/res/layout/main.json index 0b5a459..a5a0e28 100755 --- a/res/layout/main.json +++ b/res/layout/main.json @@ -86,7 +86,7 @@ }, { "id":"render", "type":"ru.m.tankz.render.Render", - "pWidth":100, "pHeight":100 + "contentSize":true } ] } diff --git a/src/client/haxe/ru/m/tankz/config/TankzConfig.hx b/src/client/haxe/ru/m/tankz/config/TankzConfig.hx new file mode 100644 index 0000000..aef9576 --- /dev/null +++ b/src/client/haxe/ru/m/tankz/config/TankzConfig.hx @@ -0,0 +1,12 @@ +package ru.m.tankz.config; + +typedef MapConfig = { + var cellWidth:Float; + var cellHeight:Float; + var gridWidth:Int; + var gridHeight:Int; +} + +typedef TankzConfig = { + var map:MapConfig; +} diff --git a/src/client/haxe/ru/m/tankz/core/Direction.hx b/src/client/haxe/ru/m/tankz/core/Direction.hx index 5fbf437..a1bf855 100644 --- a/src/client/haxe/ru/m/tankz/core/Direction.hx +++ b/src/client/haxe/ru/m/tankz/core/Direction.hx @@ -1,6 +1,9 @@ package ru.m.tankz.core; +import haxe.ds.IntMap; + class Direction { + private static var directions:IntMap = new IntMap(); public static var LEFT(default, null) = new Direction(-1, 0); public static var TOP(default, null) = new Direction(0, -1); @@ -13,5 +16,10 @@ class Direction { public function new(x, y) { this.x = x; this.y = y; + directions.set(x + y * 10, this); + } + + public function reverse():Direction { + return directions.get(-x + (-y) * 10); } } diff --git a/src/client/haxe/ru/m/tankz/core/IMobileEntity.hx b/src/client/haxe/ru/m/tankz/core/IMobileEntity.hx index 78ec9b6..69caf9b 100755 --- a/src/client/haxe/ru/m/tankz/core/IMobileEntity.hx +++ b/src/client/haxe/ru/m/tankz/core/IMobileEntity.hx @@ -3,4 +3,7 @@ package ru.m.tankz.core; 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; } diff --git a/src/client/haxe/ru/m/tankz/core/MobileEntity.hx b/src/client/haxe/ru/m/tankz/core/MobileEntity.hx index 5f4b57b..cd2ee30 100755 --- a/src/client/haxe/ru/m/tankz/core/MobileEntity.hx +++ b/src/client/haxe/ru/m/tankz/core/MobileEntity.hx @@ -5,7 +5,12 @@ class MobileEntity extends Entity implements IMobileEntity { public var mx(default, default):Float; public var my(default, default):Float; - public function new() { + public var speed(default, null):Float; + public var direction(default, default):Direction; + + public function new(speed:Float) { super(); + this.speed = speed; + this.direction = Direction.BOTTOM; } } diff --git a/src/client/haxe/ru/m/tankz/core/Tank.hx b/src/client/haxe/ru/m/tankz/core/Tank.hx index 453b441..95d0da2 100755 --- a/src/client/haxe/ru/m/tankz/core/Tank.hx +++ b/src/client/haxe/ru/m/tankz/core/Tank.hx @@ -2,20 +2,16 @@ package ru.m.tankz.core; class Tank extends MobileEntity implements ITank { - private var speed:Float; - public function new() { - super(); - x = 0 + Math.random() * 50; - y = 0 + Math.random() * 50; - mx = 2 + Math.random() * 3; - my = 2 + Math.random() * 3; - speed = 4; - width = 20 + Math.random() * 10; - height = 20 + Math.random() * 10; + super(4); + x = 0; + y = 0; + width = 36; + height = 36; } public function move(direction:Direction):Void { + this.direction = direction; mx = direction.x * speed; my = direction.y * speed; } diff --git a/src/client/haxe/ru/m/tankz/game/ITankz.hx b/src/client/haxe/ru/m/tankz/game/ITankz.hx index 103513c..b15254b 100755 --- a/src/client/haxe/ru/m/tankz/game/ITankz.hx +++ b/src/client/haxe/ru/m/tankz/game/ITankz.hx @@ -1,13 +1,15 @@ package ru.m.tankz.game; +import ru.m.tankz.config.TankzConfig; import ru.m.tankz.core.ITank; import ru.m.tankz.map.ITankzMap; interface ITankz { + public var config(default, default):TankzConfig; public var map(default, null):ITankzMap; public var tanks(default, null):Array; public function clear():Void; - public function init():Void; + public function init(config:TankzConfig):Void; public function update():Void; } diff --git a/src/client/haxe/ru/m/tankz/game/Tankz.hx b/src/client/haxe/ru/m/tankz/game/Tankz.hx index b5f44b7..cad817c 100755 --- a/src/client/haxe/ru/m/tankz/game/Tankz.hx +++ b/src/client/haxe/ru/m/tankz/game/Tankz.hx @@ -1,5 +1,6 @@ package ru.m.tankz.game; +import ru.m.tankz.config.TankzConfig; import ru.m.tankz.core.PlayerTank; import ru.m.tankz.map.TankzMap; import ru.m.tankz.core.Tank; @@ -8,41 +9,44 @@ import ru.m.tankz.map.ITankzMap; class Tankz implements ITankz { + public var config(default, default):TankzConfig; public var map(default, null):ITankzMap; public var tanks(default, null):Array; private var x_limit:Float; private var y_limit:Float; - public function new() { - map = new TankzMap(); - } + public function new() {} public function clear():Void { tanks = []; } - public function init():Void { + public function init(config:TankzConfig):Void { + this.config = config; + map = new TankzMap(config.map); tanks = [ new PlayerTank(), new Tank() ]; - x_limit = map.width * map.cellWidth; - y_limit = map.height * map.cellHeight; + x_limit = map.gridWidth * map.cellWidth; + y_limit = map.gridHeight * map.cellHeight; } public function update():Void { for (tank in tanks) { + if (tank.direction.x != 0) { + tank.y = Math.round((tank.y + tank.height / 2) / config.map.cellHeight) * config.map.cellHeight - tank.height / 2; + } + if (tank.direction.y != 0) { + tank.x = Math.round((tank.x + tank.width / 2) / config.map.cellWidth) * config.map.cellWidth - tank.width / 2; + } tank.x += tank.mx; tank.y += tank.my; - if (tank.x < 0 || tank.x + tank.width > x_limit) { - tank.mx = - tank.mx; - tank.x += tank.mx; - } - if (tank.y < 0 || tank.y + tank.height > y_limit) { - tank.my = - tank.my; - tank.y += tank.my; - } + if (tank.x < 0) tank.x = 0; + if (tank.x + tank.width > x_limit) tank.x = x_limit - tank.width; + if (tank.y < 0) tank.y = 0; + if (tank.y + tank.height > y_limit) tank.y = y_limit - tank.height; } } } diff --git a/src/client/haxe/ru/m/tankz/map/ITankzMap.hx b/src/client/haxe/ru/m/tankz/map/ITankzMap.hx index b36f1dc..0a509f1 100755 --- a/src/client/haxe/ru/m/tankz/map/ITankzMap.hx +++ b/src/client/haxe/ru/m/tankz/map/ITankzMap.hx @@ -3,6 +3,6 @@ package ru.m.tankz.map; interface ITankzMap { public var cellWidth(default, null):Float; public var cellHeight(default, null):Float; - public var width(default, null):Int; - public var height(default, null):Int; + public var gridWidth(default, null):Int; + public var gridHeight(default, null):Int; } diff --git a/src/client/haxe/ru/m/tankz/map/TankzMap.hx b/src/client/haxe/ru/m/tankz/map/TankzMap.hx index d99fc59..44e062a 100755 --- a/src/client/haxe/ru/m/tankz/map/TankzMap.hx +++ b/src/client/haxe/ru/m/tankz/map/TankzMap.hx @@ -1,16 +1,18 @@ package ru.m.tankz.map; +import ru.m.tankz.config.TankzConfig.MapConfig; + class TankzMap implements ITankzMap { public var cellWidth(default, null):Float; public var cellHeight(default, null):Float; - public var width(default, null):Int; - public var height(default, null):Int; + public var gridWidth(default, null):Int; + public var gridHeight(default, null):Int; - public function new() { - cellWidth = 10; - cellHeight = 10; - width = 26; - height = 26; + public function new(config:MapConfig) { + cellWidth = config.cellWidth; + cellHeight = config.cellHeight; + gridWidth = config.gridWidth; + gridHeight = config.gridHeight; } } diff --git a/src/client/haxe/ru/m/tankz/render/Render.hx b/src/client/haxe/ru/m/tankz/render/Render.hx index 2d41113..ecfe7dd 100755 --- a/src/client/haxe/ru/m/tankz/render/Render.hx +++ b/src/client/haxe/ru/m/tankz/render/Render.hx @@ -19,18 +19,45 @@ class Render extends SpriteView implements IRender { } public function draw(game:ITankz):Void { + var mapWidth = game.map.gridWidth * game.map.cellWidth; + var mapHeight = game.map.gridHeight * game.map.cellHeight; + + if (contentSize) { + width = mapWidth; + height = mapHeight; + } + var g:Graphics = mapLayer.graphics; g.clear(); g.beginFill(0x00ff00); - g.drawRect(0,0,game.map.width*game.map.cellWidth,game.map.height*game.map.cellHeight); + g.drawRect(0, 0, mapWidth, mapHeight); g.endFill(); + g.lineStyle(1, 0x000000); + for (i in 0...game.map.gridWidth + 1) { + g.moveTo(i * game.map.cellWidth, 0); + g.lineTo(i * game.map.cellWidth, mapHeight); + } + for (i in 0...game.map.gridHeight + 1) { + g.moveTo(0, i * game.map.cellHeight); + g.lineTo(mapWidth, i * game.map.cellHeight); + } + g.lineStyle(); + var g:Graphics = tankLayer.graphics; g.clear(); for (tank in game.tanks) { g.beginFill(0xff0000); g.drawRect(tank.x, tank.y, tank.width, tank.height); g.endFill(); + g.beginFill(0x990000); + g.drawRect( + tank.x + tank.width / 2 - tank.width / 8 + tank.direction.x * tank.width / 4, + tank.y + tank.height / 2 - tank.height / 8 + tank.direction.y * tank.height / 4, + tank.width / 4, + tank.height / 4 + ); + g.endFill(); } } } 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 046d44e..b39c6ad 100755 --- a/src/client/haxe/ru/m/tankz/view/frames/GameFrame.hx +++ b/src/client/haxe/ru/m/tankz/view/frames/GameFrame.hx @@ -1,5 +1,6 @@ package ru.m.tankz.view.frames; +import ru.m.tankz.config.TankzConfig; import ru.m.tankz.render.IRender; import flash.events.Event; import ru.m.tankz.game.Tankz; @@ -34,7 +35,16 @@ class GameFrame extends VGroupView { public function onShow():Void { var person = Provider.get(GameData).person; findViewById("name", LabelView).text = person.name; - game.init(); + + var config:TankzConfig = { + map: { + cellWidth: 20, + cellHeight: 20, + gridWidth: 26, + gridHeight: 26 + } + }; + game.init(config); content.addEventListener(Event.ENTER_FRAME, updateGame); }