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

@@ -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;
import haxe.ds.IntMap;
class Direction {
private static var directions:IntMap<Direction> = new IntMap<Direction>();
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);
}
}

View File

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

View File

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

View File

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

View File

@@ -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<ITank>;
public function clear():Void;
public function init():Void;
public function init(config:TankzConfig):Void;
public function update():Void;
}

View File

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

View File

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

View File

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

View File

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

View File

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