public override bool Equals(object obj) { if (obj.GetType() == typeof(MuscleConnectionFile)) { MuscleConnectionFile file = (MuscleConnectionFile)obj; if (!file.GetEnd().table.Equals(muscleTable)) { return(false); } if (!file.GetEnd().type.Equals(type)) { return(false); } if (!file.GetStart()[0].Equals(startTable)) { return(false); } if (!file.GetStart()[1].Equals(startSeat)) { return(false); } return(true); } else { return(false); } }
public static void MuscleConnectionRemoved(MuscleConnection mus) { MuscleConnectionFile file; Controller start = mus.GetStart().GetComponent <Controller>(); Muscle end = mus.GetEnd().GetComponent <Muscle>(); MuscleType type; if (end is SwingMuscle) { //What type? SwingMuscle swing = end as SwingMuscle; switch (swing.moveDirection) { case SwingMuscle.SwingFunction.Backward: { type = MuscleType.Backward; break; } case SwingMuscle.SwingFunction.Forward: { type = MuscleType.Forward; break; } default: { //So we have something type = MuscleType.Forward; break; } } } else { //Stance muscle type = MuscleType.Stance; } file = new MuscleConnectionFile(end.table, type, start.GetTableNum(), start.GetSeatNum()); int endpoint = muscleConnectionsToReset.Count; //Avoid changing list while iterating. for (int i = endpoint - 1; i >= 0; i--) { if (muscleConnectionsToReset[i].Equals(mus.gameObject)) { muscleConnectionsToReset.Remove(mus.gameObject); } } endpoint = spawnedMuscleConnections.Count; for (int i = endpoint - 1; i >= 0; i--) { if (spawnedMuscleConnections[i].Equals(file)) { spawnedMuscleConnections.Remove(file); } } }
//Helper method to spawn muscle connections based on given connection file private static void SpawnConnection(MuscleConnectionFile file) { //Find start and end gameObjects GameObject start = null, end = null; foreach (NeuronDesignation nd in loadedPlayers) { //Find start if (nd.isGivenNeuron(file.GetStart()[0], file.GetStart()[1])) { start = nd.GetObject(); } } foreach (Muscle m in muscles) { //Find end MuscleType type; if (m is SwingMuscle) { //What type? SwingMuscle swing = m as SwingMuscle; switch (swing.moveDirection) { case SwingMuscle.SwingFunction.Backward: { type = MuscleType.Backward; break; } case SwingMuscle.SwingFunction.Forward: { type = MuscleType.Forward; break; } default: { //So we have something type = MuscleType.Forward; break; } } } else { //Stance muscle type = MuscleType.Stance; } if (m.table.Equals(file.GetEnd().table) && type.Equals(file.GetEnd().type)) { end = m.gameObject; } } if (start && end) { GameObject player = NetworkManager.singleton.client.connection.playerControllers[0].gameObject; player.GetComponent <ConnectionManager>().MuscleConnection(start, end); } }
public static void MuscleConnectionMade(MuscleConnection mus) { MuscleConnectionFile file; Controller start = mus.GetStart().GetComponent <Controller>(); Muscle end = mus.GetEnd().GetComponent <Muscle>(); MuscleType type; if (end is SwingMuscle) { //What type? SwingMuscle swing = end as SwingMuscle; switch (swing.moveDirection) { case SwingMuscle.SwingFunction.Backward: { type = MuscleType.Backward; break; } case SwingMuscle.SwingFunction.Forward: { type = MuscleType.Forward; break; } default: { //So we have something type = MuscleType.Forward; break; } } } else { //Stance muscle type = MuscleType.Stance; } file = new MuscleConnectionFile(end.table, type, start.GetTableNum(), start.GetSeatNum()); muscleConnectionsToReset.Add(mus.gameObject); if (!spawnedMuscleConnections.Contains(file)) { spawnedMuscleConnections.Add(file); } }
public static void LoadGame(string file) { StreamReader reader = new StreamReader(file); while (!reader.EndOfStream) { string line = reader.ReadLine(); //Check if neural connection, sensor connection, muscle connection, or neuron parameter string checkNeural = line.Substring(0, 5); string checkSensor = line.Substring(0, 6); string checkMuscle = line.Substring(0, 6); string checkParam = line.Substring(0, 6); if (checkNeural.Equals("Start")) { //Neural connection, move ahead ConnectionFile con; //Strip off the formatting to get to numbers line = line.Substring(9); //Start table number int startTable; if (!int.TryParse(line.Substring(0, 1), out startTable)) { Debug.LogError("Start Table format incorrect"); continue; } line = line.Substring(6); //Start seat number int startSeat; if (!int.TryParse(line.Substring(0, 1), out startSeat)) { Debug.LogError("Start Seat format incorrect"); continue; } line = line.Substring(10); //End table number int endTable; if (!int.TryParse(line.Substring(0, 1), out endTable)) { Debug.LogError("End Table format incorrect"); continue; } line = line.Substring(6); //End seat number int endSeat; if (!int.TryParse(line.Substring(0, 1), out endSeat)) { Debug.LogError("End Seat format incorrect"); continue; } line = line.Substring(6); //Strength float strength; if (!float.TryParse(line, out strength)) { Debug.LogError("Strength format incorrect"); continue; } con = new ConnectionFile(startTable, endTable, startSeat, endSeat, strength); connectionsToLoad.Add(con); } else if (checkSensor.Equals("Sensor")) { //Sensor connection, move ahead SensorConnectionFile sen; //Strip off the formatting to get to numbers line = line.Substring(10); //Sensor table number int sensorTable; if (!int.TryParse(line.Substring(0, 1), out sensorTable)) { Debug.LogError("Sensor Table format incorrect"); continue; } line = line.Substring(11); //Sensor location Sensor.SensorLocation location; switch (line.Substring(0, 1)) { case "P": { //Backward location = Sensor.SensorLocation.Backward; break; } case "A": { //Forward location = Sensor.SensorLocation.Forward; break; } default: { //Incorrect Debug.LogError("Sensor Location format incorrect"); continue; } } line = line.Substring(10); //End table number int endTable; if (!int.TryParse(line.Substring(0, 1), out endTable)) { Debug.LogError("End Table format incorrect"); continue; } line = line.Substring(6); //End seat number int endSeat; if (!int.TryParse(line.Substring(0, 1), out endSeat)) { Debug.LogError("End Seat format incorrect"); continue; } sen = new SensorConnectionFile(sensorTable, location, endTable, endSeat); sensorConnectionsToLoad.Add(sen); } else if (checkMuscle.Equals("Muscle")) { //Muscle connection, move ahead MuscleConnectionFile mus; //Strip off the formatting to get to numbers line = line.Substring(10); //Muscle table number int muscleTable; if (!int.TryParse(line.Substring(0, 1), out muscleTable)) { Debug.LogError("Muscle Table format incorrect"); continue; } line = line.Substring(7); //Muscle type MuscleType type; switch (line.Substring(0, 1)) { case "B": { //Backward type = MuscleType.Backward; break; } case "F": { //Forward type = MuscleType.Forward; break; } case "D": { //Stance type = MuscleType.Stance; break; } default: { //Incorrect Debug.LogError("Muscle Type format incorrect"); continue; } } line = line.Substring(12); //Start table number int startTable; if (!int.TryParse(line.Substring(0, 1), out startTable)) { Debug.LogError("Start Table format incorrect"); continue; } line = line.Substring(6); //Start seat number int startSeat; if (!int.TryParse(line.Substring(0, 1), out startSeat)) { Debug.LogError("Start Seat format incorrect"); continue; } mus = new MuscleConnectionFile(muscleTable, type, startTable, startSeat); muscleConnectionsToLoad.Add(mus); } else if (checkParam.Equals("Neuron")) { //Neuron parameters, move ahead NeuronFile neu; //Strip off the formatting to get to numbers line = line.Substring(10); //Table number int table; if (!int.TryParse(line.Substring(0, 1), out table)) { Debug.LogError("Table format incorrect"); continue; } line = line.Substring(6); //Seat number int seat; if (!int.TryParse(line.Substring(0, 1), out seat)) { Debug.LogError("Seat format incorrect"); continue; } line = line.Substring(7); //Resting threshold float rest; string number = Regex.Match(line, @"[0-9\.]+").Value; if (!float.TryParse(number, out rest)) { Debug.LogError("Resting Threshold format incorrect"); continue; } line = line.Substring(4 + number.Length); //Recovery threshold float rec; number = Regex.Match(line, @"[0-9\.]+").Value; if (!float.TryParse(number, out rec)) { Debug.LogError("Recovery Threshold format incorrect"); continue; } line = line.Substring(4 + number.Length); //Absolute refractory period float abs; number = Regex.Match(line, @"[0-9\.]+").Value; if (!float.TryParse(number, out abs)) { Debug.LogError("Absolute Refractory Period format incorrect"); continue; } line = line.Substring(4 + number.Length); //Relative refractory period float rel; if (!float.TryParse(line, out rel)) { Debug.LogError("Relative Refractory Period format incorrect"); continue; } neu = new NeuronFile(table, seat, rest, rec, abs, rel); neuronSettingsToLoad.Add(neu); } else if (line.Equals(string.Empty)) { //Empty file, don't bother reading return; } else { //Incorrect formatting, send error and stop Debug.LogError("Save file incorrect. Please fix before loading."); return; } } reader.Close(); Debug.Log("Game Loaded!"); //Reset all connections. Done once here so it isn't done several times in PlayerEntered. ResetConnections(); spawnedConnections = new List <ConnectionFile>(); //Must be reset on load so ghost connections don't pop back up. spawnedSensorConnections = new List <SensorConnectionFile>(); spawnedMuscleConnections = new List <MuscleConnectionFile>(); adjustedNeuronSettings = new List <NeuronFile>(); //Spawn connections for players already in scene. foreach (NeuronDesignation nd in loadedPlayers) { if (nd.GetObject() != null) { PlayerEntered(nd.GetObject(), nd.GetTableNum(), nd.GetSeatNum(), true); } } }