[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

@@ -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 {

View File

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

View File

@@ -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<Bullet> {
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++;

View File

@@ -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 {

View File

@@ -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<ClassicGameSettings> {
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<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.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<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 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<Player>):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 {

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