diff --git a/src/client/haxe/ru/m/tankz/core/Direction.hx b/src/client/haxe/ru/m/tankz/core/Direction.hx new file mode 100644 index 0000000..5fbf437 --- /dev/null +++ b/src/client/haxe/ru/m/tankz/core/Direction.hx @@ -0,0 +1,17 @@ +package ru.m.tankz.core; + +class Direction { + + public static var LEFT(default, null) = new Direction(-1, 0); + public static var TOP(default, null) = new Direction(0, -1); + public static var RIGHT(default, null) = new Direction(1, 0); + public static var BOTTOM(default, null) = new Direction(0, 1); + + public var x(default, null):Int; + public var y(default, null):Int; + + public function new(x, y) { + this.x = x; + this.y = y; + } +} diff --git a/src/client/haxe/ru/m/tankz/core/PlayerTank.hx b/src/client/haxe/ru/m/tankz/core/PlayerTank.hx new file mode 100644 index 0000000..1be177f --- /dev/null +++ b/src/client/haxe/ru/m/tankz/core/PlayerTank.hx @@ -0,0 +1,28 @@ +package ru.m.tankz.core; + +import flash.ui.Keyboard; +import flash.events.KeyboardEvent; +import flash.Lib; + +class PlayerTank extends Tank { + + public function new() { + super(); + + Lib.current.stage.addEventListener(KeyboardEvent.KEY_DOWN, onKeyDown); + Lib.current.stage.addEventListener(KeyboardEvent.KEY_UP, onKeyUp); + } + + private function onKeyDown(event:KeyboardEvent):Void { + switch (event.keyCode) { + case Keyboard.A: move(Direction.LEFT); + case Keyboard.S: move(Direction.BOTTOM); + case Keyboard.W: move(Direction.TOP); + case Keyboard.D: move(Direction.RIGHT); + } + } + + private function onKeyUp(event:KeyboardEvent):Void { + stop(); + } +} diff --git a/src/client/haxe/ru/m/tankz/core/Tank.hx b/src/client/haxe/ru/m/tankz/core/Tank.hx index 927fa59..453b441 100755 --- a/src/client/haxe/ru/m/tankz/core/Tank.hx +++ b/src/client/haxe/ru/m/tankz/core/Tank.hx @@ -2,13 +2,26 @@ 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; } + + public function move(direction:Direction):Void { + 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/game/Tankz.hx b/src/client/haxe/ru/m/tankz/game/Tankz.hx index 2609350..b5f44b7 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.PlayerTank; import ru.m.tankz.map.TankzMap; import ru.m.tankz.core.Tank; import ru.m.tankz.core.ITank; @@ -23,7 +24,7 @@ class Tankz implements ITankz { public function init():Void { tanks = [ - new Tank(), + new PlayerTank(), new Tank() ]; x_limit = map.width * map.cellWidth;