diff --git a/src/client/haxe/ru/m/tankz/view/frames/GameFrame.hx b/src/client/haxe/ru/m/tankz/view/frames/GameFrame.hx index ec5edf4..04be35f 100755 --- a/src/client/haxe/ru/m/tankz/view/frames/GameFrame.hx +++ b/src/client/haxe/ru/m/tankz/view/frames/GameFrame.hx @@ -8,11 +8,8 @@ import haxework.provider.Provider; import protohx.Message; import ru.m.connect.IConnection; import ru.m.tankz.config.ConfigBundle; -import ru.m.tankz.control.PlayerControl; -import ru.m.tankz.engine.Engine; import ru.m.tankz.game.ClassicGame; import ru.m.tankz.game.Game; -import ru.m.tankz.player.Player; import ru.m.tankz.proto.pack.GameUpdateResponse; @@ -23,7 +20,7 @@ class GameFrame extends VGroupView implements ViewBuilder implements IPacketHand public static inline var ID = "game"; - private var game:Game; + private var game:Game; private var timer:Timer; public function init():Void { @@ -31,9 +28,10 @@ class GameFrame extends VGroupView implements ViewBuilder implements IPacketHand public function onShow():Void { game = new ClassicGame(ConfigBundle.get(ClassicGame.TYPE, 0)); - game.start([ - new Player(1) - ]); + game.start({ + humans: 1, + bots: 3, + }); content.addEventListener(Event.ENTER_FRAME, redraw); Provider.get(IConnection).packetHandler.addListener(this); render.draw(game.engine); diff --git a/src/client/resources/config/config.yaml b/src/client/resources/config/config.yaml index 1800afe..ac21f52 100644 --- a/src/client/resources/config/config.yaml +++ b/src/client/resources/config/config.yaml @@ -16,17 +16,17 @@ map: y: 24 direction: top - type: bot - index: 1 + index: 0 x: 0 y: 0 direction: bottom - type: bot - index: 2 + index: 1 x: 12 y: 0 direction: bottom - type: bot - index: 3 + index: 2 x: 24 y: 0 direction: bottom diff --git a/src/common/haxe/ru/m/tankz/bot/Bot.hx b/src/common/haxe/ru/m/tankz/bot/Bot.hx index babcae1..8c24f36 100644 --- a/src/common/haxe/ru/m/tankz/bot/Bot.hx +++ b/src/common/haxe/ru/m/tankz/bot/Bot.hx @@ -1,5 +1,7 @@ package ru.m.tankz.bot; +import ru.m.tankz.game.Game; +import ru.m.tankz.game.Player; import ru.m.tankz.control.Control; import ru.m.geom.Direction; import haxe.Timer; @@ -8,13 +10,13 @@ import ru.m.tankz.core.Tank; import Type.ValueType; -class Bot extends Control { +class Bot extends Player { private var shotTimer:Timer; private var turnTimer:Timer; - public function new(tankId:Int) { - super(tankId); + public function new(team:TeamId, index:Int) { + super(team, index); } override public function onCollision(with:Dynamic):Void { diff --git a/src/common/haxe/ru/m/tankz/core/Bullet.hx b/src/common/haxe/ru/m/tankz/core/Bullet.hx index 186ea02..0d53235 100644 --- a/src/common/haxe/ru/m/tankz/core/Bullet.hx +++ b/src/common/haxe/ru/m/tankz/core/Bullet.hx @@ -1,20 +1,21 @@ package ru.m.tankz.core; +import ru.m.tankz.game.Game; import ru.m.tankz.config.Config; import ru.m.geom.Rectangle; import ru.m.geom.Direction; class Bullet extends MobileEntity { + public var team(default, null):TeamId; public var tankId(default, null):Int; - public var tankConfig(default, null):TankConfig; public var config(default, null):BulletConfig; - public function new(tankId:Int, tankConfig:TankConfig, config:BulletConfig) { + public function new(tank:Tank) { + this.team = tank.team; + this.config = tank.config.bullet; super(new Rectangle(0, 0, config.width, config.height), config.speed, Direction.RIGHT); - this.tankId = tankId; - this.tankConfig = tankConfig; - this.config = config; + this.tankId = tank.id; this.layer = 2; } } diff --git a/src/common/haxe/ru/m/tankz/core/Tank.hx b/src/common/haxe/ru/m/tankz/core/Tank.hx index f3cf48e..49bb3ba 100755 --- a/src/common/haxe/ru/m/tankz/core/Tank.hx +++ b/src/common/haxe/ru/m/tankz/core/Tank.hx @@ -1,5 +1,6 @@ package ru.m.tankz.core; +import ru.m.tankz.game.Game; import ru.m.geom.Point; import ru.m.tankz.config.Config; import ru.m.tankz.core.Bullet; @@ -8,14 +9,16 @@ import ru.m.geom.Direction; class Tank extends MobileEntity { + public var team(default, null):TeamId; public var index(default, null):Int; public var config(default, set):TankConfig; private var bulletsCounter:Int = 0; - public function new(index:Int, config: TankConfig) { + public function new(index:Int, team:TeamId, config:TankConfig) { super(new Rectangle(0, 0, config.width, config.height), config.speed, Direction.RIGHT); this.index = index; + this.team = team; this.config = config; this.layer = 1; } @@ -31,7 +34,7 @@ class Tank extends MobileEntity { public function shot():Null { if (bulletsCounter >= config.bullets) return null; - var bullet = new Bullet(id, config, config.bullet); + var bullet = new Bullet(this); bullet.rect.center = rect.center.add(new Point(rect.width / 4 * rect.direction.x, rect.height / 4 * rect.direction.y)); bullet.move(rect.direction); bulletsCounter++; diff --git a/src/common/haxe/ru/m/tankz/engine/Engine.hx b/src/common/haxe/ru/m/tankz/engine/Engine.hx index 1fac4f6..940d9f1 100755 --- a/src/common/haxe/ru/m/tankz/engine/Engine.hx +++ b/src/common/haxe/ru/m/tankz/engine/Engine.hx @@ -30,7 +30,7 @@ class CollisionProcessor implements EngineListener { public function onSpawn(entity:EntityType):Void {} private function checkTankBullet(tank:Tank, bullet:Bullet):Bool { - return tank.config.group != bullet.tankConfig.group; + return tank.team != bullet.team; } public function onCollision(entity:EntityType, with:EntityType):Void { diff --git a/src/common/haxe/ru/m/tankz/game/ClassicGame.hx b/src/common/haxe/ru/m/tankz/game/ClassicGame.hx index 67706d3..a2ab163 100644 --- a/src/common/haxe/ru/m/tankz/game/ClassicGame.hx +++ b/src/common/haxe/ru/m/tankz/game/ClassicGame.hx @@ -1,13 +1,56 @@ package ru.m.tankz.game; +import ru.m.tankz.bot.Bot; +import ru.m.tankz.game.Game; import ru.m.tankz.config.Config; -class ClassicGame extends Game { +typedef ClassicGameSettings = { > GameSettings, + var humans:Int; + var bots:Int; +} - public static var TYPE(default, never):String = Type.getClassName(ClassicGame); + +class ClassicGame extends Game { + + public static var TYPE(default, never):GameType = Type.getClassName(ClassicGame); public function new(config:Config) { super(TYPE, config); } + + override public function start(settings:ClassicGameSettings):Void { + super.start(settings); + var humans = new Team('player', settings.humans, 3); + for (index in 0...humans.size) { + var player = new Player(humans.id, index); + player.bind(this); + humans.players.push(player); + } + var bots = new Team('bot', settings.bots, 20); + for (index in 0...bots.size) { + var bot = new Bot(bots.id, 0); + bot.bind(this); + bots.players.push(bot); + } + teams = new Map(); + teams.set(humans.id, humans); + teams.set(bots.id, bots); + + for (player in humans.players) { + var point:SpawnPoint = config.getSpawnPoint(humans.type, 0); + var tank = buildTank(0, humans.id, config.getTank(humans.type, 0), point); + engine.spawn(tank); + player.tankId = tank.id; + } + + for (player in bots.players) { + var index = bots.players.indexOf(player); + var point:SpawnPoint = config.getSpawnPoint(bots.type, index); + var tank = buildTank(0, bots.id, config.getTank(bots.type, 0), point); + engine.spawn(tank); + player.tankId = tank.id; + cast(player, Bot).start(); + } + } } diff --git a/src/common/haxe/ru/m/tankz/game/Game.hx b/src/common/haxe/ru/m/tankz/game/Game.hx index d7e2358..2dbf112 100644 --- a/src/common/haxe/ru/m/tankz/game/Game.hx +++ b/src/common/haxe/ru/m/tankz/game/Game.hx @@ -7,44 +7,38 @@ import ru.m.tankz.config.Config; import ru.m.tankz.control.Control; import ru.m.tankz.core.Tank; import ru.m.tankz.engine.Engine; -import ru.m.tankz.player.Player; + +typedef GameType = String; +typedef TeamId = Int; -class Game implements EngineListener implements ControlListener { +typedef GameSettings = {} - public var type(default, null):String; + +class Game implements EngineListener implements ControlListener { + + public var type(default, null):GameType; + public var teams(default, null):Map; public var config(default, null):Config; public var engine(default, null):Engine; + public var settings(default, null):G; - public function new(type:String, config:Config) { + public function new(type:GameType, config:Config) { this.type = type; this.config = config; this.engine = new Engine(config); } - private function buildTank(index:Int, config:TankConfig, point:SpawnPoint):Tank { - var tank = new Tank(index, config); + private function buildTank(index:Int, teamId:TeamId, config:TankConfig, point:SpawnPoint):Tank { + var tank = new Tank(index, teamId, config); tank.rect.center = new Point((point.x + 1) * engine.map.cellWidth, (point.y + 1) * engine.map.cellHeight); tank.rect.direction = Direction.fromString(point.direction); return tank; } - public function start(players:Array):Void { - for (index in 0...players.length) { - var player:Player = players[index]; - var point:SpawnPoint = config.getSpawnPoint('player', index); - var tank = buildTank(index, config.getTank('player', 0), point); - engine.spawn(tank); - } - for (index in 1...4) { - var point:SpawnPoint = config.getSpawnPoint('bot', index); - var tank = buildTank(0, config.getTank('bot', 0), point); - engine.spawn(tank); - /*var bot = new Bot(tank.id); - bindControl(bot); - bot.start();*/ - } + public function start(settings:G):Void { + this.settings = settings; } public function onSpawn(entity:EntityType):Void { diff --git a/src/common/haxe/ru/m/tankz/game/Player.hx b/src/common/haxe/ru/m/tankz/game/Player.hx new file mode 100644 index 0000000..81bd953 --- /dev/null +++ b/src/common/haxe/ru/m/tankz/game/Player.hx @@ -0,0 +1,17 @@ +package ru.m.tankz.game; + +import ru.m.tankz.game.Game; +import ru.m.tankz.control.Control; + + +class Player extends Control { + + public var index(default, null):Int; + public var team(default, null):TeamId; + + public function new(team:TeamId, index:Int) { + super(); + this.team = team; + this.index = index; + } +} diff --git a/src/common/haxe/ru/m/tankz/game/Team.hx b/src/common/haxe/ru/m/tankz/game/Team.hx new file mode 100644 index 0000000..21aca40 --- /dev/null +++ b/src/common/haxe/ru/m/tankz/game/Team.hx @@ -0,0 +1,24 @@ +package ru.m.tankz.game; + +import ru.m.tankz.game.Player; +import ru.m.tankz.game.Game; + + +class Team { + + public var id(default, null):TeamId; + public var type(default, null):String; + public var size(default, null):Int; + public var life(default, default):Int; + public var players(default, null):Array; + + private static var i:Int = 0; + + public function new(type:String, size:Int, life:Int) { + this.id = ++i; + this.type = type; + this.size = size; + this.life = life; + this.players = []; + } +} diff --git a/src/common/haxe/ru/m/tankz/player/Player.hx b/src/common/haxe/ru/m/tankz/player/Player.hx deleted file mode 100644 index 1bc6843..0000000 --- a/src/common/haxe/ru/m/tankz/player/Player.hx +++ /dev/null @@ -1,14 +0,0 @@ -package ru.m.tankz.player; - -import ru.m.tankz.control.Control; - - -class Player extends Control { - - public var index(default, null):Int; - - public function new(index:Int) { - super(); - this.index = index; - } -}