private void UpdateRadarRange(PlayerGame playerGame, Game game, Unit newUnit) { //update tiles in radar range for user List<MapTile> radarRange = playerGame.Map.MapTiles.Where(c => c.Xpos >= newUnit.Position.Xpos - newUnit.RadarRange && c.Xpos <= newUnit.Position.Xpos + newUnit.RadarRange && c.Ypos >= newUnit.Position.Ypos - newUnit.RadarRange && c.Ypos <= newUnit.Position.Ypos + newUnit.RadarRange).ToList(); foreach (MapTile mt in radarRange) { MapTile motherTile = game.Map.MapTiles.Where(c => c.Xpos == mt.Xpos && c.Ypos == mt.Ypos).First(); mt.TerrainType = motherTile.TerrainType; mt.IsAccessible = motherTile.IsAccessible; mt.IsSpawningPoint = motherTile.IsSpawningPoint; mt.UserId = playerGame.UserId; PlayerMapTile pmt = this.flPlayerTiles.TypedItems<PlayerMapTile>().Where(c => c.Tile.Xpos == mt.Xpos && c.Tile.Ypos == mt.Ypos && c.UserId == playerGame.UserId).FirstOrDefault(); if (pmt == null) { PlayerMapTile playerMapTile = new PlayerMapTile(game.Id, playerGame.UserId, mt); playerMapTile.Save(this.FAppPrivate); } } }
private void btnAddGame_Click(object sender, EventArgs e) { var game = new Game() { Name = "Super Game", Rules = "There are no rules", MaxNumberOfPlayers = 2, AccessibleTerrainTypes = 2, InAccessibleTerrainTypes = 2, }; game.Map.LoadMap("Maps\\Map02.csv"); game.Map.Name = "Demo map"; game.Map.Description = "My first demo map created in google spreadsheets"; var gunSmallArms = new Weapon() { Class = 0, FiringRange = 2, Name = "small arms", RotatingSpeed = 90, ShieldDamage = 1, ArmorDamage = 2 }; var unitScout = new Unit() { Name = "Scout", Class = 0, Heading = enHeading.North, MovingSpeed = 3, MovingRange = 1, RotatingSpeed = 90, BuildTimeInSeconds = 10, MaxNumberOf = 1, Armor = 5, RadarRange = 1, Shields = 5, HasFixedGun = true, FixedGunArmorDamage = 1, FixedGunFiringRange = 1, FixedGunShieldDamage = 1, UnitTypeId = Guid.NewGuid().ToString() }; // unitScout.Weapons.Add(gunSmallArms); game.AvailableUnits.Add(unitScout); var gunTurret = new Weapon() { Class = 1, FiringRange = 2, Name = "heavy arms", RotatingSpeed = 45, ShieldDamage = 10, ArmorDamage = 20 }; var heavyUnit = new Unit() { Name = "Heavy", Class = 1, Heading = enHeading.NorthEast, MovingSpeed = 2, MovingRange = 1, RotatingSpeed = 45, BuildTimeInSeconds = 10, MaxNumberOf = 1, Armor = 100, RadarRange = 1, Shields = 200, HasFixedGun = true, FixedGunArmorDamage = 10, FixedGunFiringRange = 10, FixedGunShieldDamage = 10, UnitTypeId = Guid.NewGuid().ToString() }; // heavyUnit.Weapons.Add(gunTurret); game.AvailableUnits.Add(heavyUnit); game.Save(this.FAppPublic); }
private void MoveUnit(GameCommand cmdReceived, PlayerGame playerGame, Game game) { if (playerGame == null) { new CommandFeedback(cmdReceived, enCommandStatus.Rejected, "User did not join game").Save(this.FAppPrivate); } else { if (game == null) { new CommandFeedback(cmdReceived, enCommandStatus.Rejected, "Game not found").Save(this.FAppPrivate); } else { PlayerGame myPlayerGame = this.flPlayerGames.TypedItems<PlayerGame>().Where(c => c.UserId == cmdReceived.UserId).FirstOrDefault(); if (myPlayerGame == null) { new CommandFeedback(cmdReceived, enCommandStatus.Rejected, "Player not found in game").Save(this.FAppPrivate); } else { // Unit myUnit = myPlayerGame.Units.Where(c => c.Id == cmdReceived.ObjectId).FirstOrDefault(); PlayerUnit playerUnit = this.flPlayerUnits.TypedItems<PlayerUnit>().Where(c => c.Id == cmdReceived.ObjectId && c.Unit.UserId == playerGame.UserId).FirstOrDefault(); if (playerUnit == null) { new CommandFeedback(cmdReceived, enCommandStatus.Rejected, "Unit not found in game").Save(this.FAppPrivate); } else { Unit myUnit = playerUnit.Unit; //can the unit move? if (cmdReceived.Distance > myUnit.MovingRange) { new CommandFeedback(cmdReceived, enCommandStatus.Rejected, playerUnit.Unit.Name + " can not move that distance in one move").Save(this.FAppPrivate); } else { int targetYpos = -1; int targetXpos = -1; switch (playerUnit.Unit.Heading) { case enHeading.North: targetYpos = (int)(myUnit.Position.Ypos - cmdReceived.Distance); targetXpos = (int)(myUnit.Position.Xpos); break; case enHeading.NorthWest: targetYpos = (int)(myUnit.Position.Ypos - cmdReceived.Distance); targetXpos = (int)(myUnit.Position.Xpos - cmdReceived.Distance); break; case enHeading.South: targetYpos = (int)(myUnit.Position.Ypos + cmdReceived.Distance); targetXpos = (int)(myUnit.Position.Xpos); break; case enHeading.SouthWest: targetYpos = (int)(myUnit.Position.Ypos + cmdReceived.Distance); targetXpos = (int)(myUnit.Position.Xpos - cmdReceived.Distance); break; case enHeading.SouthEast: targetYpos = (int)(myUnit.Position.Ypos + cmdReceived.Distance); targetXpos = (int)(myUnit.Position.Xpos + cmdReceived.Distance); break; case enHeading.NorthEast: targetYpos = (int)(myUnit.Position.Ypos - cmdReceived.Distance); targetXpos = (int)(myUnit.Position.Xpos + cmdReceived.Distance); break; case enHeading.East: targetYpos = (int)(myUnit.Position.Ypos); targetXpos = (int)(myUnit.Position.Xpos + cmdReceived.Distance); break; case enHeading.West: targetYpos = (int)(myUnit.Position.Ypos); targetXpos = (int)(myUnit.Position.Xpos - cmdReceived.Distance); break; default: break; } if (targetXpos > -1 && targetYpos > -1) { moveUnit(cmdReceived, playerGame, game, playerUnit, targetYpos, targetXpos); } } } } } } }
private void moveUnit(GameCommand cmdReceived, PlayerGame playerGame, Game game, PlayerUnit myUnit, int targetYpos, int targetXpos) { MapTile targetMapTile = game.Map.MapTiles.Where(c => c.Xpos == targetXpos && c.Ypos == targetYpos).FirstOrDefault(); if (targetMapTile == null) { new CommandFeedback(cmdReceived, enCommandStatus.Rejected, "Map position not found (out of map bounds)").Save(this.FAppPrivate); } else { if (!targetMapTile.IsAccessible) { new CommandFeedback(cmdReceived, enCommandStatus.Rejected, "Map position type is inaccessible").Save(this.FAppPrivate); } else { PlayerUnit unitsOnPosition = this.flPlayerUnits.TypedItems<PlayerUnit>().Where(c => c.Unit.Position.Xpos == targetXpos && c.Unit.Position.Ypos == targetYpos).FirstOrDefault(); if (unitsOnPosition != null) { new CommandFeedback(cmdReceived, enCommandStatus.Rejected, "Map position is occupied").Save(this.FAppPrivate); } else { //yes can move there myUnit.Unit.Position.Ypos = targetYpos; myUnit.Unit.Position.Xpos = targetXpos; UpdateRadarRange(playerGame, game, myUnit.Unit); myUnit.Save(this.FAppPrivate); //List<PlayerMapTile> pmtOthers = this.flPlayerTiles.TypedItems<PlayerMapTile>().Where(c => c.Tile.Xpos == targetXpos && c.Tile.Ypos == targetYpos && c.UserId != playerGame.UserId && c.GameId == game.Id).ToList(); //foreach (PlayerMapTile pmt in pmtOthers) //{ // PlayerUnit pu = CnC.Helpers.DeepClone.CloneJson<PlayerUnit>(myUnit); // pu.Key = null; // pu.UserId = pmt.UserId; // pu.Unit.UserId = playerGame.UserId; // pu.Save(this.FAppPrivate); //} // PlayerUnit pu = this.flPlayerUnits.TypedItems<PlayerUnit>().Where(c => c.Unit.Id == myUnit.Unit.Id && c.UserId == pmt.UserId).FirstOrDefault(); // if(pu == null) // { // pu = CnC.Helpers.DeepClone.CloneJson<PlayerUnit>(myUnit); // pu.Unit.UserId = pmt.UserId; // pu.UserId = playerGame.UserId; // } // else // { // pu.Unit.Position.Xpos = targetXpos; // pu.Unit.Position.Ypos = targetYpos; // } // pu.Save(this.FAppPrivate); new CommandFeedback(cmdReceived, enCommandStatus.Accepted, myUnit.Unit.Name + " moved").Save(this.FAppPrivate); } } } }
private void AddUnit(GameCommand cmdReceived, PlayerGame playerGame, Game game) { Unit newUnitType = game.AvailableUnits.Where(c => c.Id == cmdReceived.ObjectId).FirstOrDefault(); if (newUnitType == null) { new CommandFeedback(cmdReceived, enCommandStatus.Rejected, newUnitType.Name + " not found in the game").Save(this.FAppPrivate); } else { if (playerGame == null) { new CommandFeedback(cmdReceived, enCommandStatus.Rejected, "PlayerGame not found").Save(this.FAppPrivate); } else { if (this.flPlayerUnits.TypedItems<PlayerUnit>().Where(c => c.UserId == cmdReceived.UserId && c.Unit.UnitTypeId == newUnitType.UnitTypeId && c.Unit.UserId == playerGame.UserId).Count() >= newUnitType.MaxNumberOf) { new CommandFeedback(cmdReceived, enCommandStatus.Rejected, "Already too many " + newUnitType.Name + "s in the game").Save(this.FAppPrivate); } else { // Unit unitsOnPosition = game.UnitsInGame.Where(c => c.Position.Xpos == playerGame.Spawn.Xpos && c.Position.Ypos == playerGame.Spawn.Ypos).FirstOrDefault(); PlayerUnit unitsOnPosition = this.flPlayerUnits.TypedItems<PlayerUnit>().Where(c => c.Unit.Position.Xpos == playerGame.Spawn.Xpos && c.Unit.Position.Ypos == playerGame.Spawn.Ypos).FirstOrDefault(); if (unitsOnPosition != null) { new CommandFeedback(cmdReceived, enCommandStatus.Rejected, "Could not spawn, spawn is occupied").Save(this.FAppPrivate); } else { Unit newUnit = DeepClone.CloneJson<Unit>(newUnitType); newUnit.Id = Guid.NewGuid().ToString(); newUnit.CreatedAt = DateTime.Now; newUnit.UpdatedAt = DateTime.Now; newUnit.Position.Xpos = playerGame.Spawn.Xpos; newUnit.Position.Ypos = playerGame.Spawn.Ypos; newUnit.UserId = cmdReceived.UserId; game.UnitsInGame.Add(newUnit); //TEST game.Save(this.FAppPublic); PlayerUnit newPlayerUnit = new PlayerUnit(game.Id, playerGame.UserId, newUnit); newPlayerUnit.Save(this.FAppPrivate); UpdateRadarRange(playerGame, game, newUnit); //update map for player //playerGame.Units.Add(newUnit); //playerGame.Save(this.FAppPrivate); //subscribe unit to game ticker this.gameUpdateList.Add(new GameSubscriber() { FireObject = playerGame, GameObject = newUnit, Game = game, Unit = newUnit }); new CommandFeedback(cmdReceived, enCommandStatus.Accepted, newUnitType.Name + " added to the game").Save(this.FAppPrivate); } } } } }
private void LeaveGame(GameCommand cmdReceived, PlayerGame playerGame, Game game) { if (playerGame == null) { new CommandFeedback(cmdReceived, enCommandStatus.Rejected, "User did not join this game").Save(this.FAppPrivate); } else { if (game == null) { new CommandFeedback(cmdReceived, enCommandStatus.Rejected, "Could not find this game").Save(this.FAppPrivate); } else { PlayerGame playerToRemove = game.PlayersInGame.Where(c => c.UserId == cmdReceived.UserId).FirstOrDefault(); if (playerToRemove == null) { new CommandFeedback(cmdReceived, enCommandStatus.Rejected, "Player not found in the game").Save(this.FAppPrivate); } else { game.PlayersInGame.Remove(playerToRemove); game.Save(this.FAppPublic); playerToRemove.Delete(this.FAppPrivate); new CommandFeedback(cmdReceived, enCommandStatus.Accepted, "Left the Game").Save(this.FAppPrivate); } } } }
private void JoinGame(GameCommand cmdReceived, Player player, PlayerGame playerGame, Game game) { if (playerGame != null) { new CommandFeedback(cmdReceived, enCommandStatus.Rejected, "User already joined game").Save(this.FAppPrivate); } else { if (game == null) { new CommandFeedback(cmdReceived, enCommandStatus.Rejected, "Game not found").Save(this.FAppPrivate); } else { if (game.PlayersInGame.Count >= game.MaxNumberOfPlayers) { new CommandFeedback(cmdReceived, enCommandStatus.Rejected, "Already too many players in the game").Save(this.FAppPrivate); } else { PlayerGame newPlayerGame = new PlayerGame(game, cmdReceived.UserId, player.NickName); foreach (MapTile tile in game.Map.MapTiles) { MapTile playerTile = new MapTile(); playerTile.TerrainType = -1;// tile.TerrainType; playerTile.UserId = newPlayerGame.UserId; playerTile.Xpos = tile.Xpos; playerTile.Ypos = tile.Ypos; newPlayerGame.Map.MapTiles.Add(playerTile); //set spawn, check if not occupied first if (newPlayerGame.Spawn == null && tile.IsSpawningPoint) { if (game.PlayersInGame.Where(c => c.Spawn.Xpos == tile.Xpos && c.Spawn.Ypos == tile.Ypos).Count() == 0) { //not used yet newPlayerGame.Spawn = playerTile; PlayerMapTile playerMapTile = new PlayerMapTile(game.Id, cmdReceived.UserId, playerTile); playerTile.TerrainType = 100; playerTile.IsSpawningPoint = true; playerTile.IsAccessible = true; playerMapTile.Save(this.FAppPrivate); } } } newPlayerGame.Save(this.FAppPrivate); game.PlayersInGame.Add(newPlayerGame); game.Save(this.FAppPublic); new CommandFeedback(cmdReceived, enCommandStatus.Accepted, "Joined the game").Save(this.FAppPrivate); } } } }
private void RotateUnit(GameCommand cmdReceived, PlayerGame playerGame, Game game) { PlayerUnit playerUnit = this.flPlayerUnits.TypedItems<PlayerUnit>().Where(c => c.Id == cmdReceived.ObjectId && c.Unit.UserId == playerGame.UserId).FirstOrDefault(); if (playerUnit == null) { new CommandFeedback(cmdReceived, enCommandStatus.Rejected, "PlayerUnit not found").Save(this.FAppPrivate); } else { Unit myUnit = playerUnit.Unit; if (myUnit == null) { new CommandFeedback(cmdReceived, enCommandStatus.Rejected, "Unit not found").Save(this.FAppPrivate); } else { switch (myUnit.Heading) { case enHeading.South: if (cmdReceived.Command == enCommand.RotateCW) { myUnit.Heading = enHeading.SouthEast; new CommandFeedback(cmdReceived, enCommandStatus.Accepted, "Heading South-East").Save(this.FAppPrivate); } else { myUnit.Heading = enHeading.SouthWest; new CommandFeedback(cmdReceived, enCommandStatus.Accepted, "Heading South-West").Save(this.FAppPrivate); } break; case enHeading.SouthWest: if (cmdReceived.Command == enCommand.RotateCW) { myUnit.Heading = enHeading.South; new CommandFeedback(cmdReceived, enCommandStatus.Accepted, "Heading South").Save(this.FAppPrivate); } else { myUnit.Heading = enHeading.West; new CommandFeedback(cmdReceived, enCommandStatus.Accepted, "Heading West").Save(this.FAppPrivate); } break; case enHeading.West: if (cmdReceived.Command == enCommand.RotateCW) { myUnit.Heading = enHeading.SouthWest; new CommandFeedback(cmdReceived, enCommandStatus.Accepted, "Heading South-West").Save(this.FAppPrivate); } else { myUnit.Heading = enHeading.NorthWest; new CommandFeedback(cmdReceived, enCommandStatus.Accepted, "Heading North-East").Save(this.FAppPrivate); } break; case enHeading.NorthWest: if (cmdReceived.Command == enCommand.RotateCW) { myUnit.Heading = enHeading.West; new CommandFeedback(cmdReceived, enCommandStatus.Accepted, "Heading West").Save(this.FAppPrivate); } else { myUnit.Heading = enHeading.North; new CommandFeedback(cmdReceived, enCommandStatus.Accepted, "Heading North").Save(this.FAppPrivate); } break; case enHeading.North: if (cmdReceived.Command == enCommand.RotateCW) { myUnit.Heading = enHeading.NorthWest; new CommandFeedback(cmdReceived, enCommandStatus.Accepted, "Heading North-West").Save(this.FAppPrivate); } else { myUnit.Heading = enHeading.NorthEast; new CommandFeedback(cmdReceived, enCommandStatus.Accepted, "Heading North-East").Save(this.FAppPrivate); } break; case enHeading.NorthEast: if (cmdReceived.Command == enCommand.RotateCW) { myUnit.Heading = enHeading.North; new CommandFeedback(cmdReceived, enCommandStatus.Accepted, "Heading North").Save(this.FAppPrivate); } else { myUnit.Heading = enHeading.East; new CommandFeedback(cmdReceived, enCommandStatus.Accepted, "Heading East").Save(this.FAppPrivate); } break; case enHeading.East: if (cmdReceived.Command == enCommand.RotateCW) { myUnit.Heading = enHeading.NorthEast; new CommandFeedback(cmdReceived, enCommandStatus.Accepted, "Heading North-East").Save(this.FAppPrivate); } else { myUnit.Heading = enHeading.SouthEast; new CommandFeedback(cmdReceived, enCommandStatus.Accepted, "Heading South-East").Save(this.FAppPrivate); } break; case enHeading.SouthEast: if (cmdReceived.Command == enCommand.RotateCW) { myUnit.Heading = enHeading.East; new CommandFeedback(cmdReceived, enCommandStatus.Accepted, "Heading East").Save(this.FAppPrivate); } else { myUnit.Heading = enHeading.South; new CommandFeedback(cmdReceived, enCommandStatus.Accepted, "Heading South").Save(this.FAppPrivate); } break; default: break; } playerUnit.Save(this.FAppPrivate); // playerGame.Save(this.FAppPrivate); // game.UnitsInGame.Where(c => c.Id == myUnit.Id).FirstOrDefault().Heading = myUnit.Heading; //Test game.Save(this.FAppPublic); } } }
private void FireFixedGun(GameCommand cmdReceived, PlayerGame playerGame, Game game) { PlayerUnit playerUnit = this.flPlayerUnits.TypedItems<PlayerUnit>().Where(c => c.Id == cmdReceived.ObjectId && c.Unit.UserId == playerGame.UserId).FirstOrDefault(); if (playerUnit == null) { new CommandFeedback(cmdReceived, enCommandStatus.Rejected, "PlayerUnit not found").Save(this.FAppPrivate); } else { Unit myUnit = playerUnit.Unit; if (myUnit == null) { new CommandFeedback(cmdReceived, enCommandStatus.Rejected, "Unit not found").Save(this.FAppPrivate); } else { if (!myUnit.HasFixedGun || myUnit.FixedGunFiringRange < cmdReceived.Distance) { new CommandFeedback(cmdReceived, enCommandStatus.Rejected, "Unit doesn't have a fixed gun or distance out of range").Save(this.FAppPrivate); } else { int targetYpos = -1; int targetXpos = -1; switch (myUnit.Heading) { case enHeading.North: targetYpos = (int)(myUnit.Position.Ypos - cmdReceived.Distance); targetXpos = (int)(myUnit.Position.Xpos); break; case enHeading.NorthWest: targetYpos = (int)(myUnit.Position.Ypos - cmdReceived.Distance); targetXpos = (int)(myUnit.Position.Xpos - cmdReceived.Distance); break; case enHeading.South: targetYpos = (int)(myUnit.Position.Ypos + cmdReceived.Distance); targetXpos = (int)(myUnit.Position.Xpos); break; case enHeading.SouthWest: targetYpos = (int)(myUnit.Position.Ypos + cmdReceived.Distance); targetXpos = (int)(myUnit.Position.Xpos - cmdReceived.Distance); break; case enHeading.SouthEast: targetYpos = (int)(myUnit.Position.Ypos + cmdReceived.Distance); targetXpos = (int)(myUnit.Position.Xpos + cmdReceived.Distance); break; case enHeading.NorthEast: targetYpos = (int)(myUnit.Position.Ypos - cmdReceived.Distance); targetXpos = (int)(myUnit.Position.Xpos + cmdReceived.Distance); break; case enHeading.East: targetYpos = (int)(myUnit.Position.Ypos); targetXpos = (int)(myUnit.Position.Xpos + cmdReceived.Distance); break; case enHeading.West: targetYpos = (int)(myUnit.Position.Ypos); targetXpos = (int)(myUnit.Position.Xpos - cmdReceived.Distance); break; default: break; } if (targetXpos > -1 && targetYpos > -1) { PlayerUnit pu = this.flPlayerUnits.TypedItems<PlayerUnit>().Where(c => c.Unit.Position.Xpos == targetXpos && c.Unit.Position.Ypos == targetYpos).FirstOrDefault(); if (pu == null) { new CommandFeedback(cmdReceived, enCommandStatus.Rejected, "Target missed").Save(this.FAppPrivate); } else { Unit attackedUnit = pu.Unit; attackedUnit.Armor -= myUnit.FixedGunArmorDamage; attackedUnit.Shields -= myUnit.FixedGunShieldDamage; pu.Save(this.FAppPrivate); if (attackedUnit.Armor < 1 || attackedUnit.Shields < 1) { attackedUnit.Status = enUnitStatus.Destroyed; new CommandFeedback(cmdReceived, enCommandStatus.Accepted, attackedUnit.Name + " destroyed!").Save(this.FAppPrivate); pu.Delete(this.FAppPrivate); } else { new CommandFeedback(cmdReceived, enCommandStatus.Accepted, attackedUnit.Name + " hit!, enemy shields: " + attackedUnit.Shields + ", enemy armor: " + attackedUnit.Armor).Save(this.FAppPrivate); } } } } } } }