This commit is contained in:
2014-08-25 17:25:05 +04:00
parent 937ebd5945
commit deefba434e
5 changed files with 72 additions and 19 deletions

View File

@@ -1,7 +1,7 @@
<?xml version="1.0" encoding="utf-8"?> <?xml version="1.0" encoding="utf-8"?>
<project> <project>
<meta title="Armageddon" package="ru.m.tankz" version="0.0.0" company="m"/> <meta title="Tank'z" package="ru.m.tankz" version="0.0.0" company="m"/>
<app main="ru.m.tankz.Client" path="target" file="armageddon"/> <app main="ru.m.tankz.Client" path="target" file="tankz"/>
<source path="src/common/haxe"/> <source path="src/common/haxe"/>
<source path="src/client/haxe"/> <source path="src/client/haxe"/>
<source path="src-gen/haxe"/> <source path="src-gen/haxe"/>

View File

@@ -1,5 +1,5 @@
package ru.m.tankz.core; package ru.m.tankz.core;
interface ITank extends IMobileEntity { interface ITank extends IMobileEntity {
public function shot():Void;
} }

View File

@@ -1,28 +1,34 @@
package ru.m.tankz.core; package ru.m.tankz.core;
import flash.ui.Keyboard; import flash.geom.Point;
import ru.m.tankz.core.Tank.TankAction;
import flash.events.KeyboardEvent; import flash.events.KeyboardEvent;
import flash.Lib; import flash.Lib;
class PlayerTank extends Tank { class PlayerTank extends Tank {
public function new() { private var keyBinding:Map<Int, TankAction>;
super();
public function new(position:Point, keyBinding:Map<Int, TankAction>) {
super(position);
this.keyBinding = keyBinding;
Lib.current.stage.addEventListener(KeyboardEvent.KEY_DOWN, onKeyDown); Lib.current.stage.addEventListener(KeyboardEvent.KEY_DOWN, onKeyDown);
Lib.current.stage.addEventListener(KeyboardEvent.KEY_UP, onKeyUp); Lib.current.stage.addEventListener(KeyboardEvent.KEY_UP, onKeyUp);
} }
private function onKeyDown(event:KeyboardEvent):Void { private function onKeyDown(event:KeyboardEvent):Void {
switch (event.keyCode) { switch (keyBinding.get(event.keyCode)) {
case Keyboard.A: move(Direction.LEFT); case TankAction.MOVE_LEFT: move(Direction.LEFT);
case Keyboard.S: move(Direction.BOTTOM); case TankAction.MOVE_TOP: move(Direction.TOP);
case Keyboard.W: move(Direction.TOP); case TankAction.MOVE_RIGHT: move(Direction.RIGHT);
case Keyboard.D: move(Direction.RIGHT); case TankAction.MOVE_BOTTOM: move(Direction.BOTTOM);
case TankAction.SHOT: shot();
} }
} }
private function onKeyUp(event:KeyboardEvent):Void { private function onKeyUp(event:KeyboardEvent):Void {
stop(); if (keyBinding.exists(event.keyCode)) {
stop();
}
} }
} }

View File

@@ -1,13 +1,23 @@
package ru.m.tankz.core; package ru.m.tankz.core;
import flash.geom.Point;
enum TankAction {
MOVE_LEFT;
MOVE_TOP;
MOVE_RIGHT;
MOVE_BOTTOM;
SHOT;
}
class Tank extends MobileEntity implements ITank { class Tank extends MobileEntity implements ITank {
public function new() { public function new(position:Point) {
super(4); super(4);
x = 0; x = position.x;
y = 0; y = position.y;
width = 36; width = 34;
height = 36; height = 34;
} }
public function move(direction:Direction):Void { public function move(direction:Direction):Void {
@@ -20,4 +30,8 @@ class Tank extends MobileEntity implements ITank {
mx = 0; mx = 0;
my = 0; my = 0;
} }
public function shot():Void {
}
} }

View File

@@ -1,6 +1,10 @@
package ru.m.tankz.game; package ru.m.tankz.game;
import flash.geom.Point;
import flash.ui.Keyboard;
import flash.geom.Rectangle;
import ru.m.tankz.config.TankzConfig; import ru.m.tankz.config.TankzConfig;
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.Tank;
@@ -26,8 +30,18 @@ class Tankz implements ITankz {
this.config = config; this.config = config;
map = new TankzMap(config.map); map = new TankzMap(config.map);
tanks = [ tanks = [
new PlayerTank(), new PlayerTank(new Point(0, 0), [
new Tank() Keyboard.A => TankAction.MOVE_LEFT,
Keyboard.S => TankAction.MOVE_BOTTOM,
Keyboard.W => TankAction.MOVE_TOP,
Keyboard.D => TankAction.MOVE_RIGHT,
]),
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,
])
]; ];
x_limit = map.gridWidth * map.cellWidth; x_limit = map.gridWidth * map.cellWidth;
y_limit = map.gridHeight * map.cellHeight; y_limit = map.gridHeight * map.cellHeight;
@@ -43,6 +57,25 @@ class Tankz implements ITankz {
} }
tank.x += tank.mx; tank.x += tank.mx;
tank.y += tank.my; tank.y += tank.my;
var tankR = new Rectangle(tank.x, tank.y, tank.width, tank.height);
for (t in tanks) if (t != tank) {
var r = new Rectangle(t.x, t.y, t.width, t.height);
if (tankR.intersects(r)) {
if (tank.direction.x > 0) {
if (tank.x + tank.width > t.x) tank.x = t.x - tank.width;
} else if (tank.direction.x < 0) {
if (tank.x < t.x + t.width) tank.x = t.x + t.width;
}
if (tank.direction.y > 0) {
if (tank.y + tank.height > t.y) tank.y = t.y - tank.height;
} else if (tank.direction.y < 0) {
if (tank.y < t.y + t.height) tank.y = t.y + t.height;
}
}
}
if (tank.x < 0) tank.x = 0; if (tank.x < 0) tank.x = 0;
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;