/// <summary> /// checks and updates the player's horizontal movement. /// NOTE: MEANT TO BE USED IN THE UPDATE METHOD ONLY. /// </summary> /// <param name="gameTime">the current game time.</param> /// <param name="keyboard">the state of the keyboard.</param> private void UpdateHorizontal(GameTime gameTime, KeyboardState keyboard) { if (keyboard.IsKeyDown(Keys.Left)) { if (verticalState == VerticalMovementState.Jumping || verticalState == VerticalMovementState.Falling) { animationState = PlayerAnimState.JumpingLeft; } else { animationState = PlayerAnimState.WalkingLeft; } Position.X -= PLAYER_SPEED; } else if (keyboard.IsKeyDown(Keys.Right)) { if (verticalState == VerticalMovementState.Jumping || verticalState == VerticalMovementState.Falling) { animationState = PlayerAnimState.JumpingRight; } else { animationState = PlayerAnimState.WalkingRight; } Position.X += PLAYER_SPEED; } else { animationState = PlayerAnimState.Idle; } }
public void ResetForLevel() { m_Coins = 0; m_Position = new Vector3( 3.0f, 1.0f, 0.0f ); m_lastPosition = new Vector3( 0.0f, 0.0f, 0.0f ); m_realAnimRunSpeed = m_animRunSpeed; m_renderer.m_TextureIdx = 0; m_animState = PlayerAnimState.RUNNING; m_Colliding = false; m_CollidedBounces = 2; m_animFrame = 0; m_animFrameTime = 0.0f; m_animRunSpeed = 16.0f; m_realAnimRunSpeed = 16.0f; m_jumpVelocity = 0.0f; m_jumpFloatVelocity = 5.0f; m_fallTimer = 0.0f; m_highAnalogueTimer = 0.0f; }
public void Start() { rBody = gameObject.GetComponent <Rigidbody2D>(); playerAnimState = PlayerAnimState.IDLE; Ow = GetComponent <AudioSource>(); Jump = GetComponent <AudioSource>(); }
private void CompleteAction() { if (animState == PlayerAnimState.Action) { animState = PlayerAnimState.Idle; } }
/// <summary> /// 改变玩家状态方法 /// </summary> /// <param name="state">玩家状态</param> public void ChangeState(PlayerAnimState state) { switch (state) { case PlayerAnimState.run: playerAnimator.SetBool("IsPlay", true); playerAnimator.SetBool("IsIdle", false); break; case PlayerAnimState.idle: playerAnimator.SetBool("IsIdle", true); playerAnimator.SetBool("IsOpen", false); playerAnimator.SetBool("IsPlay", false); break; case PlayerAnimState.use: playerAnimator.SetBool("IsOpen", true); playerAnimator.SetBool("IsIdle", false); break; case PlayerAnimState.eat: playerAnimator.SetBool("IsEat", true); playerAnimator.SetBool("IsOpen", false); break; case PlayerAnimState.fail: playerAnimator.SetBool("IsUnhappy", true); playerAnimator.SetBool("IsOpen", false); break; default: break; } }
public void Reset() { life = 4; animationState = PlayerAnimState.Idle; verticalState = VerticalMovementState.OnGround; spriteEffects = SpriteEffects.None; color = Color.White; Position = new Vector2(200, 200); }
// Start is called before the first frame update void Start() { // rb2d = GetComponent<Rigidbody2D>(); //anim = GetComponent<Animator>(); direction = true; playerAnimState = PlayerAnimState.IDLE; normalforce = jumpForce; waterforce = jumpForce / 2; }
private void Move() { isGrounded = Physics2D.BoxCast( transform.position, new Vector2(2.0f, 1.0f), 0.0f, Vector2.down, 1.0f, 1 << LayerMask.NameToLayer("Ground")); //Idle if (Input.GetAxis("Horizontal") == 0) { playerAnimState = PlayerAnimState.IDLE; playerAnimator.SetInteger("AnimState", (int)PlayerAnimState.IDLE); } //Move Right if (Input.GetAxis("Horizontal") > 0) { transform.localScale = new Vector3(0.2f, 0.2f, 1.0f); if (isGrounded) { playerAnimState = PlayerAnimState.RUN; playerAnimator.SetInteger("AnimState", (int)PlayerAnimState.RUN); playerRigidBody.AddForce(new Vector2(1, 0.1f) * moveForce); } } //Move Left if (Input.GetAxis("Horizontal") < 0) { transform.localScale = new Vector3(-0.2f, 0.2f, 1.0f); if (isGrounded) { playerAnimState = PlayerAnimState.RUN; playerAnimator.SetInteger("AnimState", (int)PlayerAnimState.RUN); playerRigidBody.AddForce(new Vector2(-1, 0.1f) * moveForce); } } //Jump if (Input.GetAxis("Jump") > 0 && (isGrounded)) { playerAnimState = PlayerAnimState.JUMP; playerAnimator.SetInteger("AnimState", (int)PlayerAnimState.JUMP); playerRigidBody.AddForce(Vector2.up * jumpForce); isGrounded = false; jumpSound.Play(); } //Cap Player Speed playerRigidBody.velocity = new Vector2( Mathf.Clamp(playerRigidBody.velocity.x, -maximumVelocity.x, maximumVelocity.x), Mathf.Clamp(playerRigidBody.velocity.y, -maximumVelocity.y, maximumVelocity.y) ); }
//public AudioSource jumpSound; // Start is called before the first frame update void Start() { playerAnimState = PlayerAnimState.IDLE; isGrounded = false; punchSound.volume = 0.5f; jumpSound.volume = 0.5f; deadSound.volume = 0.5f; }
public void Initialize() { Position = new Vector2(240, 500); state = State.Idle; animationState = PlayerAnimState.Idle; verticalState = VerticalMovementState.OnGround; Bounds.Width = FRAME_WIDTH; Bounds.Height = FRAME_HEIGHT; }
public void Reset() { life = 4; animationState = PlayerAnimState.Idle; verticalState = VerticalMovementState.OnGround; spriteEffects = SpriteEffects.None; color = Color.White; Position = OriginalPosition; dead = false; }
public void Damage(int damage) { life -= damage; hurtNoise.Play(); if (life <= 0) { animationState = PlayerAnimState.Dead; currentFrame = frames.Length - 1; dead = true; } }
/// <summary> /// Constructs a new player /// </summary> /// <param name="frames">The sprite frames associated with the player</param> /// <param name="x">The spawn point x</param> /// <param name="y">The spawn point y</param> /// <param name="jumpNoise">The sound effect to play when the player jumps</param> /// <param name="hurtNoise">The sound effect to play when the player gets hurt</param> /// <param name="jumpParticles">The particle system to activate when the player jumps</param> /// <param name="coinParticles">The particle system to activate when the player collects a coin</param> public Player(IEnumerable <Sprite> frames, uint x, uint y, SoundEffect jumpNoise, SoundEffect hurtNoise, ParticleSystem coinParticles) { this.frames = frames.ToArray(); animationState = PlayerAnimState.WalkingLeft; Position.X = x + 10; Position.Y = y + 21; OriginalPosition = Position; this.jumpNoise = jumpNoise; this.hurtNoise = hurtNoise; this.coinParticles = coinParticles; }
// Handles Player movement private void Move() { isGrounded = Physics2D.BoxCast( transform.position - new Vector3(0, playerSpriteRenderer.bounds.extents.y + 0.08f, 0), new Vector2(0.9f, 0.08f), 0.0f, Vector2.down, 0.08f, 1 << LayerMask.NameToLayer("Ground")); // Idle State if (Input.GetAxis("Horizontal") == 0) { playerAnimState = PlayerAnimState.IDLE; } // Move Right if (Input.GetAxis("Horizontal") > 0) { transform.localScale = new Vector3(1.0f, 1.0f, 1.0f); playerRigidBody.AddForce(new Vector2(1, 0) * moveForce); if (isGrounded) { playerAnimState = PlayerAnimState.RUN; } } // Move Left if (Input.GetAxis("Horizontal") < 0) { transform.localScale = new Vector3(-1.0f, 1.0f, 1.0f); playerRigidBody.AddForce(new Vector2(-1, 0) * moveForce); if (isGrounded) { playerAnimState = PlayerAnimState.RUN; } } jumpCooldownTime += Time.deltaTime; // Jump if ((Input.GetAxis("Jump") > 0) && (isGrounded) && jumpCooldownTime >= jumpCooldown) { playerAnimState = PlayerAnimState.JUMP; jumpSound.Play(); playerRigidBody.AddForce(Vector2.up * jumpForce); isGrounded = false; jumpCooldownTime = 0.0f; //jumpSound.Play(); } playerAnimator.SetInteger("AnimState", (int)playerAnimState); playerRigidBody.velocity = new Vector2( Mathf.Clamp(playerRigidBody.velocity.x, -maximumVelocity.x, maximumVelocity.x), Mathf.Clamp(playerRigidBody.velocity.y, -maximumVelocity.y, maximumVelocity.y) ); }
private void AirAnimation() { if (!anim_air_did_up) { broom.exists = false; anim_air_did_up = true; if (ON_CONVEYER) { SoundManager.PlaySoundEffect("puddle_up"); } else { SoundManager.PlaySoundEffect("player_jump_up"); } shadow.Play("get_small"); ANIM_STATE = PlayerAnimState.as_idle; // Always land in idle state. PlayFacing("jump"); } if (!anim_air_did_down && jump_timer > jump_period / 2) { shadow.Play("get_big"); anim_air_did_down = true; } offset.Y = DEFAULT_Y_OFFSET + (((-4 * 24) / (jump_period * jump_period)) * jump_timer * (jump_timer - jump_period)); jump_timer += GameTimes.DeltaTime; if (jump_timer > jump_period) { jump_timer = 0; if (ON_CONVEYER) { SoundManager.PlaySoundEffect("puddle_down"); } else { SoundManager.PlaySoundEffect("player_jump_down"); } state = PlayerState.GROUND; //my_shadow.visible = false; offset.Y = DEFAULT_Y_OFFSET; just_landed = true; anim_air_did_down = anim_air_did_up = false; } }
private void GoBackToIdle() { //if (this == null) { //So, it has come to this... // return; // } if (IsAlive) { animState = PlayerAnimState.Idle; SetAnimationState(nextMovement); animator.SetInteger("State", (int)animState); } IsAlive = true; this.GetComponent <Collider2D>().enabled = true; }
public Player(PlayState parent) : base(Vector2.Zero, ORIGINAL_WIDTH, ORIGINAL_HEIGHT, Drawing.DrawOrder.ENTITIES) { DEATH_FRAME = 32; this.parent = parent; AddAnimation("walk_d", CreateAnimFrameArray(1, 0), 6, true); AddAnimation("walk_r", CreateAnimFrameArray(2, 3), 8, true); AddAnimation("walk_u", CreateAnimFrameArray(4, 5), 6, true); AddAnimation("walk_l", CreateAnimFrameArray(6, 7), 8, true); AddAnimation("attack_d", CreateAnimFrameArray(8, 9), 10, false); AddAnimation("attack_r", CreateAnimFrameArray(10, 11), 10, false); AddAnimation("attack_u", CreateAnimFrameArray(12, 13), 10, false); AddAnimation("attack_l", CreateAnimFrameArray(14, 15), 10, false); AddAnimation("fall", CreateAnimFrameArray(28, 29, 30, 31), 5, false); AddAnimation("die", CreateAnimFrameArray(25, 26, 27, 24, 25, 26, 27, 24, 25, 26, 27, 32), 6, false); AddAnimation("slumped", CreateAnimFrameArray(32)); AddAnimation("whirl", CreateAnimFrameArray(25, 26, 27, 24), 12, true); AddAnimation("idle_d", CreateAnimFrameArray(24), 4, true); AddAnimation("idle_r", CreateAnimFrameArray(25), 4, true); AddAnimation("idle_u", CreateAnimFrameArray(26), 4, true); AddAnimation("idle_l", CreateAnimFrameArray(27), 4, true); AddAnimation("jump_d", CreateAnimFrameArray(16, 17), 4, true); AddAnimation("jump_r", CreateAnimFrameArray(18, 19), 4, true); AddAnimation("jump_u", CreateAnimFrameArray(20, 21), 4, true); AddAnimation("jump_l", CreateAnimFrameArray(22, 23), 4, true); AddAnimation("idle_climb", CreateAnimFrameArray(33)); AddAnimation("climb", CreateAnimFrameArray(34, 35), 8, true); height = HITBOX_HEIGHT; offset = new Vector2(3, DEFAULT_Y_OFFSET); width = HITBOX_WIDTH; Play("idle_u"); ANIM_STATE = PlayerAnimState.as_idle; facing = Facing.UP; broom = new Broom(this); shadow = new Shadow(this, new Vector2(3, -1), fps: 20); }
public Player() : base(Vector2.Zero, Player_Sprite, ORIGINAL_WIDTH, ORIGINAL_HEIGHT, Drawing.DrawOrder.ENTITIES) { AddAnimation("walk_d", CreateAnimFrameArray(1, 0), 6, true); AddAnimation("walk_r", CreateAnimFrameArray(2, 3), 8, true); AddAnimation("walk_u", CreateAnimFrameArray(4, 5), 6, true); AddAnimation("walk_l", CreateAnimFrameArray(6, 7), 8, true); AddAnimation("attack_d", CreateAnimFrameArray(8, 9), 10, false); AddAnimation("attack_r", CreateAnimFrameArray(10, 11), 10, false); AddAnimation("attack_u", CreateAnimFrameArray(12, 13), 10, false); AddAnimation("attack_l", CreateAnimFrameArray(14, 15), 10, false); AddAnimation("fall", CreateAnimFrameArray(28, 29, 30, 31), 5, false); AddAnimation("slumped", CreateAnimFrameArray(32)); AddAnimation("whirl", CreateAnimFrameArray(25, 26, 27, 24), 12, true); AddAnimation("idle_d", CreateAnimFrameArray(24), 4, true); AddAnimation("idle_r", CreateAnimFrameArray(25), 4, true); AddAnimation("idle_u", CreateAnimFrameArray(26), 4, true); AddAnimation("idle_l", CreateAnimFrameArray(27), 4, true); AddAnimation("jump_d", CreateAnimFrameArray(16, 17), 4, true); AddAnimation("jump_r", CreateAnimFrameArray(18, 19), 4, true); AddAnimation("jump_u", CreateAnimFrameArray(20, 21), 4, true); AddAnimation("jump_l", CreateAnimFrameArray(22, 23), 4, true); AddAnimation("idle_climb", CreateAnimFrameArray(33)); AddAnimation("climb", CreateAnimFrameArray(34, 35), 8, true); height = HITBOX_HEIGHT; offset = new Vector2(3, DEFAULT_Y_OFFSET); width = HITBOX_WIDTH; Play("idle_u"); ANIM_STATE = PlayerAnimState.as_idle; facing = Facing.UP; broom = new(this); transformer = new(this); shadow = new(this, new Vector2(3, -1), fps : 20); foot_overlay = new(this); reflection = new(this); light = new(this); }
private void ResetAfterFalling() { fallTimer -= GameTimes.DeltaTime; if (_curFrame == 31) { Position = grid_entrance; hasFallen = false; Flicker(1); Play("idle_d"); Solid = false; justFell = true; dontMove = false; ANIM_STATE = PlayerAnimState.as_idle; //TODO: Re-enable for Kaizo mode? //ReceiveDamage(1, false, false); } }
// public void SetFaceDir(float faceDir) // { // Instance._animator.SetFloat("faceDir", faceDir); // } public void SetAnim(PlayerAnimState playerState) { if (playerState == _lastState) { return; } ReSetAnim(); switch (playerState) { case PlayerAnimState.Run: _animator.SetBool("isRun", true); break; } _lastState = playerState; }
private void Play(PlayerAnimState state) { playedState = state; switch (state) { case PlayerAnimState.Idle_Float: animator.Play("Idle Float"); break; case PlayerAnimState.Idle_Ground: animator.Play("Idle Stand On Bottom"); break; case PlayerAnimState.Swim: animator.Play("Fox Swim"); break; case PlayerAnimState.Swim_Fast: animator.Play("Fox Swim Fast"); break; case PlayerAnimState.Hurt: animator.Play("Hurt"); break; case PlayerAnimState.Dead: animator.Play("Dead"); break; case PlayerAnimState.Swim_Up: animator.Play("Fox Swim Up"); break; case PlayerAnimState.Walk: animator.Play("Fox Walk"); break; default: throw new ArgumentOutOfRangeException(nameof(state), state, null); } }
private void SlippingLogic() { fallTimer -= GameTimes.DeltaTime; if (just_landed) { fallTimer = -1; just_landed = false; } if ((dashing && fallTimer < FALL_TIMER_DEFAULT / 2) || fallTimer < 0) { SoundManager.PlaySoundEffect("fall_in_hole"); ANIM_STATE = PlayerAnimState.ANIM_FALL; hasFallen = true; isSlipping = false; dontMove = true; Position = fallPoint + new Vector2(3, 5); } }
/// <summary> /// 改变状态方法 /// </summary> public void ChangeState(PlayerAnimState state) { switch (state) { case PlayerAnimState.run: playerAnimator.SetBool("IsPlay", true); playerAnimator.SetBool("IsIdle", false); break; case PlayerAnimState.idle: playerAnimator.SetBool("IsIdle", true); playerAnimator.SetBool("IsOpen", false); playerAnimator.SetBool("IsPlay", false); break; case PlayerAnimState.walk: playerAnimator.SetBool("IsOpen", true); playerAnimator.SetBool("IsIdle", false); break; } }
private void Update_actions() { if (action_latency > 0) { action_latency -= GameTimes.DeltaTime; } if (state != PlayerState.AIR) { if (KeyInput.JustPressedRebindableKey(KeyFunctions.Accept) && action_latency <= 0 && !skipBroom) { if (InventoryManager.EquippedBroom != BroomType.NONE && !broom.exists) { broom.visible_timer = 0; broom.exists = true; broom.facing = facing; action_latency = action_latency_max; broom.Play("stab", true); SoundManager.PlaySoundEffect("swing_broom_1", "swing_broom_2", "swing_broom_3"); ANIM_STATE = PlayerAnimState.ANIM_ATK; } else if (InventoryManager.EquippedBroom == BroomType.Transformer) { //TODO transformer stuff } } skipBroom = false; if (KeyInput.JustPressedRebindableKey(KeyFunctions.Cancel) && !sinking) { state = PlayerState.AIR; shadow.visible = true; broom.exists = false; } } }
void Start() { timer = new Timer(PLAYER_SPEED, CompleteMoving); timer.repeating = true; actionTimer = new Timer(PLAYER_SPEED, CompleteAction); var detector = FindObjectOfType <HandyDetector>(); if (detector != null) { detector.defaultObject = transform; } previousPosition = player.position; animState = PlayerAnimState.Idle; canSwitch = true; canMove = true; GroupManager.main.group["Running"].Add(this); GroupManager.main.group["Running"].Add(animator); //GroupManager.main.group["Running"].Add(this, new GroupDelegator(null, ()=>{canMove = true;}, null)); GroupManager.main.group["To Level Over"].Add(this, new GroupDelegator(null, GoBackToIdle, null)); GroupManager.main.group["To Level Over"].Add(animator); // not sure which one is better. GroupManager.main.group["Dialogue"].Add(this, new GroupDelegator(null, GoBackToIdle, null)); GroupManager.main.group["Dialogue"].Add(animator); KeyboardController.Instance.KeyboardEventHandler = this; ashes = Entity.Spawn(gameObject, ashes); _ashController = ashes.GetComponent <Ashes>(); ashes.SetActive(false); IsAlive = true; }
public Player(IEnumerable <Sprite> frames, Race game, int playerType) { this.frames = frames.ToArray(); this.playerType = playerType; this.game = game; animationState = PlayerAnimState.WalkingLeft; if (playerType == 1) { Position.X = 20; Position.Y = 285; left = Keys.A; right = Keys.D; jump = Keys.W; } else if (playerType == 2) { Position.X = 20; Position.Y = 430; left = Keys.Left; right = Keys.Right; jump = Keys.Up; } }
private void startLocalOrNot() { if (!animState) { animState = GetComponent <PlayerAnimState>(); } if (!rb) { rb = GetComponent <Rigidbody>(); } if (!collidr) { collidr = GetComponent <Collider>(); } if (!aud) { aud = GetComponent <AudioSource>(); } if (!debugHUD) { debugHUD = FindObjectOfType <DebugHUD>(); } }
// Update is called once per frame public void Update() { //Call moving function Movement(); //Flip the character Vector3 characterScale = transform.localScale; if (Input.GetAxis("Horizontal") < 0) { facingRight = false; characterScale.x = -13; playerAnimState = PlayerAnimState.WALK; playerAnimator.SetInteger("AnimState", (int)PlayerAnimState.WALK); } if (Input.GetAxis("Horizontal") > 0) { facingRight = true; characterScale.x = 13; playerAnimState = PlayerAnimState.WALK; playerAnimator.SetInteger("AnimState", (int)PlayerAnimState.WALK); } transform.localScale = characterScale; //Return to idle if (Input.GetAxis("Horizontal") == 0) { playerAnimState = PlayerAnimState.IDLE; playerAnimator.SetInteger("AnimState", (int)PlayerAnimState.IDLE); } if (Input.GetButtonDown("Fire1") && Time.time > nextFire) { nextFire = Time.time + fireRate; fire(); } }
/// <summary> /// Updates the player, applying movement and physics /// </summary> /// <param name="gameTime">The GameTime object</param> public void Update(GameTime gameTime) { var keyboard = Keyboard.GetState(); // Vertical movement switch (verticalState) { case VerticalMovementState.OnGround: if (keyboard.IsKeyDown(Keys.Space)) { verticalState = VerticalMovementState.Jumping; jumpTimer = new TimeSpan(0); } break; case VerticalMovementState.Jumping: jumpTimer += gameTime.ElapsedGameTime; // Simple jumping with platformer physics Position.Y -= (250 / (float)jumpTimer.TotalMilliseconds); if (jumpTimer.TotalMilliseconds >= JUMP_TIME) { verticalState = VerticalMovementState.Falling; } break; case VerticalMovementState.Falling: Position.Y += speed; // TODO: This needs to be replaced with collision logic if (Position.Y > 500) { Position.Y = 500; } break; } // Horizontal movement if (keyboard.IsKeyDown(Keys.Left)) { if (verticalState == VerticalMovementState.Jumping || verticalState == VerticalMovementState.Falling) { animationState = PlayerAnimState.JumpingLeft; } else { animationState = PlayerAnimState.WalkingLeft; } Position.X -= speed; } else if (keyboard.IsKeyDown(Keys.Right)) { if (verticalState == VerticalMovementState.Jumping || verticalState == VerticalMovementState.Falling) { animationState = PlayerAnimState.JumpingRight; } else { animationState = PlayerAnimState.WalkingRight; } Position.X += speed; } else { animationState = PlayerAnimState.Idle; } // Apply animations switch (animationState) { case PlayerAnimState.Idle: currentFrame = 0; animationTimer = new TimeSpan(0); break; case PlayerAnimState.JumpingLeft: spriteEffects = SpriteEffects.FlipHorizontally; currentFrame = 7; break; case PlayerAnimState.JumpingRight: spriteEffects = SpriteEffects.None; currentFrame = 7; break; case PlayerAnimState.WalkingLeft: animationTimer += gameTime.ElapsedGameTime; spriteEffects = SpriteEffects.FlipHorizontally; // Walking frames are 9 & 10 if (animationTimer.TotalMilliseconds > FRAME_RATE * 2) { animationTimer = new TimeSpan(0); } currentFrame = (int)Math.Floor(animationTimer.TotalMilliseconds / FRAME_RATE) + 9; break; case PlayerAnimState.WalkingRight: animationTimer += gameTime.ElapsedGameTime; spriteEffects = SpriteEffects.None; // Walking frames are 9 & 10 if (animationTimer.TotalMilliseconds > FRAME_RATE * 2) { animationTimer = new TimeSpan(0); } currentFrame = (int)Math.Floor(animationTimer.TotalMilliseconds / FRAME_RATE) + 9; break; } }
/// <summary> /// Constructs a new player /// </summary> /// <param name="frames">The sprite frames associated with the player</param> public Player(IEnumerable <Sprite> frames) { this.frames = frames.ToArray(); animationState = PlayerAnimState.WalkingLeft; }
void InputHandeling() { //Key Input if(Input.GetKeyDown(KeyCode.Q)){ rigidbody.AddForce(Vector3.left * dodgeAmount * Time.deltaTime, ForceMode.Impulse); }else if(Input.GetKeyDown(KeyCode.E)){ rigidbody.AddForce(Vector3.right * dodgeAmount * Time.deltaTime, ForceMode.Impulse); } //Controls float inputX = Input.GetAxis("Horizontal"); float inputY = Input.GetAxis("Vertical"); if(inputX == 0 && inputY == 0){ playerState = PlayerState.Idle; }else{ playerState = PlayerState.Moving; } //Set player state for animations if(inputX == 0 && inputY == 0){ playerAnimState = PlayerAnimState.Idle; }else if(inputX < 0 && inputY == 0){ playerAnimState = PlayerAnimState.Left; }else if(inputX > 0 && inputY == 0){ playerAnimState = PlayerAnimState.Right; }else if(inputX == 0 && inputY > 0){ playerAnimState = PlayerAnimState.Forward; }else if(inputX == 0 && inputY < 0){ playerAnimState = PlayerAnimState.Back; }else if(inputX < 0 && inputY < 0){ playerAnimState = PlayerAnimState.BackDiagonalLeft; }else if(inputX > 0 && inputY < 0){ playerAnimState = PlayerAnimState.BackDiagonalRight; }else if(inputX < 0 && inputY > 0){ playerAnimState = PlayerAnimState.ForwardDiagonalLeft; }else if(inputX > 0 && inputY > 0){ playerAnimState = PlayerAnimState.ForwardDiagonalRight; } //Camera control RotationX = Input.GetAxis("Mouse X") * sensitivityX * Time.deltaTime; RotationY -= Input.GetAxis("Mouse Y") * sensitivityY * Time.deltaTime; RotationY = Mathf.Clamp(RotationY, minimumY, maximumY); switch(playerState){ case PlayerState.Idle: mCamera.transform.position = new Vector3(mCamera.transform.position.x, RotationY, mCamera.transform.position.z); mCamera.transform.RotateAround(myTransform.position, Vector3.up, RotationX); mCamera.transform.LookAt(myTransform); break; case PlayerState.Moving: myTransform.position += myTransform.forward * moveSpeed * Time.deltaTime; mCamera.transform.position = new Vector3(mCamera.transform.position.x, RotationY, mCamera.transform.position.z); myTransform.Rotate(Vector3.up, RotationX); mCamera.transform.LookAt(myTransform); break; } //Set player animation switch(playerAnimState){ case PlayerAnimState.Idle: break; case PlayerAnimState.Forward: mCamera.transform.parent = null; myTransform.rotation = Quaternion.Euler(myTransform.rotation.x, mCamera.transform.localEulerAngles.y, myTransform.rotation.z); mCamera.transform.parent = myTransform; break; case PlayerAnimState.Back: mCamera.transform.parent = null; myTransform.rotation = Quaternion.Euler(myTransform.rotation.x, mCamera.transform.localEulerAngles.y - 180, myTransform.rotation.z); mCamera.transform.parent = myTransform; break; case PlayerAnimState.Left: mCamera.transform.parent = null; myTransform.rotation = Quaternion.Euler(myTransform.rotation.x, mCamera.transform.localEulerAngles.y - 90, myTransform.rotation.z); mCamera.transform.parent = myTransform; break; case PlayerAnimState.Right: mCamera.transform.parent = null; myTransform.rotation = Quaternion.Euler(myTransform.rotation.x, mCamera.transform.localEulerAngles.y + 90, myTransform.rotation.z); mCamera.transform.parent = myTransform; break; case PlayerAnimState.ForwardDiagonalLeft: mCamera.transform.parent = null; myTransform.rotation = Quaternion.Euler(myTransform.rotation.x, mCamera.transform.localEulerAngles.y - 45, myTransform.rotation.z); mCamera.transform.parent = myTransform; break; case PlayerAnimState.ForwardDiagonalRight: mCamera.transform.parent = null; myTransform.rotation = Quaternion.Euler(myTransform.rotation.x, mCamera.transform.localEulerAngles.y + 45, myTransform.rotation.z); mCamera.transform.parent = myTransform; break; case PlayerAnimState.BackDiagonalLeft: mCamera.transform.parent = null; myTransform.rotation = Quaternion.Euler(myTransform.rotation.x, mCamera.transform.localEulerAngles.y - (180 - 45), myTransform.rotation.z); mCamera.transform.parent = myTransform; break; case PlayerAnimState.BackDiagonalRight: mCamera.transform.parent = null; myTransform.rotation = Quaternion.Euler(myTransform.rotation.x, mCamera.transform.localEulerAngles.y + (180 - 45), myTransform.rotation.z); mCamera.transform.parent = myTransform; break; case PlayerAnimState.AttackingStill: break; case PlayerAnimState.AttackingMoving: break; } }
// Update is called once per frame protected override void Update() { if (health <= 0) { Kill(); } //Regen HP if (timeGotHit > 0.0f) { if (Time.time - timeGotHit >= regenCD) { if (tempHealth < health) { tempHealth += (regenRate * Time.deltaTime); } if (tempHealth > health) { timeGotHit = 0.0f; tempHealth = health; } } } //Jessica is Sexy! (.)(.) <3 //Determine the updates on the player depending on player state //The player contorller handles user input based on these states. //We must do all the rigidbody movement from outside forces, animations and soundeffects which belong to the character. Temporary sprites involving player interaction will be handled outside this class playerAnimState = PlayerAnimState.NONE; //making a temporary enum here so we dont call change animation a dozen times switch (playerState) { case (PlayerState.GROUNDED): SetGravity(1.0f); //set drag for ground if (!knockedback) { _rbody.drag = dragGround; } else { _rbody.drag = dragSliding; } transform.rotation = new Quaternion(0.0f, 0.0f, 0.0f, 0.0f); if (playerAnimState == PlayerAnimState.FALL) { playerAnimState = PlayerAnimState.CROUCH; break; } //We are grounded so check for movement to determine if idle or running //If the x velocity is greater then 0.1 or less then -0.1 we are moving if (_rbody.velocity.x > 0.1 || _rbody.velocity.x < -0.1) { float h = Input.GetAxis(horizontalAxis); if ((h > 0.01f && _rbody.velocity.x > 0.1f) || (h < -0.01f && _rbody.velocity.x < -0.1f)) { playerAnimState = PlayerAnimState.RUN; kickedUpDust = false; } else { if (!kickedUpDust && playerAnimState != PlayerAnimState.SLIDE) { kickedUpDust = true; TempSpriteManager.GetInstance().PlayAnimation("Small_Dust_02", (Vector2)transform.position + new Vector2(Mathf.Sign(transform.localScale.x) * 1, 0), new Vector2(0.5f * Mathf.Sign(transform.localScale.x), 0.5f), new Vector2(0.02f * Mathf.Sign(transform.localScale.x), 0), 0.0f, "Background2", -1, 0.3f); } playerAnimState = PlayerAnimState.SLIDE; } } //We dont have to set idle because we did at the beginning. If the player isnt running and is in this state nothing else will be called because of the break. //Jittering in the transition from idle and run can be solved using the horizontal axis, however the playercontroller class will have to be altered slightly else { playerAnimState = PlayerAnimState.IDLE; } break; case (PlayerState.AIRBORNE): if (!knockedback) { SetGravity(1.0f); } //PROBABLY WANNA CHANGE THIS AT SOME POINT TO BETTER CODE.... WAY TO LAZY RIGHT NOW if (attacking) { _rbody.drag = dragGround; } else { _rbody.drag = dragAir; } //playerAnimState = PlayerAnimState.FALL; if (Mathf.Abs(_rbody.velocity.y) <= 5.5f) { playerAnimState = PlayerAnimState.APEX; } else if (_rbody.velocity.y > 0.1f) { playerAnimState = PlayerAnimState.JUMP; } else { playerAnimState = PlayerAnimState.FALL; } break; case (PlayerState.CLIMBING): _rbody.drag = dragClimbing; //If we are climbing then set the animation to climbing. In this player state there isnt any other animation that could occur. transform.rotation = new Quaternion(0.0f, 0.0f, 0.0f, 0.0f); SetGravity(0.33f); playerAnimState = PlayerAnimState.CLIMB; break; case (PlayerState.HANGING): //If we are hanging then set the animation to hang. In this player state there isnt any other animation that could occur. playerAnimState = PlayerAnimState.HANG; SetGravity(0.0f); _rbody.velocity = Vector2.zero; break; case (PlayerState.DEAD): timeSinceDied += Time.deltaTime; if (timeSinceDied >= respawnCD) { timeSinceDied = 0.0f; Respawn(GameObject.Find("Main Camera").transform.position); } break; } //Crouching? if (crouching) { playerAnimState = PlayerAnimState.CROUCH; } //If we have changed animations update the animator if (animator.animState != (int)playerAnimState) { animator.ChangeAnimation((int)playerAnimState); } //animator is updated in the base class base.Update(); if (playerState != PlayerState.DEAD) { controller.Update(); } }
void changeState( PlayerAnimState new_state ) { switch( new_state ) { case PlayerAnimState.RUNNING: m_animFrame = 0; m_animFrameTime = 1.0f/m_realAnimRunSpeed; break; case PlayerAnimState.JUMP_TAKE_OFF: if( RL.m_Prototype.m_PlayerType == PrototypeConfiguration.PlayerTypes.BALDY ) m_animFrame = 16; else m_animFrame = 11; m_animFrameTime = 1.0f/m_animFrameSpeed; RL.m_SoundController.Play("Elvis_Jump"); RL.m_MusicPlayer.Fade("VLV_BongoLoop", 0.0f, 1.0f, 0.1f, false); break; case PlayerAnimState.JUMP_SAILING: if( RL.m_Prototype.m_PlayerType == PrototypeConfiguration.PlayerTypes.BALDY ) m_animFrame = 18; else { m_animFrameTime = 1.0f/m_animFrameSpeed; m_animFrame = 12; } break; case PlayerAnimState.JUMP_LANDING: if( RL.m_Prototype.m_PlayerType == PrototypeConfiguration.PlayerTypes.BALDY ) m_animFrame = 19; else m_animFrame = 16; m_animFrameTime = 2.0f/m_animFrameSpeed; break; case PlayerAnimState.JUMP_LANDED: if( RL.m_Prototype.m_PlayerType == PrototypeConfiguration.PlayerTypes.BALDY ) m_animFrame = 19; else m_animFrame = 16; m_animFrameTime = 0.2f/m_animFrameSpeed; m_realAnimRunSpeed = m_animRunSpeed + 15.0f; RL.m_MusicPlayer.Fade("VLV_BongoLoop", 1.0f, 1.0f, 0.1f, false); break; case PlayerAnimState.COLLIDE_WALL: if( m_animState != PlayerAnimState.COLLIDE_WALL ) { if( RL.m_Prototype.m_PlayerType == PrototypeConfiguration.PlayerTypes.ELVIS ) { m_animFrame = 17; m_animFrameTime = 0.25f; } } break; } m_animState = new_state; }