private void CreatePlayer(int id) { if (id < 0 || id >= 8) { Debug.LogErrorFormat("GameManager CreatePlayer: Invalid id: {0} maximum id is 7", id); } int index = id % _spaceShipPrefabs.Count; GameObject prefab = _spaceShipPrefabs[index]; GameObject ship = Instantiate(prefab, _playerContainer) as GameObject; _playerShips[id] = new PlayerState(ship); //init game controller keys. See "Project Settings > Input" where the id mapping is GameConstants.PlayerKeys keys = GameConstants.getPlayerKeys(id); PlayerController ctrl = ship.GetComponent <PlayerController>(); ctrl.Init(id, keys, PlayerInformationManager.Instance.GetPlayerInformation(id)); ScoreManager.Instance.addPoints(id, 0); ship.GetComponent <WeaponLauncher>().setFireKeyCode(keys.FireBtn); //set initial position to unique place around the home planet Vector3 directionOnUnitCircle = GameConstants.getPlayerStartDirection(id); Vector3 startPos = _homePlanet.position + 1.2f * _homePlanet.localScale.magnitude * directionOnUnitCircle; ship.transform.position = startPos; ctrl.addTimeoutListener(this); }
public void Init(int id, GameConstants.PlayerKeys keys, PlayerInformation playerInformation) { this.Id = id; SetPlayerKeys(keys); this.PlayerInformation = playerInformation; GameObject go = Instantiate(playerInformation.PlayerShipPrefab, _shipContainer, false) as GameObject; _playerShip = go.GetComponent <SpaceShipController> (); _playerShip.Init(this); _playerShip.OnTriggerEnterAsObservable().Subscribe(collider => { OnPlayerShipTriggerEnter(collider); }); }
private void Update() { if (!_isGameActive) { return; } //TODO remove direct button listening from this class... abstract it somewhere and just get events, etc for (int id = 0; id < GameConstants.NUMBER_OF_PLAYERS; id++) { if (!_playerShips.ContainsKey(id)) { GameConstants.PlayerKeys keys = GameConstants.getPlayerKeys(id); bool pressed = Input.GetButtonDown(keys.SpawnBtn); if (pressed) { Debug.LogFormat("Spawnbutton {0} pressed for ship ID {1}", keys.SpawnBtn, id); CreatePlayer(id); } } } _secsUntilNextCollectable -= Time.deltaTime; if (_secsUntilNextCollectable <= 0.0f) { //These should also be cleaned up.. Add logic to CollectableBehaviour or here createCollectable(); InitCollectableTimeout(); } if (!_warningGiven) { float secsLeft = 30.0f; if (SessionManager.Instance.gameSessionLeft() < secsLeft) { AudioManager.Instance.Speak("You have thirty seconds"); _warningGiven = true; } } // Update the time view text _timeText.text = string.Format("{0} SEC", (int)(_gameEndTime - Time.time)); // Attempt to spawn orbit items at regular intervals UpdateOrbitItems(); }
private void SetPlayerKeys(GameConstants.PlayerKeys keys) { _keys = keys; }