added player tank
This commit is contained in:
17
src/client/haxe/ru/m/tankz/core/Direction.hx
Normal file
17
src/client/haxe/ru/m/tankz/core/Direction.hx
Normal file
@@ -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;
|
||||
}
|
||||
}
|
||||
28
src/client/haxe/ru/m/tankz/core/PlayerTank.hx
Normal file
28
src/client/haxe/ru/m/tankz/core/PlayerTank.hx
Normal file
@@ -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();
|
||||
}
|
||||
}
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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;
|
||||
|
||||
Reference in New Issue
Block a user