/// <summary> /// Constructor. /// </summary> public GameplayScreen(GameLogicMode mode, string mapName) { this.mode = mode; this.mapName = mapName; TransitionOnTime = TimeSpan.FromSeconds(1.5); TransitionOffTime = TimeSpan.FromSeconds(0.5); nPlayers = 2; }
// Constructor public GameLogic() { mode = GameLogicMode.Undefined; ict = (IControlTank)TankAGame.ThisGame.Services.GetService(typeof(IControlTank)); map = (IMap)TankAGame.ThisGame.Services.GetService(typeof(IMap)); InitializeItemData(); nPlayers.Add(0, 1); nPlayers.Add(1, 0); }
void HostModeChooserSelected(object sender, PlayerIndexEventArgs e) { if (currentMode == GameLogicMode.HostQuake) { currentMode = GameLogicMode.HostCS; hostModeChooser.Text = "Host mode: Counter-strike"; } else { currentMode = GameLogicMode.HostQuake; hostModeChooser.Text = "Host mode: Quake"; } }
// Constructor. public NetStat() { mode = GameLogicMode.Undefined; }
public void SetMode(GameLogicMode mode) { this.mode = mode; }
// Private methods void ProcessMessage() { var ict = (IControlTank)TankAGame.ThisGame.Services.GetService(typeof(IControlTank)); var map = (IMap)TankAGame.ThisGame.Services.GetService(typeof(IMap)); var netStat = (INetStat)TankAGame.ThisGame.Services.GetService(typeof(INetStat)); MessageType type = (MessageType)readBuffer.ReadByte(); byte tankIndex; Point pos = new Point(); Direction direction; byte num; byte team; byte cIndex; byte itemIndex; int health; int freezeTime; List<byte> survivalIndexes = new List<byte>(); switch (type) { case MessageType.FireAck: if (!ready) break; tankIndex = readBuffer.ReadByte(); pos.X = readBuffer.ReadInt16(); pos.Y = readBuffer.ReadInt16(); ict.Fire(tankIndex, pos); break; case MessageType.StateUpdate: if (!ready) break; num = readBuffer.ReadByte(); for (int i = 0; i < num; ++i) { tankIndex = readBuffer.ReadByte(); pos.X = readBuffer.ReadInt16(); pos.Y = readBuffer.ReadInt16(); direction = (Direction)readBuffer.ReadByte(); ict.ChangeTankInMapState(tankIndex, new Vector2(pos.X, pos.Y), direction); survivalIndexes.Add(tankIndex); } ict.FilterSurvival(survivalIndexes); break; case MessageType.CreateTank: if (!ready) break; bool newCSRound = readBuffer.ReadBoolean(); if (newCSRound) { map.NewCSRound(); Point teamScore; teamScore.X = readBuffer.ReadInt16(); teamScore.Y = readBuffer.ReadInt16(); netStat.ChangeTeamStat(teamScore); } num = readBuffer.ReadByte(); for (int i = 0; i < num; ++i) { TankState state = UserTank.DefaultUserTankState; cIndex = readBuffer.ReadByte(); tankIndex = readBuffer.ReadByte(); pos.X = readBuffer.ReadInt16(); pos.Y = readBuffer.ReadInt16(); team = readBuffer.ReadByte(); state.tankIndex = tankIndex; state.position = new Vector2(pos.X, pos.Y); state.team = team; if (cIndex != clientIndex) state.playerIndex = 0xfe; Factory.CreateTank(state, ict.AllTanksInMap()); } break; case MessageType.TankFreezeAnn: if (!ready) break; tankIndex = readBuffer.ReadByte(); freezeTime = readBuffer.ReadInt16(); ict.AckTankFreeze(tankIndex, freezeTime); break; case MessageType.CreateItem: if (!ready) break; { ItemType itemType = (ItemType)readBuffer.ReadByte(); ItemState state = Item.DefaultItemState(itemType); state.itemIndex = readBuffer.ReadByte(); state.position.X = readBuffer.ReadInt16(); state.position.Y = readBuffer.ReadInt16(); // Add waiting time time to client's item to avoid // network latency. state.waitingTime += 200; Factory.CreateItem(state, map.AllItemsInMap()); } break; case MessageType.ActiveItem: if (!ready) break; itemIndex = readBuffer.ReadByte(); tankIndex = readBuffer.ReadByte(); map.AckItemActivation(itemIndex, tankIndex); break; case MessageType.HealthAnn: if (!ready) break; // Tank's health. num = readBuffer.ReadByte(); for (int i = 0; i < num; ++i) { tankIndex = readBuffer.ReadByte(); health = readBuffer.ReadByte(); ict.AckTankHealthChanged(tankIndex, health); } // BlockSprite's health. num = readBuffer.ReadByte(); for (int i = 0; i < num; ++i) { pos.X = readBuffer.ReadByte(); pos.Y = readBuffer.ReadByte(); health = readBuffer.ReadInt16(); map.AckBlockSpriteHealthChanged(pos, health); } break; case MessageType.StatAnn: if (!ready) break; AckStat(netStat, true); AckStat(netStat, false); break; case MessageType.ClientInfoAnn: clientIndex = readBuffer.ReadByte(); hostMode = (GameLogicMode)readBuffer.ReadByte(); string mapName = readBuffer.ReadString(); LoadingScreen.Load(TankAGame.ScreenManager, true, PlayerIndex.One, new GameplayScreen(GameLogicMode.Client, mapName)); TankAGame.ThisGame.Services.RemoveService(typeof(IClientMenu)); break; case MessageType.MapInfoAnn: // Tank info. num = readBuffer.ReadByte(); for (int i = 0; i < num; ++i) { tankIndex = readBuffer.ReadByte(); pos.X = readBuffer.ReadInt16(); pos.Y = readBuffer.ReadInt16(); team = readBuffer.ReadByte(); direction = (Direction)readBuffer.ReadByte(); Direction gunDirection = (Direction)readBuffer.ReadByte(); byte speed = readBuffer.ReadByte(); byte ammoSpeed = readBuffer.ReadByte(); byte armor = readBuffer.ReadByte(); int tbf = readBuffer.ReadInt16(); health = readBuffer.ReadByte(); int invulTime = readBuffer.ReadInt16(); AmmoType ammoType = (AmmoType)readBuffer.ReadByte(); TankState state = UserTank.DefaultUserTankState; state.tankIndex = tankIndex; state.position = new Vector2(pos.X, pos.Y); state.tankInfo.gunDirection = gunDirection; state.team = team; state.playerIndex = 0xfe; state.tankInfo.speed = speed; state.tankInfo.armor = armor; state.tankInfo.timeBetweenFires = tbf; state.tankInfo.health = health; state.tankInfo.invulnerableTime = invulTime; state.tankInfo.ammoInfo = Ammo.DefaultAmmoInfo(ammoType); state.tankInfo.ammoInfo.speed = ammoSpeed; Factory.CreateTank(state, ict.AllTanksInMap()); ict.ChangeTankInMapState(tankIndex, state.position, direction); } // Item info. num = readBuffer.ReadByte(); for (int i = 0; i < num; ++i) { ItemState state = new ItemState(); state.itemIndex = readBuffer.ReadByte(); state.type = (ItemType)readBuffer.ReadByte(); state.position.X = readBuffer.ReadInt16(); state.position.Y = readBuffer.ReadInt16(); state.waitingTime = readBuffer.ReadInt16(); state.activeTime = readBuffer.ReadInt16(); state.affectedTankIndex = readBuffer.ReadByte(); state.oldValue = readBuffer.ReadInt32(); Factory.CreateItem(state, map.AllItemsInMap()); } // Stat info. num = readBuffer.ReadByte(); for (int i = 0; i < num; ++i) { byte[] b = readBuffer.ReadBytes(4); IPAddress ip = new IPAddress(b); string name = readBuffer.ReadString(); team = readBuffer.ReadByte(); short kill = readBuffer.ReadInt16(); short dealth = readBuffer.ReadInt16(); netStat.NewClient(ip, name, team); netStat.ChangeStat(ip, kill, true); netStat.ChangeStat(ip, dealth, false); } RequestBlockSpriteInfo(); break; case MessageType.BlockSpriteInfoAnn: num = readBuffer.ReadByte(); var remainingBlockSprite = new List<Point>(); for (int i = 0; i < num; ++i) { health = readBuffer.ReadInt16(); int j = readBuffer.ReadInt16(); for (int k = 0; k < j; ++k) { pos.X = readBuffer.ReadByte(); pos.Y = readBuffer.ReadByte(); remainingBlockSprite.Add(pos); map.AckBlockSpriteHealthChanged(pos, health); } } map.FilterRemainingBlockSprite(remainingBlockSprite); ready = true; break; } }
public void BeginGame(GameLogicMode mode) { if (hasBegun) return; host = (ITankAHost)TankAGame.ThisGame.Services.GetService(typeof(ITankAHost)); nextItemTime = 3000; hasBegun = true; this.mode = mode; var state = UserTank.DefaultUserTankState; switch (mode) { case GameLogicMode.Single: for (int i = 0; i < 1; ++i) { state.team = (byte)i; state.playerIndex = (byte)i; state.position = ict.RandomTankPosition(); Factory.CreateTank(state, ict.AllTanksInMap()); } break; case GameLogicMode.HostQuake: NewNetworkGame(); break; case GameLogicMode.HostCS: NewNetworkGame(); break; } }
public TankAHost(GameLogicMode hostMode) { NetConfiguration netConfig = new NetConfiguration("TankA"); netConfig.MaxConnections = 10; netConfig.Port = NetworkManager.Port; host = new NetServer(netConfig); readBuffer = host.CreateBuffer(); currentFrame = framePerUpdate; this.hostMode = hostMode; }
public NetworkManager(Game game) : base(game) { // TODO: Construct any child components here role = GameLogicMode.Undefined; }
public void StopHost() { if (tankAHost != null) { tankAHost.Stop(); // Wait for host to properly send shutdown message. Thread.Sleep(100); tankAHost.Dispose(); tankAHost = null; } role = GameLogicMode.Undefined; TankAGame.ThisGame.Services.RemoveService(typeof(ITankAHost)); }
// INetwork interface implementation. public void StartHost(GameLogicMode hostMode) { tankAHost = new TankAHost(hostMode); tankAHost.Initialize(); tankAHost.Start(); role = hostMode; TankAGame.ThisGame.Services.AddService(typeof(ITankAHost), tankAHost); }
public void StartClient() { tankAClient = new TankAClient(); role = GameLogicMode.Client; TankAGame.ThisGame.Services.AddService(typeof(ITankAClient), tankAClient); }