// Update is called once per frame void Update() { _moving = true; _running = false; _direction = MovementDirection.None; if (_state == State.Respawning) { _spawnTimer -= Time.deltaTime; if (_spawnTimer > 0.66f) { } else if (_spawnTimer > 0f && _spawnTimer < 0.33f) { } else if (_spawnTimer > 0.33f && _spawnTimer < 0.66f) { if (_deathTimer) { _deathTimer.transform.position = Vector3.zero; } } else if (_spawnTimer < 0f) { _spawnTimer = 1f; _spawnTime--; if (_isPlayer) { _deathTimer.transform.position = Vector3.zero; } if (_spawnTime == 4) { if (_isPlayer) { _deathTimer.GetComponent <SpriteRenderer>().sprite = Timer4; } } if (_spawnTime == 3) { if (_isPlayer) { _deathTimer.GetComponent <SpriteRenderer>().sprite = Timer3; } } if (_spawnTime == 2) { if (_isPlayer) { _deathTimer.GetComponent <SpriteRenderer>().sprite = Timer2; } } if (_spawnTime == 1) { if (_isPlayer) { _deathTimer.GetComponent <SpriteRenderer>().sprite = Timer1; } } if (_spawnTime == 0) { if (_isPlayer) { _deathTimer.GetComponent <SpriteRenderer>().sprite = TimerReady; } } if (_spawnTime == -1) { _spawnTime = 5; if (_isPlayer) { _deathTimer.GetComponent <SpriteRenderer>().sprite = Timer5; _deathTimer.transform.position = new Vector3(4.5f, _deathTimer.transform.position.y, 0f); } transform.position = _spawnPoint; _state = State.Playing; } } } else if (_state == State.Playing) { // Poll the controls and update the movement direction if (_activeKeys.Contains(KeyCode.W)) { _direction = MovementDirection.Up; } if (_activeKeys.Contains(KeyCode.S)) { _direction = MovementDirection.Down; } if (_activeKeys.Contains(KeyCode.A)) { _direction = MovementDirection.Left; } if (_activeKeys.Contains(KeyCode.D)) { _direction = MovementDirection.Right; } if (_activeKeys.Contains(KeyCode.W) && _activeKeys.Contains(KeyCode.D)) { _direction = MovementDirection.UpRight; } if (_activeKeys.Contains(KeyCode.W) && _activeKeys.Contains(KeyCode.A)) { _direction = MovementDirection.UpLeft; } if (_activeKeys.Contains(KeyCode.S) && _activeKeys.Contains(KeyCode.D)) { _direction = MovementDirection.DownRight; } if (_activeKeys.Contains(KeyCode.S) && _activeKeys.Contains(KeyCode.A)) { _direction = MovementDirection.DownLeft; } if (_direction == MovementDirection.None) { _moving = false; } else { _moving = true; } // Is the player running? if (_activeKeys.Contains(KeyCode.LeftShift)) { if (_direction == MovementDirection.None) { if (_staminaTimer < _staminaTime) { _staminaTimer += Time.deltaTime * 0.25f; if (_uiStaminaBar != null) { _uiStaminaBar.transform.position += new Vector3(Time.deltaTime * 0.25f, 0f, 0f); } } } else { if (_staminaTimer > 0f) { _running = true; _staminaTimer -= Time.deltaTime; if (_uiStaminaBar != null) { _uiStaminaBar.transform.position -= new Vector3(Time.deltaTime, 0f, 0f); } } else { _running = false; } } } else { _running = false; if (_staminaTimer < _staminaTime) { _staminaTimer += Time.deltaTime * 0.25f; if (_uiStaminaBar != null) { _uiStaminaBar.transform.position += new Vector3(Time.deltaTime * 0.25f, 0f, 0f); } } } if (_moving) { switch (_direction) { case MovementDirection.Up: _vectorDirection.x = 0; _vectorDirection.y = 1; break; case MovementDirection.Down: _vectorDirection.x = 0; _vectorDirection.y = -1; break; case MovementDirection.Left: _vectorDirection.x = -1; _vectorDirection.y = 0; break; case MovementDirection.Right: _vectorDirection.x = 1; _vectorDirection.y = 0; break; case MovementDirection.UpRight: _vectorDirection.x = 0.5f; _vectorDirection.y = 0.5f; break; case MovementDirection.DownRight: _vectorDirection.x = 0.5f; _vectorDirection.y = -0.5f; break; case MovementDirection.UpLeft: _vectorDirection.x = -0.5f; _vectorDirection.y = 0.5f; break; case MovementDirection.DownLeft: _vectorDirection.x = -0.5f; _vectorDirection.y = -0.5f; break; } _vectorDirection = _vectorDirection.normalized; Vector3 tgDelta = _vectorDirection * (_carriesFlag ? _flagSpeed : _speed) * Time.deltaTime; if (_running) { tgDelta *= _sprintMultiplier; } if (!_audioSource1.GetComponent <AudioSource>().isPlaying) { _audioSource1.GetComponent <AudioSource>().clip = _walkAudio[Random.Range(0, _walkAudio.Length)]; _audioSource1.GetComponent <AudioSource>().Play(); } transform.position += tgDelta; } // Camera movements and rotations are only handled if // the character is player controlled. If it's not, // the server updates the rotations if (_isPlayer) { // Get the world space mouse coordinates _mousePosition = Input.mousePosition; //Get the offset of the real 64x64 screen on the screen device int mouse_x_offset = (Screen.width - Screen.height) / 2; _mousePosition.x -= mouse_x_offset; //Adjust the _mousePosition.x to fit only the space that fills the 64x64 screen on de real device _mousePosition.x = Mathf.Min(Mathf.Max(0, _mousePosition.x), Screen.height); //Adjust the _mousePosition.y too _mousePosition.y = Mathf.Min(Mathf.Max(0, _mousePosition.y), Screen.height); //Make the operations to normalize the _mousePosition vector _mousePosition.x /= Screen.height * 0.5f; _mousePosition.y /= Screen.height * 0.5f; _mousePosition.x -= 1f; _mousePosition.y -= 1f; _mousePosition.z = 0f; // Make the character look at the crosshair position Vector3 tgDir = _mousePosition; // Check if the player wants to shoot // First, set the cadence to the current weapon // Second, check if the player has ennough ammo bool canShoot = false; switch (_currentWeapon) { case WeaponType.Pistol: _shootCadence = _pistolShootCadence; break; case WeaponType.Machinegun: _shootCadence = _machineShootCadence; break; case WeaponType.Flamethrower: _shootCadence = _flameShootCadence; break; case WeaponType.RPG: _shootCadence = _rpgShootCadence; break; } if (_currentAmmo == -1) { canShoot = true; } else if (_currentAmmo > 0) { canShoot = true; } else { _currentWeapon = WeaponType.Pistol; _currentAmmo = -1; } // Finally, perform the shoot if (canShoot && _nextShoot && _shootTimer > _shootCadence) { GameObject goPref = _bullet; _currentAmmo--; if (_currentWeapon == PlayerController.WeaponType.Flamethrower) { goPref = _flamethrowerBullet; } else if (_currentWeapon == PlayerController.WeaponType.RPG) { goPref = _rpgBullet; } GameObject goBullet = GameObject.Instantiate(goPref); goBullet.transform.position = transform.position + tgDir.normalized * 0.5f; goBullet.GetComponent <BulletController>().SetWeaponType(_currentWeapon); goBullet.GetComponent <BulletController>().SetDirection(tgDir.normalized); goBullet.GetComponent <BulletController>().SetTeam(_team); // Send the bullet to the other players NetworkController.instance.ShootEvent(_id, tgDir.normalized, _currentWeapon); _shootTimer = 0f; } _shootTimer += Time.deltaTime; _shootAngle = Mathf.Atan2(tgDir.y, tgDir.x) * Mathf.Rad2Deg; if (_shootAngle > 78.5f && _shootAngle <= 101.25f) { _shootAngle = 90f; _animationDirection = 0; _rotationByte = 0x00; } else if (_shootAngle > 56.25f && _shootAngle <= 78.75f) { _shootAngle = 90f; _animationDirection = 22; _rotationByte = 0x01; } else if (_shootAngle > 33.75f && _shootAngle <= 56.25f) { _shootAngle = 90f; _animationDirection = 45; _rotationByte = 0x02; } else if (_shootAngle > 11.25f && _shootAngle <= 33.75f) { _shootAngle = 90f; _animationDirection = 67; _rotationByte = 0x03; } else if (_shootAngle > 101.25f && _shootAngle <= 123.75) { _shootAngle = 180f; _animationDirection = 67; _rotationByte = 0x04; } else if (_shootAngle > 123.75 && _shootAngle <= 145.25) { _shootAngle = 180f; _animationDirection = 45; _rotationByte = 0x05; } else if (_shootAngle > 145.25f && _shootAngle <= 167.75) { _shootAngle = 180f; _animationDirection = 22; _rotationByte = 0x06; } else if ((_shootAngle > 167.75f && _shootAngle <= 180f) || (_shootAngle > -180f && _shootAngle <= -168.75f)) { _shootAngle = 180f; _animationDirection = 0; _rotationByte = 0x07; } else if (_shootAngle > -168.75 && _shootAngle <= -146.25f) { _shootAngle = -90f; _animationDirection = 67; _rotationByte = 0x08; } else if (_shootAngle > -146.25 && _shootAngle <= -123.75f) { _shootAngle = -90f; _animationDirection = 45; _rotationByte = 0x09; } else if (_shootAngle > -123.75 && _shootAngle <= -101.25) { _shootAngle = -90f; _animationDirection = 22; _rotationByte = 0x0A; } else if (_shootAngle > -101.25f && _shootAngle <= -78.75f) { _shootAngle = -90f; _animationDirection = 0; _rotationByte = 0x0B; } else if (_shootAngle > -78.75f && _shootAngle <= -56.25f) { _shootAngle = 0f; _animationDirection = 67; _rotationByte = 0x0C; } else if (_shootAngle > -56.25f && _shootAngle <= -33.75f) { _shootAngle = 0f; _animationDirection = 45; _rotationByte = 0x0D; } else if (_shootAngle > -33.75f && _shootAngle <= -11.25f) { _shootAngle = 0f; _animationDirection = 22; _rotationByte = 0x0E; } else if ((_shootAngle < 45f && _shootAngle >= 0f) || (_shootAngle >= -45f && _shootAngle < 0f)) { _shootAngle = 0f; _animationDirection = 0; _rotationByte = 0x0F; } if (_previousRotation != _rotationByte) { _game.ForceSync(); } _previousRotation = _rotationByte; transform.localRotation = Quaternion.AngleAxis(_shootAngle, Vector3.forward); Vector3 tgPos = transform.position + 1.3f * _mousePosition; tgPos.z = -1; _mainCamera.transform.position = tgPos; } else { switch (_rotationByte) { case 0x00: { _shootAngle = 90f; _animationDirection = 0; } break; case 0x01: { _shootAngle = 90f; _animationDirection = 22; } break; case 0x02: { _shootAngle = 90f; _animationDirection = 45; } break; case 0x03: { _shootAngle = 90f; _animationDirection = 67; } break; case 0x04: { _shootAngle = 180f; _animationDirection = 67; } break; case 0x05: { _shootAngle = 180f; _animationDirection = 45; } break; case 0x06: { _shootAngle = 180f; _animationDirection = 22; } break; case 0x07: { _shootAngle = 180f; _animationDirection = 0; } break; case 0x08: { _shootAngle = -90f; _animationDirection = 67; } break; case 0x09: { _shootAngle = -90f; _animationDirection = 45; } break; case 0x0A: { _shootAngle = -90f; _animationDirection = 22; } break; case 0x0B: { _shootAngle = -90f; _animationDirection = 0; } break; case 0x0C: { _shootAngle = 0f; _animationDirection = 67; } break; case 0x0D: { _shootAngle = 0f; _animationDirection = 45; } break; case 0x0E: { _shootAngle = 0f; _animationDirection = 22; } break; case 0x0F: { _shootAngle = 0f; _animationDirection = 0; } break; } transform.localRotation = Quaternion.AngleAxis(_shootAngle, Vector3.forward); } SpriteAnimator spriteAnimator = GetComponent <SpriteAnimator>(); if (_team == Game.GameTeam.Blue) { _animationOffset = 24; } else { _animationOffset = 0; } switch (_currentWeapon) { case WeaponType.Pistol: _animationOffset += 0; spriteAnimator._animations.row[4].column[1].timeToNext = 0.5f; spriteAnimator._animations.row[5].column[1].timeToNext = 0.5f; spriteAnimator._animations.row[6].column[1].timeToNext = 0.5f; spriteAnimator._animations.row[7].column[1].timeToNext = 0.5f; break; case WeaponType.Machinegun: _animationOffset += 0; spriteAnimator._animations.row[4].column[1].timeToNext = 0.1f; spriteAnimator._animations.row[5].column[1].timeToNext = 0.1f; spriteAnimator._animations.row[6].column[1].timeToNext = 0.1f; spriteAnimator._animations.row[7].column[1].timeToNext = 0.1f; break; case WeaponType.Flamethrower: _animationOffset += 8; break; case WeaponType.RPG: _animationOffset += 16; break; } //Animation if (_nextShoot) { if (!_audioSource2.GetComponent <AudioSource>().isPlaying) { switch (_currentWeapon) { case WeaponType.Pistol: _audioSource2.GetComponent <AudioSource>().clip = _pistolAudio; break; case WeaponType.Machinegun: _audioSource2.GetComponent <AudioSource>().clip = _machineAudio[Random.Range(0, _machineAudio.Length)]; break; case WeaponType.Flamethrower: _audioSource2.GetComponent <AudioSource>().clip = _flameAudio; break; case WeaponType.RPG: _audioSource2.GetComponent <AudioSource>().clip = _rpgAudio; break; } _audioSource2.GetComponent <AudioSource>().Play(); } switch (_animationDirection) { case 0: if (_animationLock != 4) { spriteAnimator.SetActiveAnimation(4 + _animationOffset); _animationLock = 4; } break; case 22: if (_animationLock != 5) { spriteAnimator.SetActiveAnimation(5 + _animationOffset); _animationLock = 5; } break; case 45: if (_animationLock != 6) { spriteAnimator.SetActiveAnimation(6 + _animationOffset); _animationLock = 6; } break; case 67: if (_animationLock != 7) { spriteAnimator.SetActiveAnimation(7 + _animationOffset); _animationLock = 7; } break; } } else { switch (_animationDirection) { case 0: if (_animationLock != 0) { spriteAnimator.SetActiveAnimation(0 + _animationOffset); _animationLock = 0; } break; case 22: if (_animationLock != 1) { spriteAnimator.SetActiveAnimation(1 + _animationOffset); _animationLock = 1; } break; case 45: if (_animationLock != 2) { spriteAnimator.SetActiveAnimation(2 + _animationOffset); _animationLock = 2; } break; case 67: if (_animationLock != 3) { spriteAnimator.SetActiveAnimation(3 + _animationOffset); _animationLock = 3; } break; } if (!_moving) { spriteAnimator.SetActiveAnimation(spriteAnimator.GetActiveAnimation(), 0); spriteAnimator.SetAnimationIndex(-1); _animationLock = -1; } } } else if (_state == State.Dead) { _deathWait += Time.deltaTime; if (_deathWait > 0.45f) { _deathWait = 0f; //La animacion de muerte se destruira sola //if (_deathAnimInstance != null) GameObject.Destroy(_deathAnimInstance); _health = _maxHealth; if (_uiHealthBar != null) { _uiHealthBar.transform.localPosition = _uiHealthBarPos; } _state = State.Respawning; } } //Pixel indicators if (_isPlayer) { if (_game._npcBluePixel != null) { float shootAngle = Mathf.Atan2(_game._npcBluePixel.transform.position.y, _game._npcBluePixel.transform.position.x) * Mathf.Rad2Deg; _blueInd.transform.position = transform.position + new Vector3(Mathf.Cos(shootAngle), Mathf.Sin(shootAngle)); } else { float shootAngle = Mathf.Atan2(_game._bluePixelInstance.transform.position.y, _game._bluePixelInstance.transform.position.x) * Mathf.Rad2Deg; _blueInd.transform.position = transform.position + new Vector3(Mathf.Cos(shootAngle), Mathf.Sin(shootAngle)); } if (_game._npcRedPixel != null) { float shootAngle = Mathf.Atan2(_game._npcRedPixel.transform.position.y, _game._npcRedPixel.transform.position.x) * Mathf.Rad2Deg; _redInd.transform.position = transform.position + new Vector3(Mathf.Cos(shootAngle), Mathf.Sin(shootAngle)); } else { float shootAngle = Mathf.Atan2(_game._greenPixelInstance.transform.position.y, _game._greenPixelInstance.transform.position.x) * Mathf.Rad2Deg; _redInd.transform.position = transform.position + new Vector3(Mathf.Cos(shootAngle), Mathf.Sin(shootAngle)); } } }//END update()