This commit is contained in:
2014-08-25 16:28:35 +04:00
parent 5855b723a8
commit 937ebd5945
12 changed files with 107 additions and 38 deletions

View File

@@ -86,7 +86,7 @@
}, },
{ {
"id":"render", "type":"ru.m.tankz.render.Render", "id":"render", "type":"ru.m.tankz.render.Render",
"pWidth":100, "pHeight":100 "contentSize":true
} }
] ]
} }

View File

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

View File

@@ -1,6 +1,9 @@
package ru.m.tankz.core; package ru.m.tankz.core;
import haxe.ds.IntMap;
class Direction { class Direction {
private static var directions:IntMap<Direction> = new IntMap<Direction>();
public static var LEFT(default, null) = new Direction(-1, 0); public static var LEFT(default, null) = new Direction(-1, 0);
public static var TOP(default, null) = new Direction(0, -1); public static var TOP(default, null) = new Direction(0, -1);
@@ -13,5 +16,10 @@ class Direction {
public function new(x, y) { public function new(x, y) {
this.x = x; this.x = x;
this.y = y; this.y = y;
directions.set(x + y * 10, this);
}
public function reverse():Direction {
return directions.get(-x + (-y) * 10);
} }
} }

View File

@@ -3,4 +3,7 @@ package ru.m.tankz.core;
interface IMobileEntity extends IEntity { interface IMobileEntity extends IEntity {
public var mx(default, default):Float; public var mx(default, default):Float;
public var my(default, default):Float; public var my(default, default):Float;
public var speed(default, null):Float;
public var direction(default, default):Direction;
} }

View File

@@ -5,7 +5,12 @@ class MobileEntity extends Entity implements IMobileEntity {
public var mx(default, default):Float; public var mx(default, default):Float;
public var my(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(); super();
this.speed = speed;
this.direction = Direction.BOTTOM;
} }
} }

View File

