public void throwKunai() { // Make the bullet object GameObject newBullet = (GameObject)Instantiate(bulletPrefab); // Setup the bullet’s starting position newBullet.transform.position = bulletSpawnTransform.position; //VERIFICA O SCALE DA KUNEI BASEDO NO SCALE DO PERSONAGEM Vector3 localScale = transform.localScale; if (localScale.x < 0.0f) { newBullet.transform.Rotate(newBullet.transform.rotation.x, newBullet.transform.rotation.y, 180); } // Acquire the PlayerBulletController component on the new object so we can specify some data PlayerBulletController bullCon = newBullet.GetComponent <PlayerBulletController>(); // Set the player object bullCon.playerObject = gameObject; // Launch the bullet! bullCon.launchBullet(); // With the bullet made, set the state of the player back to the previous state onStateChange(currentState); PlayerStateController.stateDelayTimer[(int)PlayerStateController.playerStates.firingWeapon] = Time.time + 0.25f; }
// onStateChange는 게임 코드의 어디서든 플레이어의 상태를 변경하면 호출된다 public void onStateChange(PlayerStateController.playerStates newState) { // 현재 상태와 새로운 상태가 동일하면 중단 - 이미 지정된 상태를 바꿀 필요가 없다. if(newState == currentState) return; // 새로운 상태를 중단시킬 특별한 조건이 없는지 검증한다. if(checkIfAbortOnStateCondition(newState)) return; // 현재의 상태가 새로운 상태로 전환될 수 있는지 확인한다. 아니면 중단. if(!checkForValidStatePair(newState)) return; // 여기까지 도달했다면 이제 상태 변경이 허용된다는 것을 알 수 있다. // 새로운 상태가 무엇인지에 따라 필요한 동작을 하게 하자. switch(newState) { case PlayerStateController.playerStates.idle: playerAnimator.SetBool("Walking", false); break; case PlayerStateController.playerStates.left: playerAnimator.SetBool("Walking", true); break; case PlayerStateController.playerStates.right: playerAnimator.SetBool("Walking", true); break; case PlayerStateController.playerStates.jump: if(playerHasLanded) { // jumpDirection 변수를 이용하여 플레이어가 왼쪽/오른쪽/위쪽으로 점프할지를 지정한다 float jumpDirection = 0.0f; if(currentState == PlayerStateController.playerStates.left) jumpDirection = -1.0f; else if(currentState == PlayerStateController.playerStates.right) jumpDirection = 1.0f; else jumpDirection = 0.0f; // 실제로 점프하는 힘을 적용한다 GetComponent<Rigidbody2D>().AddForce(new Vector2(jumpDirection * playerJumpForceHorizontal, playerJumpForceVertical)); playerHasLanded = false; PlayerStateController.stateDelayTimer[ (int)PlayerStateController.playerStates.jump] = 0f; } break; case PlayerStateController.playerStates.landing: playerHasLanded = true; PlayerStateController.stateDelayTimer[(int)PlayerStateController.playerStates.jump]= Time.time + 0.1f; break; case PlayerStateController.playerStates.falling: PlayerStateController.stateDelayTimer[ (int)PlayerStateController.playerStates.jump] = 0.0f; break; case PlayerStateController.playerStates.kill: break; case PlayerStateController.playerStates.resurrect: transform.position = playerRespawnPoint.transform.position; transform.rotation = Quaternion.identity; break; case PlayerStateController.playerStates.firingWeapon: // 총알 오브젝트를 만든다 GameObject newBullet = (GameObject)Instantiate(bulletPrefab); // 총알의 시작 위치를 설정한다 newBullet.transform.position = bulletSpawnTransform.position; // 새 오브젝트의 PlayerBulletController 컴포넌트를 할당해서 몇 가지 데이터를 지정할 수 있다 PlayerBulletController bullCon = newBullet.GetComponent<PlayerBulletController>(); // 플레이어 오브젝트를 지정한다 bullCon.playerObject = gameObject; // 총알 발사! bullCon.launchBullet(); // 총알이 발사되고 나면 플레이어의 상태를 이전 상태로 되돌린다 onStateChange(currentState); PlayerStateController.stateDelayTimer[(int)PlayerStateController.playerStates.firingWeapon] = Time.time + 0.25f; break; } // 현재의 상태를 이전 상태로 저장해 둔다 previousState = currentState; // 최종적으로 새로운 상태를 플레이어 오브젝트에 할당한다. currentState = newState; }
public void Fire() { if (!stop) { if (Bazooka == true) { bulletSpawnTransform = GameObject.Find("PLSpawn").transform; if (!fire && !timedFire && !onLadder && !stuckToWall) { if (pickup.bazookaAmmo > 0f) { Vib100(); isAttacking = true; fire = true; animator.SetBool("BazookaFire", true); GetComponent <AudioSource>().PlayOneShot(Launch, 1.0f); GameObject newBullet = (GameObject)Instantiate(bulletPrefab); GameObject newKick = (GameObject)Instantiate(rocketKick); PlayerBulletController bullCon = newBullet.GetComponent <PlayerBulletController>(); ParticleFlip kickCon = newKick.GetComponent <ParticleFlip>(); bullCon.playerObject = gameObject; kickCon.playerObject = gameObject; bullCon.launchBullet(); kickCon.launchBullet(); newBullet.transform.position = bulletSpawnTransform.position; newKick.transform.position = bulletSpawnTransform.position; pickup.bazookaAmmo--; } else if (pickup.bazookaAmmo == 0f) { GetComponent <AudioSource>().PlayOneShot(outOfAmmo); } } } if (Shotgun == true) { shotgunBulletSpawn = GameObject.Find("ShotGunSpawnPoint").transform; if (!fire && !onLadder && !stuckToWall) { if (pickup.shotgunAmmo > 0f) { Vib100(); isAttacking = true; fire = true; animator.SetBool("ShotgunFire", true); GetComponent <AudioSource>().PlayOneShot(shotgunShot, 1.0f); GameObject newBullet = (GameObject)Instantiate(shotgunBullet); GameObject newKick = (GameObject)Instantiate(shotgunKick); ShotgunController bullCon = newBullet.GetComponent <ShotgunController>(); ParticleFlip kickCon = newKick.GetComponent <ParticleFlip>(); bullCon.playerObject = gameObject; kickCon.playerObject = gameObject; bullCon.ShotgunShoot(); kickCon.ShotgunShoot(); newBullet.transform.position = shotgunBulletSpawn.position; newKick.transform.position = shotgunBulletSpawn.position; pickup.shotgunAmmo--; } else if (pickup.shotgunAmmo == 0f) { GetComponent <AudioSource>().PlayOneShot(outOfAmmo); } } } if (Watergun == true) { watergunBulletSpawn = GameObject.Find("WaterGunSpawnPoint").transform; if (!fire && !onLadder && !stuckToWall) { Vib100(); isAttacking = true; fire = true; animator.SetBool("WaterGunFire", true); GetComponent <AudioSource>().PlayOneShot(watergunShot, 1.0f); GameObject newBullet = (GameObject)Instantiate(watergunBullet); GameObject newKick = (GameObject)Instantiate(watergunKick); WaterGunController bullCon = newBullet.GetComponent <WaterGunController>(); ParticleFlip kickCon = newKick.GetComponent <ParticleFlip>(); bullCon.playerObject = gameObject; kickCon.playerObject = gameObject; bullCon.WaterGunShoot(); kickCon.WaterGunShoot(); newBullet.transform.position = watergunBulletSpawn.position; newKick.transform.position = watergunBulletSpawn.position; } } if (Staff) { staffBulletSpawn = GameObject.Find("StaffSpawnPoint").transform; if (!fire && !onLadder && !stuckToWall) { staffClicks++; if (staffClicks == 1) { staffTimer = 0.5f; } else if (pickup.staffAmmo > 5f && staffClicks == 2 && !staffFired) { if (staffTimer > 0.3f) { Vib500(); isAttacking = true; fire = true; stop = true; Stop(); animator.SetBool("StaffBoom", true); GameObject newPower = Instantiate(power); newPower.transform.position = staffBulletSpawn.position; pickup.staffAmmo -= 5; } } else if (pickup.staffAmmo == 0f) { GetComponent <AudioSource>().PlayOneShot(outOfAmmo); } } } if (Granade == true) { granadeBulletSpawn = GameObject.Find("GranadeSpawnPoint").transform; if (!fire && !timedFire && !onLadder && !stuckToWall) { if (pickup.granadeAmmo > 0f) { isAttacking = true; fire = true; animator.SetBool("GranadeFire", true); } else if (pickup.granadeAmmo == 0f) { GetComponent <AudioSource>().PlayOneShot(outOfAmmo); } } } if (Boomerang == true) { boomerangBulletSpawn = GameObject.Find("BoomerangSpawnPoint").transform; if (!fire && !timedFire && !onLadder && !stuckToWall) { isAttacking = true; fire = true; animator.SetBool("BoomerangFire", true); } } } }
public void onStateChange(PlayerStateController.playerStates newState) { if (newState == currentState) { return; } // Comprovar que no hi hagi condicions per abortar l'estat if (checkIfAbortOnStateCondition(newState)) { return; } if (!checkForValidStatePair(newState)) { return; } Debug.Log(newState + ", current: " + currentState); rb2D = GetComponent <Rigidbody2D>(); switch (newState) { case PlayerStateController.playerStates.idle: playerAnimator.SetBool("Walking", false); break; case PlayerStateController.playerStates.left: playerAnimator.SetBool("Walking", true); break; case PlayerStateController.playerStates.right: playerAnimator.SetBool("Walking", true); break; case PlayerStateController.playerStates.jump: if (playerHasLanded) { if (right) { if (playerJumpStrongY < 0) { playerJumpStrongY = playerJumpStrongY * -1f; } rb2D.AddForce(new Vector2(playerJumpStrongY, playerJumpStrongX)); } else { if (playerJumpStrongY < 0) { rb2D.AddForce(new Vector2(playerJumpStrongY, playerJumpStrongX)); } else { playerJumpStrongY = playerJumpStrongY * -1f; rb2D.AddForce(new Vector2(playerJumpStrongY, playerJumpStrongX)); } } } playerHasLanded = false; Debug.Log("landing = " + playerHasLanded); break; case PlayerStateController.playerStates.landing: // Debug.Log("Landed " + playerHasLanded); playerHasLanded = true; // Debug.Log("Landed " + playerHasLanded); break; case PlayerStateController.playerStates.falling: break; case PlayerStateController.playerStates.kill: break; case PlayerStateController.playerStates.resurrect: transform.position = playerRespawnPoint.transform.position; transform.rotation = Quaternion.identity; rb2D.velocity = Vector2.zero; break; case PlayerStateController.playerStates.firingWeapon: PlayerStateController.stateDelayTimer[(int)PlayerStateController.playerStates.firingWeapon] = Time.time + 0.25f; GameObject newBullet = (GameObject)Instantiate(bulletPrefab); newBullet.transform.position = bulletSpawnTransform.position; PlayerBulletController bullCon = newBullet.GetComponent <PlayerBulletController>(); bullCon.playerObject = gameObject; bullCon.launchBullet(); onStateChange(currentState); break; case PlayerStateController.playerStates.firingWeapon2: PlayerStateController.stateDelayTimer[(int)PlayerStateController.playerStates.firingWeapon2] = Time.time + 0.25f; GameObject newBullet2 = (GameObject)Instantiate(bulletPrefab2); newBullet2.transform.position = bulletSpawnTransform.position; PlayerBulletController2 bullCon2 = newBullet2.GetComponent <PlayerBulletController2>(); bullCon2.playerObject = gameObject; bullCon2.launchBullet(); onStateChange(currentState); break; } previousState = currentState; currentState = newState; }
// funkce reagujici na event zmeny stavu hrace. public void onStateChange(PlayerStateController.playerStates newState) { // kontorlujeme zda se novy stav nerovna staremu stavu, neni nutno provadet akci if (newState == currentState) { return; } // zkontrolujeme specialni podminky zmeny stavu if (checkIfAbortOnStateCondition(newState)) { return; } // zkontrolujeme, jestli novy stav muze prepsat stav, ve kterem se nachazi hrac if (!checkForValidStatePair(newState)) { return; } // muzeme bezpecne provezt akce podle noveho stavu switch (newState) { // nastavime promenne ovladajici animace pohybu case PlayerStateController.playerStates.idle: playerAnimator.SetBool("Walking", false); playerAnimator.SetBool("Hurted", false); break; case PlayerStateController.playerStates.left: playerAnimator.SetBool("Walking", true); playerAnimator.SetBool("Hurted", false); break; case PlayerStateController.playerStates.right: playerAnimator.SetBool("Walking", true); playerAnimator.SetBool("Hurted", false); break; case PlayerStateController.playerStates.jump: playerAnimator.SetBool("Grounded", false); playerAnimator.SetBool("Hurted", false); if (playerHasLanded) { // nastavime jumpDirection podle tooh, jestli se skace doprava nebo doleva float jumpDirection = 0.0f; if (currentState == PlayerStateController.playerStates.left) { jumpDirection = -1.0f; } else if (currentState == PlayerStateController.playerStates.right) { jumpDirection = 1.0f; } else { jumpDirection = 0.0f; } // aplikujeme jumpDirection jako silu pusobici na fyzicke telo hrace GetComponent <Rigidbody2D>().AddForce(new Vector2(jumpDirection * playerJumpForceHorizontal, playerJumpForceVertical)); // hrac je ve vzduchu playerHasLanded = false; // nemuzeme skakat znovu - vypneme stav - skok PlayerStateController.stateDelayTimer[(int)PlayerStateController.playerStates.jump] = 0f; } break; // hrac pristal na zemi case PlayerStateController.playerStates.landing: playerAnimator.SetBool("Grounded", true); playerAnimator.SetBool("Hurted", false); playerHasLanded = true; PlayerStateController.stateDelayTimer[(int)PlayerStateController.playerStates.jump] = Time.time + 0.1f; break; // hrac ve vzduchu case PlayerStateController.playerStates.falling: playerAnimator.SetBool("Grounded", false); playerAnimator.SetBool("Hurted", false); PlayerStateController.stateDelayTimer[(int)PlayerStateController.playerStates.jump] = 0.0f; break; case PlayerStateController.playerStates.takingDMG: #if UNITY_ANDROID if (PlayerPrefs.GetInt("vibrations") == 1) { Handheld.Vibrate(); } #endif playerAnimator.SetBool("Hurted", true); playerHealth--; if (OnTakingDmgAction != null) { OnTakingDmgAction(playerHealth); } break; case PlayerStateController.playerStates.immortal: PlayerStateController.stateDelayTimer[(int)PlayerStateController.playerStates.takingDMG] = Time.time + 0.5f; break; case PlayerStateController.playerStates.kill: break; // oziveni hrace na predem specifikovane pozici case PlayerStateController.playerStates.resurrect: playerHealth = PlayerHealthDefault; playerAnimator.SetBool("Hurted", true); resetPosition(); if (onRessurectAction != null) { onRessurectAction(playerHealth, -15); } break; case PlayerStateController.playerStates.firingWeapon: playerAnimator.Play("PlayerFireAnim"); // Vytvor bullet object GameObject newBullet = (GameObject)Instantiate(bulletPrefab); // urci pocatecni pozici newBullet.transform.position = bulletSpawnTransform.position; // ziskame z gameObjectu Bullet jeji komponentu - skript PlayerBulletController PlayerBulletController bullCon = newBullet.GetComponent <PlayerBulletController>(); // priradime gameObject - player bullCon.PlayerObject = gameObject; // vypalime bullCon.launchBullet(); // zmenime stav na puvodni onStateChange(currentState); PlayerStateController.stateDelayTimer[(int)PlayerStateController.playerStates.firingWeapon] = Time.time + fireDelay; break; } // nastavime novy stav currentState = newState; }
public void onStateChange(PlayerStateController.PlayerStates newState) { if (newState == currentState) { return; } if (!checkForValidStatePair(newState)) { return; } if (checkIfAbortOnStateCondition(newState)) { return; } switch (newState) { case PlayerStateController.PlayerStates.idle: playerAnimator.SetBool("Walking", false); break; case PlayerStateController.PlayerStates.left: case PlayerStateController.PlayerStates.right: playerAnimator.SetBool("Walking", true); break; case PlayerStateController.PlayerStates.jump: if (playerHasLanded) { float jumpDirection = 0.0f; if (currentState == PlayerStateController.PlayerStates.left) { jumpDirection = -1.0f; } else if (currentState == PlayerStateController.PlayerStates.right) { jumpDirection = 1.0f; } else { jumpDirection = 0.0f; } body.AddForce(new Vector2(jumpDirection * playerJumpForceHorizontal, playerJumpForceVertical)); playerHasLanded = false; PlayerStateController.stateDelayTimer[(int)PlayerStateController.PlayerStates.jump] = 0f; } break; case PlayerStateController.PlayerStates.landing: playerHasLanded = true; PlayerStateController.stateDelayTimer[(int)PlayerStateController.PlayerStates.jump] = Time.time + 0.1f; break; case PlayerStateController.PlayerStates.falling: PlayerStateController.stateDelayTimer[(int)PlayerStateController.PlayerStates.jump] = 0f; break; case PlayerStateController.PlayerStates.kill: break; case PlayerStateController.PlayerStates.resurrect: transform.position = playerRespawnPoint.transform.position; transform.rotation = Quaternion.identity; body.velocity = Vector2.zero; break; case PlayerStateController.PlayerStates.firingWeapon: GameObject newBullet = (GameObject)Instantiate(bulletPrefab); newBullet.transform.position = bulletSpawnTransform.position; PlayerBulletController bullCon = newBullet.GetComponent <PlayerBulletController>(); bullCon.player = gameObject; bullCon.launchBullet(); onStateChange(currentState); PlayerStateController.stateDelayTimer[(int)PlayerStateController.PlayerStates.firingWeapon] = Time.time + 0.25f; break; } previousState = currentState; currentState = newState; }
// onStateChange es crida sempre que canvia l'estat del player public void onStateChange(PlayerStateController.playerStates newState) { // Si l'estat actual i el nou són el mateix, no cal fer res if (newState == currentState) { return; } // Comprovar que no hi hagi condicions per abortar l'estat if (checkIfAbortOnStateCondition(newState)) { return; } // Comprovar que el pas de l'estat actual al nou estat està permès. Si no està, no es continua. if (!checkForValidStatePair(newState)) { return; } // Realitzar les accions necessàries en cada cas per canviar l'estat. // De moment només es gestionen els estats idle, right i left switch (newState) { case PlayerStateController.playerStates.idle: playerAnimator.SetBool("Walking", false); break; case PlayerStateController.playerStates.left: playerAnimator.SetBool("Walking", true); break; case PlayerStateController.playerStates.right: playerAnimator.SetBool("Walking", true); break; case PlayerStateController.playerStates.jump: Debug.Log(newState); if (playerHasLanded) { // jumpDirection determina si el salt es a la dreta, esquerra o vertical float jumpDirection = 0.0f; if (currentState == PlayerStateController.playerStates.left) { jumpDirection = -1.0f; } else if (currentState == PlayerStateController.playerStates.right) { jumpDirection = 1.0f; } else { jumpDirection = 0.0f; } // aplicar la força per fer el salt GetComponent <Rigidbody2D>().AddForce(new Vector2(jumpDirection * playerJumpForceHorizontal, playerJumpForceVertical)); //indicar que el Player esta saltant en l'aire playerHasLanded = false; } break; case PlayerStateController.playerStates.landing: playerHasLanded = true; break; case PlayerStateController.playerStates.falling: break; case PlayerStateController.playerStates.kill: break; case PlayerStateController.playerStates.resurrect: //posicio: la de PlayerRespawnPoint transform.position = playerRespawnPoint.transform.position; transform.rotation = Quaternion.identity; //rotacio: cap GetComponent <Rigidbody2D>().velocity = Vector2.zero; //velocitat lineal: zero break; case PlayerStateController.playerStates.firingWeapon: // Construir l'objecte bala a partir del Prefab GameObject newBullet = (GameObject)Instantiate(bulletPrefab); // Establir la posicio inicial de la bala creada //(la posicio de BulletSpawnTransform) newBullet.transform.position = bulletSpawnTransform.position; // Agafar el component PlayerBulletController de la bala //que s'ha creat // Establir temps a partir del qual es pot tornar a disparar PlayerStateController.stateDelayTimer[(int)PlayerStateController.playerStates.firingWeapon] = Time.time + 0.25f; PlayerBulletController bullCon = newBullet.GetComponent <PlayerBulletController>(); // Assignar a l'atribut playerObject de l'script //PlayerBulletController el Player bullCon.playerObject = gameObject; // Invocar metode que dispara la bala bullCon.launchBullet(); // Despres de disparar, tornar a l'estat previ. onStateChange(currentState); break; case PlayerStateController.playerStates.firingMortar: // Construir l'objecte bala a partir del Prefab GameObject newBullet2 = (GameObject)Instantiate(bulletPrefab2); // Establir la posicio inicial de la bala creada //(la posicio de BulletSpawnTransform) newBullet2.transform.position = bulletSpawnTransform.position; // Agafar el component PlayerBulletController de la bala //que s'ha creat // Establir temps a partir del qual es pot tornar a disparar PlayerStateController.stateDelayTimer[(int)PlayerStateController.playerStates.firingMortar] = Time.time + 0.25f; PlayerBulletController2 bullCon2 = newBullet2.GetComponent <PlayerBulletController2>(); // Assignar a l'atribut playerObject de l'script //PlayerBulletController el Player bullCon2.playerObject = gameObject; // Invocar metode que dispara la bala bullCon2.launchBullet(); // Despres de disparar, tornar a l'estat previ. onStateChange(currentState); break; } // Guardar estat actual com a estat previ previousState = currentState; // Assignar el nou estat com a estat actual del player currentState = newState; }
// onStateChange is called whenever we make a change to the player's state // from anywhere within the game's code. public void onStateChange(PlayerStateController.playerStates newState) { // If the current state and the new state are the same, abort - no need // to change to the state we're already in. if (newState == currentState) { return; } // Verify there are no special conditions that would cause this state to abort if (checkIfAbortOnStateCondition(newState)) { return; } // Check if the current state is allowed to transition into this state. If it's not, abort. if (!checkForValidStatePair(newState)) { return; } // Having reached here, we now know that this state change is allowed. // So let's perform the necessary actions depending on what the new state is. switch (newState) { case PlayerStateController.playerStates.idle: playerAnimator.SetBool("Walking", false); break; case PlayerStateController.playerStates.left: playerAnimator.SetBool("Walking", true); break; case PlayerStateController.playerStates.right: playerAnimator.SetBool("Walking", true); break; case PlayerStateController.playerStates.jump: if (playerHasLanded) { // Use the jumpDirection variable to specify if the player should be jumping left, right or vertical float jumpDirection = 0.0f; if (currentState == PlayerStateController.playerStates.left) { jumpDirection = -1.0f; } else if (currentState == PlayerStateController.playerStates.right) { jumpDirection = 1.0f; } else { jumpDirection = 0.0f; } // Apply the actual jump force rigidbody2D.AddForce(new Vector2(jumpDirection * playerJumpForceHorizontal, playerJumpForceVertical)); playerHasLanded = false; PlayerStateController.stateDelayTimer[(int)PlayerStateController.playerStates.jump] = 0f; } break; case PlayerStateController.playerStates.landing: playerHasLanded = true; PlayerStateController.stateDelayTimer[(int)PlayerStateController.playerStates.jump] = Time.time + 0.1f; break; case PlayerStateController.playerStates.falling: PlayerStateController.stateDelayTimer[(int)PlayerStateController.playerStates.jump] = 0.0f; break; case PlayerStateController.playerStates.kill: break; case PlayerStateController.playerStates.resurrect: transform.position = playerRespawnPoint.transform.position; transform.rotation = Quaternion.identity; rigidbody2D.velocity = Vector2.zero; break; case PlayerStateController.playerStates.firingWeapon: // Make the bullet object GameObject newBullet = (GameObject)Instantiate(bulletPrefab); // Setup the bullet’s starting position newBullet.transform.position = bulletSpawnTransform.position; // Acquire the PlayerBulletController component on the new object so we can specify some data PlayerBulletController bullCon = newBullet.GetComponent <PlayerBulletController>(); // Set the player object bullCon.playerObject = gameObject; // Launch the bullet! bullCon.launchBullet(); // With the bullet made, set the state of the player back to the current state onStateChange(currentState); PlayerStateController.stateDelayTimer[(int)PlayerStateController.playerStates.firingWeapon] = Time.time + 0.25f; break; } // Store the current state as the previous state previousState = currentState; // And finally, assign the new state to the player object currentState = newState; }