[common] added teams

This commit is contained in:
2018-01-21 21:54:32 +03:00
parent a4558aa2f2
commit 223ff5ba93
11 changed files with 126 additions and 58 deletions

View File

@@ -8,11 +8,8 @@ import haxework.provider.Provider;
import protohx.Message; import protohx.Message;
import ru.m.connect.IConnection; import ru.m.connect.IConnection;
import ru.m.tankz.config.ConfigBundle; 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.ClassicGame;
import ru.m.tankz.game.Game; import ru.m.tankz.game.Game;
import ru.m.tankz.player.Player;
import ru.m.tankz.proto.pack.GameUpdateResponse; 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"; public static inline var ID = "game";
private var game:Game; private var game:Game<Dynamic>;
private var timer:Timer; private var timer:Timer;
public function init():Void { public function init():Void {
@@ -31,9 +28,10 @@ class GameFrame extends VGroupView implements ViewBuilder implements IPacketHand
public function onShow():Void { public function onShow():Void {
game = new ClassicGame(ConfigBundle.get(ClassicGame.TYPE, 0)); game = new ClassicGame(ConfigBundle.get(ClassicGame.TYPE, 0));
game.start([ game.start({
new Player(1) humans: 1,
]); bots: 3,
});
content.addEventListener(Event.ENTER_FRAME, redraw); content.addEventListener(Event.ENTER_FRAME, redraw);
Provider.get(IConnection).packetHandler.addListener(this); Provider.get(IConnection).packetHandler.addListener(this);
render.draw(game.engine); render.draw(game.engine);

View File

@@ -16,17 +16,17 @@ map:
y: 24 y: 24
direction: top direction: top
- type: bot - type: bot
index: 1 index: 0
x: 0 x: 0
y: 0 y: 0
direction: bottom direction: bottom
- type: bot - type: bot
index: 2 index: 1
x: 12 x: 12
y: 0 y: 0
direction: bottom direction: bottom
- type: bot - type: bot
index: 3 index: 2
x: 24 x: 24
y: 0 y: 0
direction: bottom direction: bottom

View File

@@ -1,5 +1,7 @@
package ru.m.tankz.bot; 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.tankz.control.Control;
import ru.m.geom.Direction; import ru.m.geom.Direction;
import haxe.Timer; import haxe.Timer;
@@ -8,13 +10,13 @@ import ru.m.tankz.core.Tank;
import Type.ValueType; import Type.ValueType;
class Bot extends Control { class Bot extends Player {
private var shotTimer:Timer; private var shotTimer:Timer;
private var turnTimer:Timer; private var turnTimer:Timer;
public function new(tankId:Int) { public function new(team:TeamId, index:Int) {
super(tankId); super(team, index);
} }
override public function onCollision(with:Dynamic):Void { override public function onCollision(with:Dynamic):Void {

View File

@@ -1,20 +1,21 @@
package ru.m.tankz.core; package ru.m.tankz.core;
import ru.m.tankz.game.Game;
import ru.m.tankz.config.Config; import ru.m.tankz.config.Config;
import ru.m.geom.Rectangle; import ru.m.geom.Rectangle;
import ru.m.geom.Direction; import ru.m.geom.Direction;
class Bullet extends MobileEntity { class Bullet extends MobileEntity {
public var team(default, null):TeamId;
public var tankId(default, null):Int; public var tankId(default, null):Int;
public var tankConfig(default, null):TankConfig;
public var config(default, null):BulletConfig; 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); super(new Rectangle(0, 0, config.width, config.height), config.speed, Direction.RIGHT);
this.tankId = tankId; this.tankId = tank.id;
this.tankConfig = tankConfig;
this.config = config;
this.layer = 2; this.layer = 2;
} }
} }

View File

@@ -1,5 +1,6 @@
package ru.m.tankz.core; package ru.m.tankz.core;
import ru.m.tankz.game.Game;
import ru.m.geom.Point; import ru.m.geom.Point;
import ru.m.tankz.config.Config; import ru.m.tankz.config.Config;
import ru.m.tankz.core.Bullet; import ru.m.tankz.core.Bullet;
@@ -8,14 +9,16 @@ import ru.m.geom.Direction;
class Tank extends MobileEntity { class Tank extends MobileEntity {
public var team(default, null):TeamId;
public var index(default, null):Int; public var index(default, null):Int;
public var config(default, set):TankConfig; public var config(default, set):TankConfig;
private var bulletsCounter:Int = 0; 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); super(new Rectangle(0, 0, config.width, config.height), config.speed, Direction.RIGHT);
this.index = index; this.index = index;
this.team = team;
this.config = config; this.config = config;
this.layer = 1; this.layer = 1;
} }
@@ -31,7 +34,7 @@ class Tank extends MobileEntity {
public function shot():Null<Bullet> { public function shot():Null<Bullet> {
if (bulletsCounter >= config.bullets) return 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.rect.center = rect.center.add(new Point(rect.width / 4 * rect.direction.x, rect.height / 4 * rect.direction.y));
bullet.move(rect.direction); bullet.move(rect.direction);
bulletsCounter++; bulletsCounter++;

View File

@@ -30,7 +30,7 @@ class CollisionProcessor implements EngineListener {
public function onSpawn(entity:EntityType):Void {} public function onSpawn(entity:EntityType):Void {}
private function checkTankBullet(tank:Tank, bullet:Bullet):Bool { 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 { public function onCollision(entity:EntityType, with:EntityType):Void {

View File

@@ -1,13 +1,56 @@
package ru.m.tankz.game; package ru.m.tankz.game;
import ru.m.tankz.bot.Bot;
import ru.m.tankz.game.Game;
import ru.m.tankz.config.Config; 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<ClassicGameSettings> {
public static var TYPE(default, never):GameType = Type.getClassName(ClassicGame);
public function new(config:Config) { public function new(config:Config) {
super(TYPE, 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<TeamId, Team>();
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();
}
}
} }

View File

@@ -7,44 +7,38 @@ import ru.m.tankz.config.Config;
import ru.m.tankz.control.Control; import ru.m.tankz.control.Control;
import ru.m.tankz.core.Tank; import ru.m.tankz.core.Tank;
import ru.m.tankz.engine.Engine; 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<G:GameSettings> implements EngineListener implements ControlListener {
public var type(default, null):GameType;
public var teams(default, null):Map<TeamId, Team>;
public var config(default, null):Config; public var config(default, null):Config;
public var engine(default, null):Engine; 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.type = type;
this.config = config; this.config = config;
this.engine = new Engine(config); this.engine = new Engine(config);
} }
private function buildTank(index:Int, config:TankConfig, point:SpawnPoint):Tank { private function buildTank(index:Int, teamId:TeamId, config:TankConfig, point:SpawnPoint):Tank {
var tank = new Tank(index, config); 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.center = new Point((point.x + 1) * engine.map.cellWidth, (point.y + 1) * engine.map.cellHeight);
tank.rect.direction = Direction.fromString(point.direction); tank.rect.direction = Direction.fromString(point.direction);
return tank; return tank;
} }
public function start(players:Array<Player>):Void { public function start(settings:G):Void {
for (index in 0...players.length) { this.settings = settings;
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 onSpawn(entity:EntityType):Void { public function onSpawn(entity:EntityType):Void {

View File

@@ -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;
}
}

View File

@@ -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<Player>;
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 = [];
}
}

View File

@@ -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;
}
}