diff --git a/src/client/haxe/ru/m/tankz/core/Entity.hx b/src/client/haxe/ru/m/tankz/core/Entity.hx index b7a13cf..2497746 100755 --- a/src/client/haxe/ru/m/tankz/core/Entity.hx +++ b/src/client/haxe/ru/m/tankz/core/Entity.hx @@ -1,13 +1,17 @@ package ru.m.tankz.core; +import flash.geom.Point; + class Entity implements IEntity { + public var x(default, default):Float; public var y(default, default):Float; public var width(default, default):Float; public var height(default, default):Float; - public function new() { - + public function new(position:Point) { + x = position.x; + y = position.y; } } diff --git a/src/client/haxe/ru/m/tankz/core/IMobileEntity.hx b/src/client/haxe/ru/m/tankz/core/IMobileEntity.hx index 69caf9b..cff6155 100755 --- a/src/client/haxe/ru/m/tankz/core/IMobileEntity.hx +++ b/src/client/haxe/ru/m/tankz/core/IMobileEntity.hx @@ -6,4 +6,7 @@ interface IMobileEntity extends IEntity { public var speed(default, null):Float; public var direction(default, default):Direction; + + public function move(direction:Direction):Void; + public function stop():Void; } diff --git a/src/client/haxe/ru/m/tankz/core/ITank.hx b/src/client/haxe/ru/m/tankz/core/ITank.hx index 286a129..c316fae 100755 --- a/src/client/haxe/ru/m/tankz/core/ITank.hx +++ b/src/client/haxe/ru/m/tankz/core/ITank.hx @@ -1,5 +1,8 @@ package ru.m.tankz.core; interface ITank extends IMobileEntity { + public var bullets:Array; + public function shot():Void; + public function destroyBullet(bullet:IMobileEntity):Void; } diff --git a/src/client/haxe/ru/m/tankz/core/MobileEntity.hx b/src/client/haxe/ru/m/tankz/core/MobileEntity.hx index cd2ee30..327db06 100755 --- a/src/client/haxe/ru/m/tankz/core/MobileEntity.hx +++ b/src/client/haxe/ru/m/tankz/core/MobileEntity.hx @@ -1,5 +1,6 @@ package ru.m.tankz.core; +import flash.geom.Point; class MobileEntity extends Entity implements IMobileEntity { public var mx(default, default):Float; @@ -8,9 +9,20 @@ class MobileEntity extends Entity implements IMobileEntity { public var speed(default, null):Float; public var direction(default, default):Direction; - public function new(speed:Float) { - super(); + public function new(position:Point, speed:Float, ?direction:Direction = null) { + super(position); 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; } } diff --git a/src/client/haxe/ru/m/tankz/core/Tank.hx b/src/client/haxe/ru/m/tankz/core/Tank.hx index 8202cb0..d740ad0 100755 --- a/src/client/haxe/ru/m/tankz/core/Tank.hx +++ b/src/client/haxe/ru/m/tankz/core/Tank.hx @@ -12,26 +12,25 @@ enum TankAction { class Tank extends MobileEntity implements ITank { + public var bullets:Array; + public function new(position:Point) { - super(4); - x = position.x; - y = position.y; + super(position, 4); + bullets = new Array(); width = 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 { + 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); } } diff --git a/src/client/haxe/ru/m/tankz/game/Tankz.hx b/src/client/haxe/ru/m/tankz/game/Tankz.hx index 6735c9a..87e89da 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.core.IMobileEntity; import flash.geom.Point; import flash.ui.Keyboard; 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.PlayerTank; import ru.m.tankz.map.TankzMap; -import ru.m.tankz.core.Tank; import ru.m.tankz.core.ITank; import ru.m.tankz.map.ITankzMap; @@ -35,12 +35,14 @@ class Tankz implements ITankz { Keyboard.S => TankAction.MOVE_BOTTOM, Keyboard.W => TankAction.MOVE_TOP, Keyboard.D => TankAction.MOVE_RIGHT, + Keyboard.SPACE => TankAction.SHOT ]), new PlayerTank(new Point(100, 0), [ Keyboard.LEFT => TankAction.MOVE_LEFT, Keyboard.DOWN => TankAction.MOVE_BOTTOM, Keyboard.UP => TankAction.MOVE_TOP, Keyboard.RIGHT => TankAction.MOVE_RIGHT, + Keyboard.SHIFT => TankAction.SHOT ]) ]; 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.y < 0) tank.y = 0; 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); + } } } } diff --git a/src/client/haxe/ru/m/tankz/render/Render.hx b/src/client/haxe/ru/m/tankz/render/Render.hx index ecfe7dd..bc2b5ef 100755 --- a/src/client/haxe/ru/m/tankz/render/Render.hx +++ b/src/client/haxe/ru/m/tankz/render/Render.hx @@ -58,6 +58,12 @@ class Render extends SpriteView implements IRender { tank.height / 4 ); g.endFill(); + + for (bullet in tank.bullets) { + g.beginFill(0xff0000); + g.drawRect(bullet.x, bullet.y, bullet.width, bullet.height); + g.endFill(); + } } } }