public GameSimulation() { activePlayers = new List <Player>(); teamNameToModelMap = new Dictionary <string, int>(); teams = new Dictionary <string, List <TankController> >(); tankFactory = new TankFactory(); enqueuedCommands = new Queue <GameCommand>(); tankControllers = new List <TankController>(); allObjects = new List <GameObjectState>(); objectsInFieldOfView = new Dictionary <string, List <GameObjectState> >(); tanksToBeRemoved = new List <TankController>(); healthPickups = new List <GameObject>(); ammoPickups = new List <GameObject>(); healthPickupsToRemove = new List <GameObject>(); ammoPickupsToRemove = new List <GameObject>(); ammoPickupCount = ConfigValueStore.GetIntValue("ammo_packs_active"); healthPickupCount = ConfigValueStore.GetIntValue("health_packs_active"); //event setup EventManager.Initialise(); EventManager.clientDisconnect.AddListener(x => { Debug.Log("Client disconnected, removing tank"); lock (tankDisconnects) { tankDisconnects.Add(x.Token); } }); }
internal void RecordFrag(TankController victim, TankController killer) { EventManager.destroyedEvent.Invoke(victim); victim.Deaths++; //don't reward points for same-team kills. if (ConfigValueStore.GetBoolValue("team_mode")) { if (OnSameTeam(victim.Name, killer.Name)) { return; } } if (snitch != null) { if (snitch.GetComponent <SnitchBehaviour>().collector == victim) { killer.RewardSnitchPoints(); } } killer.AddKillPoints(); EventManager.killEvent.Invoke(killer); }
internal GameObject CreatePlayer(PlayerCreate create) { //get a random point in the arena Vector3 potentialStartPoint = RandomArenaPosition(); //already exists. Ignore. if (FindTankObject(create.Token) != null) { return(null); } GameObject t = null; //team game. if (ConfigValueStore.GetBoolValue("team_mode")) { if (create.Name.Contains(":")) { //get the team name. string teamName = GetTeamName(create.Name); //add the team to the list of teams if it isn't in there already. //Assign the team a tank type if this is the first time we've seen the team. if (!teams.Keys.Contains(teamName)) { teams.Add(teamName, new List <TankController>()); teamNameToModelMap.Add(teamName, currentModel); currentModel += 2; } t = tankFactory.CreateTank(create.Color, create.Name, create.Token, potentialStartPoint, teamNameToModelMap[teamName]); teams[teamName].Add(t.GetComponent <TankController>()); } else { //don't create player, this is a team game and they've not conformed to the naming needs (i.e. teamname:playername). } } else { t = tankFactory.CreateTank(create.Color, create.Name, create.Token, potentialStartPoint, -1); } //randomly rotate the tank t.GetComponent <TankController>().transform.Rotate(Vector3.up, UnityEngine.Random.Range(0, 360)); t.GetComponent <TankController>().Sim = this; tankControllers.Add(t.GetComponent <TankController>()); return(t); }
internal void RewardSnitchPoints() { if (ConfigValueStore.GetBoolValue("kill_capture_mode")) { for (int i = 0; i < snitchKillPoints; i++) { AddKillPoints(); } } else { Points += snitchKillPoints; } }
public TCPServer(GameSimulation simulation) { ipAddress = ConfigValueStore.GetValue("ipaddress"); port = Int32.Parse(ConfigValueStore.GetValue("port")); sim = simulation; messages = new Queue <NetworkMessage>(); networkThread = new Thread(new ThreadStart(StartServer)); connectedClients = new List <TcpClient>(); networkThread.Start(); SetupEvents(); }
public void AddKillPoints() { if (ConfigValueStore.GetBoolValue("kill_capture_mode")) { for (int i = 0; i < killPoints; i++) { UnbankedPoints++; var ob = GameObject.Instantiate(Resources.Load("Prefabs/UnbankedPoint") as GameObject); pointObjects.Add(ob); } } else { Points += killPoints; } }
// Use this for initialization public virtual void Start() { root = this.gameObject; turret = root.transform.Find("top").gameObject; barrel = turret.transform.Find("barrel").gameObject; firingPoint = barrel.transform.Find("firingpoint").gameObject; uiLabel = Instantiate(Resources.Load("Prefabs/TextLabel")) as UnityEngine.GameObject; uiLabel.GetComponent <TextMeshPro>().text = Name; smokeParticleSystem = root.transform.Find("main").gameObject.GetComponent <ParticleSystem>(); var em = smokeParticleSystem.emission; var sources = GetComponents <AudioSource>(); fireSound = sources[0]; shellHit = sources[1]; tankExplosion = sources[2]; em.enabled = false; pointObjects = new List <GameObject>(); turnRate = ConfigValueStore.GetFloatValue("turn_speed"); barrelRotateSpeed = ConfigValueStore.GetFloatValue("turret_rotation_speed"); fireInterval = ConfigValueStore.GetIntValue("fire_interval"); projectileForce = ConfigValueStore.GetFloatValue("projectile_force"); speed = ConfigValueStore.GetFloatValue("movement_speed"); turnRate = ConfigValueStore.GetFloatValue("turn_speed"); startingAmmo = ConfigValueStore.GetIntValue("starting_ammo"); startingHealth = ConfigValueStore.GetIntValue("starting_health"); snitchGoalPoints = ConfigValueStore.GetIntValue("snitch_goal_points"); snitchKillPoints = ConfigValueStore.GetIntValue("snitch_kill_points"); killPoints = ConfigValueStore.GetIntValue("kill_points"); explosivo1 = Instantiate(Resources.Load("Prefabs/Explosion")) as UnityEngine.GameObject; explosivo1.SetActive(false); explosivo2 = Instantiate(Resources.Load("Prefabs/TankExplosion")) as UnityEngine.GameObject; explosivo2.SetActive(false); Health = startingHealth; Ammo = startingAmmo; }
// Use this for initialization void Start() { simulation = new GameSimulation(); server = new TCPServer(simulation); cam = GameObject.Find("CameraRig").GetComponent <StadiumCam>(); scoreBoard = GameObject.Find("Scoreboard").GetComponent <Text>(); timer = GameObject.Find("Timer").GetComponent <Text>(); gameDuration = new TimeSpan(0, 0, Int32.Parse(ConfigValueStore.GetValue("game_time"))); timer.text = string.Format("{0:hh\\:mm\\:ss}", gameDuration); scoreRefreshTime = DateTime.Now; }
private void RefreshScores() { var scores = simulation.GetScores(); StringBuilder sb = new StringBuilder(); sb.Append("LEADERBOARD"); sb.AppendLine(); sb.AppendLine(); if (!ConfigValueStore.GetBoolValue("team_mode")) { foreach (TankController t in scores) { sb.Append(t.Name + " - " + t.Points); sb.AppendLine(); } } else { foreach (string team in simulation.teams.Keys) { int teamTotal = 0; foreach (TankController t in scores) { if (GameSimulation.GetTeamName(t.Name) == team) { teamTotal += t.Points; } } sb.Append(team + " - " + teamTotal); sb.AppendLine(); } } scoreBoard.text = sb.ToString(); scoreRefreshTime = DateTime.Now; }
public void Update() { allObjects.Clear(); HandlePickupLogic(); foreach (TankController t in tanksToBeRemoved) { RemoveTank(t); } tanksToBeRemoved.Clear(); while (enqueuedCommands.Count > 0) { GameCommand command = enqueuedCommands.Dequeue(); HandleCommand(command); } lock (allObjects) { UpdateTankState(); } lock (objectsInFieldOfView) { var tanks = UnityEngine.GameObject.FindObjectsOfType <TankController>(); foreach (TankController t in tanks) { UpdateTankViewObjects(t); } } if (TrainingRoomMain.currentGameState == TrainingRoomMain.GameState.playing) { if (healthPickups.Count < healthPickupCount) { SpawnHealthPickup(); } if (ammoPickups.Count < ammoPickupCount) { SpawnAmmoPickup(); } if (TrainingRoomMain.timeLeft.TotalSeconds < ConfigValueStore.GetIntValue("snitch_spawn_threshold")) { if (!snitchSpawned && ConfigValueStore.GetBoolValue("snitch_enabled")) { SpawnSnitch(); } } } lock (tankDisconnects) { foreach (string s in tankDisconnects) { RemoveTank(FindTankObject(s)); } tankDisconnects.Clear(); } }
// Update is called once per frame public virtual void Update() { if (currentState == TankState.normal) { if (toggleForward) { Forward(); } if (toggleReverse) { Reverse(); } if (toggleLeft) { TurnLeft(); } if (toggleRight) { TurnRight(); } if (toggleTurretLeft) { TurretLeft(); } if (toggleTurretRight) { TurretRight(); } if (autoTurn) { if (IsTurnLeft(Heading, desiredHeading)) { TurnLeft(); } else { TurnRight(); } float diff = Heading - desiredHeading; if (Math.Abs(diff) < 1) { autoTurn = false; } } if (autoTurretTurn) { if (IsTurnLeft(TurretHeading, desiredTurretHeading)) { TurretLeft(); } else { TurretRight(); } float diff = TurretHeading - desiredTurretHeading; if (Math.Abs(diff) < 2) { autoTurretTurn = false; } } if (autoMove) { if (desiredDistance > 0) { Forward(); float distanceTravelled = (oldPosition - transform.position).magnitude; if (desiredDistance > distanceTravelled) { desiredDistance -= distanceTravelled; } else { autoMove = false; } } else { Reverse(); float distanceTravelled = (oldPosition - transform.position).magnitude; if (Math.Abs(desiredDistance) > distanceTravelled) { desiredDistance += distanceTravelled; } else { autoMove = false; } } } Quaternion q = Quaternion.FromToRotation(transform.up, Vector3.up) * transform.rotation; transform.rotation = Quaternion.Slerp(transform.rotation, q, Time.deltaTime * reorientateSpeed); oldPosition = transform.position; } else if (currentState == TankState.destroyed) { TimeSpan sinceDestruction = DateTime.Now - destroyTime; if (sinceDestruction.TotalSeconds > ConfigValueStore.GetIntValue("respawn_time")) { Sim.RespawnTank(this); } } if (Camera.current != null) { var pos = new Vector3(transform.position.x, 10, transform.position.z); Quaternion labelRotation = Quaternion.LookRotation(pos - Camera.current.transform.position, Camera.current.transform.up); uiLabel.transform.SetPositionAndRotation(pos, labelRotation); } X = transform.position.x; Y = transform.position.z; ForwardX = transform.forward.x; ForwardY = transform.forward.z; //A = atan2(V.y, V.x) Heading = (float)Math.Atan2(transform.forward.z, transform.forward.x); Heading = Heading * Mathf.Rad2Deg; Heading = (Heading - 360) % 360; Heading = Math.Abs(Heading); TurretHeading = (float)Math.Atan2(-turret.transform.up.z, -turret.transform.up.x); TurretHeading = TurretHeading * Mathf.Rad2Deg; TurretHeading = (TurretHeading - 360) % 360; TurretHeading = Math.Abs(TurretHeading); TurretForwardX = turret.transform.up.x; TurretForwardY = turret.transform.up.z; if (transform.position.y < -10) { DestroyTank(); } if (infiniteAmmo) { ReplenishAmmo(); } if (infiniteHealth) { ReplenishHealth(); } int i = 0; foreach (GameObject o in pointObjects) { o.transform.position = transform.position + new Vector3(0, 5 + (i * 0.5f), 0); i++; } }