@@ -2,20 +2,16 @@ package ru.m.tankz.core;
class Tank extends MobileEntity implements ITank { class Tank extends MobileEntity implements ITank {
private var speed:Float;
public function new() { public function new() {
super(); super(4);
x = 0 + Math.random() * 50; x = 0;
y = 0 + Math.random() * 50; y = 0;
mx = 2 + Math.random() * 3; width = 36;
my = 2 + Math.random() * 3; height = 36;
speed = 4;
width = 20 + Math.random() * 10;
height = 20 + Math.random() * 10;
} }
public function move(direction:Direction):Void { public function move(direction:Direction):Void {
this.direction = direction;
mx = direction.x * speed; mx = direction.x * speed;
my = direction.y * speed; my = direction.y * speed;
} }

View File

@@ -1,13 +1,15 @@
package ru.m.tankz.game; package ru.m.tankz.game;
import ru.m.tankz.config.TankzConfig;
import ru.m.tankz.core.ITank; import ru.m.tankz.core.ITank;
import ru.m.tankz.map.ITankzMap; import ru.m.tankz.map.ITankzMap;
interface ITankz { interface ITankz {
public var config(default, default):TankzConfig;
public var map(default, null):ITankzMap; public var map(default, null):ITankzMap;
public var tanks(default, null):Array<ITank>; public var tanks(default, null):Array<ITank>;
public function clear():Void; public function clear():Void;
public function init():Void; public function init(config:TankzConfig):Void;
public function update():Void; public function update():Void;
} }

View File

@@ -1,5 +1,6 @@
package ru.m.tankz.game; package ru.m.tankz.game;
import ru.m.tankz.config.TankzConfig;
import ru.m.tankz.core.PlayerTank; import ru.m.tankz.core.PlayerTank;
import ru.m.tankz.map.TankzMap; import ru.m.tankz.map.TankzMap;
import ru.m.tankz.core.Tank; import ru.m.tankz.core.Tank;
@@ -8,41 +9,44 @@ import ru.m.tankz.map.ITankzMap;
class Tankz implements ITankz { class Tankz implements ITankz {
public var config(default, default):TankzConfig;
public var map(default, null):ITankzMap; public var map(default, null):ITankzMap;
public var tanks(default, null):Array<ITank>; public var tanks(default, null):Array<ITank>;
private var x_limit:Float; private var x_limit:Float;
private var y_limit:Float; private var y_limit:Float;
public function new() { public function new() {}
map = new TankzMap();
}
public function clear():Void { public function clear():Void {
tanks = []; tanks = [];
} }
public function init():Void { public function init(config:TankzConfig):Void {
this.config = config;
map = new TankzMap(config.map);
tanks = [ tanks = [
new PlayerTank(), new PlayerTank(),
new Tank() new Tank()
]; ];
x_limit = map.width * map.cellWidth; x_limit = map.gridWidth * map.cellWidth;
y_limit = map.height * map.cellHeight; y_limit = map.gridHeight * map.cellHeight;
} }
public function update():Void { public function update():Void {
for (tank in tanks) { 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.x += tank.mx;
tank.y += tank.my; tank.y += tank.my;
if (tank.x < 0 || tank.x + tank.width > x_limit) { if (tank.x < 0) tank.x = 0;
tank.mx = - tank.mx; if (tank.x + tank.width > x_limit) tank.x = x_limit - tank.width;
tank.x += tank.mx; if (tank.y < 0) tank.y = 0;
} if (tank.y + tank.height > y_limit) tank.y = y_limit - tank.height;
if (tank.y < 0 || tank.y + tank.height > y_limit) {
tank.my = - tank.my;
tank.y += tank.my;
}
} }
} }
} }

View File

@@ -3,6 +3,6 @@ package ru.m.tankz.map;
interface ITankzMap { interface ITankzMap {
public var cellWidth(default, null):Float; public var cellWidth(default, null):Float;
public var cellHeight(default, null):Float; public var cellHeight(default, null):Float;
public var width(default, null):Int; public var gridWidth(default, null):Int;
public var height(default, null):Int; public var gridHeight(default, null):Int;
} }

View File

@@ -1,16 +1,18 @@
package ru.m.tankz.map; package ru.m.tankz.map;
import ru.m.tankz.config.TankzConfig.MapConfig;
class TankzMap implements ITankzMap { class TankzMap implements ITankzMap {
public var cellWidth(default, null):Float; public var cellWidth(default, null):Float;
public var cellHeight(default, null):Float; public var cellHeight(default, null):Float;
public var width(default, null):Int; public var gridWidth(default, null):Int;
public var height(default, null):Int; public var gridHeight(default, null):Int;
public function new() { public function new(config:MapConfig) {
cellWidth = 10; cellWidth = config.cellWidth;
cellHeight = 10; cellHeight = config.cellHeight;
width = 26; gridWidth = config.gridWidth;
height = 26; gridHeight = config.gridHeight;
} }
} }

View File

@@ -19,18 +19,45 @@ class Render extends SpriteView implements IRender {
} }
public function draw(game:ITankz):Void { 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; var g:Graphics = mapLayer.graphics;
g.clear(); g.clear();
g.beginFill(0x00ff00); 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.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; var g:Graphics = tankLayer.graphics;
g.clear(); g.clear();
for (tank in game.tanks) { for (tank in game.tanks) {
g.beginFill(0xff0000); g.beginFill(0xff0000);
g.drawRect(tank.x, tank.y, tank.width, tank.height); g.drawRect(tank.x, tank.y, tank.width, tank.height);
g.endFill(); 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();
} }
} }
} }

View File

@@ -1,5 +1,6 @@
package ru.m.tankz.view.frames; package ru.m.tankz.view.frames;
import ru.m.tankz.config.TankzConfig;
import ru.m.tankz.render.IRender; import ru.m.tankz.render.IRender;
import flash.events.Event; import flash.events.Event;
import ru.m.tankz.game.Tankz; import ru.m.tankz.game.Tankz;
@@ -34,7 +35,16 @@ class GameFrame extends VGroupView {
public function onShow():Void { public function onShow():Void {
var person = Provider.get(GameData).person; var person = Provider.get(GameData).person;
findViewById("name", LabelView).text = person.name; 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); content.addEventListener(Event.ENTER_FRAME, updateGame);
} }