This commit is contained in:
2014-08-25 17:53:38 +04:00
parent deefba434e
commit 6ea58d043c
7 changed files with 77 additions and 20 deletions

View File

@@ -1,13 +1,17 @@
package ru.m.tankz.core; package ru.m.tankz.core;
import flash.geom.Point;
class Entity implements IEntity { class Entity implements IEntity {
public var x(default, default):Float; public var x(default, default):Float;
public var y(default, default):Float; public var y(default, default):Float;
public var width(default, default):Float; public var width(default, default):Float;
public var height(default, default):Float; public var height(default, default):Float;
public function new() { public function new(position:Point) {
x = position.x;
y = position.y;
} }
} }

View File

@@ -6,4 +6,7 @@ interface IMobileEntity extends IEntity {
public var speed(default, null):Float; public var speed(default, null):Float;
public var direction(default, default):Direction; public var direction(default, default):Direction;
public function move(direction:Direction):Void;
public function stop():Void;
} }

View File

@@ -1,5 +1,8 @@
package ru.m.tankz.core; package ru.m.tankz.core;
interface ITank extends IMobileEntity { interface ITank extends IMobileEntity {
public var bullets:Array<IMobileEntity>;
public function shot():Void; public function shot():Void;
public function destroyBullet(bullet:IMobileEntity):Void;
} }

View File

@@ -1,5 +1,6 @@
package ru.m.tankz.core; package ru.m.tankz.core;
import flash.geom.Point;
class MobileEntity extends Entity implements IMobileEntity { class MobileEntity extends Entity implements IMobileEntity {
public var mx(default, default):Float; public var mx(default, default):Float;
@@ -8,9 +9,20 @@ class MobileEntity extends Entity implements IMobileEntity {
public var speed(default, null):Float; public var speed(default, null):Float;
public var direction(default, default):Direction; public var direction(default, default):Direction;
public function new(speed:Float) { public function new(position:Point, speed:Float, ?direction:Direction = null) {
super(); super(position);
this.speed = speed; this.speed = speed;
this.direction = Direction.BOTTOM; this.direction = direction == null ? Direction.BOTTOM : direction;
}
public function move(direction:Direction):Void {
this.direction = direction;
mx = direction.x * speed;
my = direction.y * speed;
}
public function stop():Void {
mx = 0;
my = 0;
} }
} }

View File

@@ -12,26 +12,25 @@ enum TankAction {
class Tank extends MobileEntity implements ITank { class Tank extends MobileEntity implements ITank {
public var bullets:Array<IMobileEntity>;
public function new(position:Point) { public function new(position:Point) {
super(4); super(position, 4);
x = position.x; bullets = new Array<IMobileEntity>();
y = position.y;
width = 34; width = 34;
height = 34; height = 34;
} }
public function move(direction:Direction):Void {
this.direction = direction;
mx = direction.x * speed;
my = direction.y * speed;
}
public function stop():Void {
mx = 0;
my = 0;
}
public function shot():Void { public function shot():Void {
if (bullets.length >= 5) return;
var bullet = new MobileEntity(new Point(x + width / 2 - 5, y + height / 2 - 5), 6, direction);
bullet.width = 10;
bullet.height = 10;
bullet.move(direction);
bullets.push(bullet);
}
public function destroyBullet(bullet:IMobileEntity):Void {
bullets.remove(bullet);
} }
} }

View File

@@ -1,5 +1,6 @@
package ru.m.tankz.game; package ru.m.tankz.game;
import ru.m.tankz.core.IMobileEntity;
import flash.geom.Point; import flash.geom.Point;
import flash.ui.Keyboard; import flash.ui.Keyboard;
import flash.geom.Rectangle; import flash.geom.Rectangle;
@@ -7,7 +8,6 @@ import ru.m.tankz.config.TankzConfig;
import ru.m.tankz.core.Tank; import ru.m.tankz.core.Tank;
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.ITank; import ru.m.tankz.core.ITank;
import ru.m.tankz.map.ITankzMap; import ru.m.tankz.map.ITankzMap;
@@ -35,12 +35,14 @@ class Tankz implements ITankz {
Keyboard.S => TankAction.MOVE_BOTTOM, Keyboard.S => TankAction.MOVE_BOTTOM,
Keyboard.W => TankAction.MOVE_TOP, Keyboard.W => TankAction.MOVE_TOP,
Keyboard.D => TankAction.MOVE_RIGHT, Keyboard.D => TankAction.MOVE_RIGHT,
Keyboard.SPACE => TankAction.SHOT
]), ]),
new PlayerTank(new Point(100, 0), [ new PlayerTank(new Point(100, 0), [
Keyboard.LEFT => TankAction.MOVE_LEFT, Keyboard.LEFT => TankAction.MOVE_LEFT,
Keyboard.DOWN => TankAction.MOVE_BOTTOM, Keyboard.DOWN => TankAction.MOVE_BOTTOM,
Keyboard.UP => TankAction.MOVE_TOP, Keyboard.UP => TankAction.MOVE_TOP,
Keyboard.RIGHT => TankAction.MOVE_RIGHT, Keyboard.RIGHT => TankAction.MOVE_RIGHT,
Keyboard.SHIFT => TankAction.SHOT
]) ])
]; ];
x_limit = map.gridWidth * map.cellWidth; x_limit = map.gridWidth * map.cellWidth;
@@ -80,6 +82,34 @@ class Tankz implements ITankz {
if (tank.x + tank.width > x_limit) tank.x = x_limit - tank.width; if (tank.x + tank.width > x_limit) tank.x = x_limit - tank.width;
if (tank.y < 0) tank.y = 0; if (tank.y < 0) tank.y = 0;
if (tank.y + tank.height > y_limit) tank.y = y_limit - tank.height; if (tank.y + tank.height > y_limit) tank.y = y_limit - tank.height;
updateBullets(tank);
}
}
private function updateBullets(tank:ITank):Void {
for (bullet in tank.bullets) {
bullet.x += bullet.mx;
bullet.y += bullet.my;
var bulletR = new Rectangle(bullet.x, bullet.y, bullet.width, bullet.height);
var i = 0;
while (i < tanks.length) {
var t = tanks[i++];
if (t != tank) {
var r = new Rectangle(t.x, t.y, t.width, t.height);
if (bulletR.intersects(r)) {
tank.destroyBullet(bullet);
tanks.remove(t);
i--;
}
}
}
if (bullet.x < 0 || bullet.x + bullet.width > x_limit || bullet.y < 0 || bullet.y + bullet.height > y_limit) {
tank.destroyBullet(bullet);
}
} }
} }
} }

View File

@@ -58,6 +58,12 @@ class Render extends SpriteView implements IRender {
tank.height / 4 tank.height / 4
); );
g.endFill(); g.endFill();
for (bullet in tank.bullets) {
g.beginFill(0xff0000);
g.drawRect(bullet.x, bullet.y, bullet.width, bullet.height);
g.endFill();
}
} }
} }
} }