From deefba434e0529322ace444dfd925ff4273945a3 Mon Sep 17 00:00:00 2001 From: shmyga Date: Mon, 25 Aug 2014 17:25:05 +0400 Subject: [PATCH] - --- project.xml | 4 +- src/client/haxe/ru/m/tankz/core/ITank.hx | 2 +- src/client/haxe/ru/m/tankz/core/PlayerTank.hx | 24 +++++++----- src/client/haxe/ru/m/tankz/core/Tank.hx | 24 +++++++++--- src/client/haxe/ru/m/tankz/game/Tankz.hx | 37 ++++++++++++++++++- 5 files changed, 72 insertions(+), 19 deletions(-) diff --git a/project.xml b/project.xml index 1c76360..e0afab1 100755 --- a/project.xml +++ b/project.xml @@ -1,7 +1,7 @@ - - + + diff --git a/src/client/haxe/ru/m/tankz/core/ITank.hx b/src/client/haxe/ru/m/tankz/core/ITank.hx index 568d424..286a129 100755 --- a/src/client/haxe/ru/m/tankz/core/ITank.hx +++ b/src/client/haxe/ru/m/tankz/core/ITank.hx @@ -1,5 +1,5 @@ package ru.m.tankz.core; interface ITank extends IMobileEntity { - + public function shot():Void; } diff --git a/src/client/haxe/ru/m/tankz/core/PlayerTank.hx b/src/client/haxe/ru/m/tankz/core/PlayerTank.hx index 1be177f..4d33177 100644 --- a/src/client/haxe/ru/m/tankz/core/PlayerTank.hx +++ b/src/client/haxe/ru/m/tankz/core/PlayerTank.hx @@ -1,28 +1,34 @@ 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.Lib; class PlayerTank extends Tank { - public function new() { - super(); + private var keyBinding:Map; + public function new(position:Point, keyBinding:Map) { + super(position); + this.keyBinding = keyBinding; 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); + switch (keyBinding.get(event.keyCode)) { + case TankAction.MOVE_LEFT: move(Direction.LEFT); + case TankAction.MOVE_TOP: move(Direction.TOP); + case TankAction.MOVE_RIGHT: move(Direction.RIGHT); + case TankAction.MOVE_BOTTOM: move(Direction.BOTTOM); + case TankAction.SHOT: shot(); } } private function onKeyUp(event:KeyboardEvent):Void { - stop(); + if (keyBinding.exists(event.keyCode)) { + stop(); + } } } diff --git a/src/client/haxe/ru/m/tankz/core/Tank.hx b/src/client/haxe/ru/m/tankz/core/Tank.hx index 95d0da2..8202cb0 100755 --- a/src/client/haxe/ru/m/tankz/core/Tank.hx +++ b/src/client/haxe/ru/m/tankz/core/Tank.hx @@ -1,13 +1,23 @@ 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 { - public function new() { + public function new(position:Point) { super(4); - x = 0; - y = 0; - width = 36; - height = 36; + x = position.x; + y = position.y; + width = 34; + height = 34; } public function move(direction:Direction):Void { @@ -20,4 +30,8 @@ class Tank extends MobileEntity implements ITank { mx = 0; my = 0; } + + public function shot():Void { + + } } diff --git a/src/client/haxe/ru/m/tankz/game/Tankz.hx b/src/client/haxe/ru/m/tankz/game/Tankz.hx index cad817c..6735c9a 100755 --- a/src/client/haxe/ru/m/tankz/game/Tankz.hx +++ b/src/client/haxe/ru/m/tankz/game/Tankz.hx @@ -1,6 +1,10 @@ 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.core.Tank; import ru.m.tankz.core.PlayerTank; import ru.m.tankz.map.TankzMap; import ru.m.tankz.core.Tank; @@ -26,8 +30,18 @@ class Tankz implements ITankz { this.config = config; map = new TankzMap(config.map); tanks = [ - new PlayerTank(), - new Tank() + new PlayerTank(new Point(0, 0), [ + 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; y_limit = map.gridHeight * map.cellHeight; @@ -43,6 +57,25 @@ class Tankz implements ITankz { } tank.x += tank.mx; 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 + tank.width > x_limit) tank.x = x_limit - tank.width; if (tank.y < 0) tank.y = 0;