Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- public class Hero : Actor
- {
- public Walker walker;
- public bool isAutoPiloting;
- public bool controllable = true;
- public float walkSpeed = 2;
- public float runSpeed = 4;
- bool isRunning;
- bool isMoving;
- float lastWalk;
- public bool canRun = true;
- float tapAgainToRunTime = 0.2f;
- Vector3 lastWalkVector;
- Vector3 currentDir;
- bool isFacingLeft;
- bool isJumpLandAnim;
- bool isJumpingAnim;
- public InputHandler input;
- public float jumpForce = 1750;
- private float jumpDuration = 0.2f;
- private float lastJumpTime;
- bool isAttackingAnim;
- float lastAttackTime;
- float attackLimit = 0.14f;
- public bool canJumpAttack = true;
- private int currentAttackChain = 1;
- public int evaluatedAttackChain = 0;
- public AttackData jumpAttack;
- bool isHurtAnim;
- // New jump mechanics
- int jumpPower = 15;
- float fallMultiplier = -10;
- float jumpMultiplier = 30.0f;
- Vector3 vecGravity = new Vector3(0, -1.0F, 0);
- //
- public override void Update()
- {
- base.Update();
- if (!isAlive)
- {
- return;
- }
- isAttackingAnim = baseAnim.GetCurrentAnimatorStateInfo(0).IsName("attack1") || baseAnim.GetCurrentAnimatorStateInfo(0).IsName("jump_attack");
- isJumpLandAnim = baseAnim.GetCurrentAnimatorStateInfo(0).IsName("jump_land");
- isJumpingAnim = baseAnim.GetCurrentAnimatorStateInfo(0).IsName("jump_rise") || baseAnim.GetCurrentAnimatorStateInfo(0).IsName("jump_fall");
- isHurtAnim = baseAnim.GetCurrentAnimatorStateInfo(0).IsName("hurt");
- if (isAutoPiloting)
- {
- return;
- }
- float h = input.GetHorizontalAxis();
- float v = input.GetVerticalAxis();
- bool jump = input.GetJumpButtonDown();
- bool attack = input.GetAttackButtonDown();
- currentDir = new Vector3(h, 0, v);
- currentDir.Normalize();
- if (!isAttackingAnim)
- {
- if ((v == 0 && h == 0))
- {
- Stop();
- isMoving = false;
- }
- else if (!isMoving && (v != 0 || h != 0))
- {
- isMoving = true;
- float dotProduct = Vector3.Dot(currentDir, lastWalkVector);
- if (canRun && Time.time < lastWalk + tapAgainToRunTime && dotProduct > 0)
- {
- Run();
- }
- else
- {
- Walk();
- if (h != 0)
- {
- lastWalkVector = currentDir;
- lastWalk = Time.time;
- }
- }
- }
- }
- // Jump mechanics
- if (jump && !isKnockedOut && !isJumpLandAnim && !isAttackingAnim && !isHurtAnim && (isGrounded || (isJumpingAnim && Time.time < lastJumpTime + jumpDuration)))
- {
- if (!isJumpingAnim && !isHurtAnim)
- {
- baseAnim.SetTrigger("Jump");
- lastJumpTime = Time.time;
- body.velocity = new Vector3(speed * currentDir.x, jumpPower, 0);
- //body.velocity = new Vector3(speed, jumpPower, currentDir.z);
- }
- }
- if (body.velocity.y > 0 && isJumpingAnim)
- {
- body.velocity += vecGravity * jumpMultiplier * Time.deltaTime;
- }
- if (body.velocity.y < 0)
- {
- body.velocity -= vecGravity * fallMultiplier * Time.deltaTime;
- }
- if (attack && Time.time >= lastAttackTime + attackLimit && !isKnockedOut)
- {
- lastAttackTime = Time.time;
- Attack();
- }
- }
- public void Stop()
- {
- speed = 0;
- baseAnim.SetFloat("Speed", speed);
- isRunning = false;
- baseAnim.SetBool("IsRunning", isRunning);
- }
- public void Walk()
- {
- speed = walkSpeed;
- baseAnim.SetFloat("Speed", speed);
- isRunning = false;
- baseAnim.SetBool("IsRunning", isRunning);
- }
- void FixedUpdate()
- {
- if (!isAlive)
- {
- return;
- }
- if (!isAutoPiloting)
- {
- Vector3 moveVector = currentDir * speed;
- if (isGrounded && !isAttackingAnim && !isJumpLandAnim && !isKnockedOut && !isHurtAnim)
- {
- body.MovePosition(transform.position + moveVector * Time.fixedDeltaTime);
- baseAnim.SetFloat("Speed", moveVector.magnitude);
- }
- if (moveVector != Vector3.zero && isGrounded && !isKnockedOut && !isAttackingAnim)
- {
- if (moveVector.x != 0)
- {
- isFacingLeft = moveVector.x < 0;
- }
- FlipSprite(isFacingLeft);
- }
- }
- }
- public void Run()
- {
- speed = runSpeed;
- isRunning = true;
- baseAnim.SetBool("IsRunning", isRunning);
- baseAnim.SetFloat("Speed", speed);
- }
- protected override void DidLand()
- {
- base.DidLand();
- //Walk();
- }
- public override void Attack()
- {
- if (!isGrounded)
- {
- if (isJumpingAnim && canJumpAttack)
- {
- canJumpAttack = false;
- currentAttackChain = 1;
- evaluatedAttackChain = 0;
- baseAnim.SetInteger("EvaluatedChain", evaluatedAttackChain);
- baseAnim.SetInteger("CurrentChain", currentAttackChain);
- body.velocity = Vector3.zero;
- body.useGravity = false;
- }
- }
- else
- {
- currentAttackChain = 1;
- evaluatedAttackChain = 0;
- baseAnim.SetInteger("EvaluatedChain", evaluatedAttackChain);
- baseAnim.SetInteger("CurrentChain", currentAttackChain);
- }
- }
- public void DidChain(int chain)
- {
- baseAnim.SetInteger("EvaluatedChain", 1);
- }
- public void AnimateTo(Vector3 position, bool shouldRun, Action callback)
- {
- if (shouldRun)
- {
- Run();
- }
- else
- {
- Walk();
- }
- walker.MoveTo(position, callback);
- }
- public void UseAutopilot(bool useAutopilot)
- {
- isAutoPiloting = useAutopilot;
- walker.enabled = useAutopilot;
- }
- protected override void OnCollisionEnter(Collision collision)
- {
- base.OnCollisionEnter(collision);
- if (collision.collider.name == "Floor")
- {
- canJumpAttack = true;
- }
- }
- public void DidJumpAttack()
- {
- body.useGravity = true;
- }
- private void AnalyzeSpecialAttack(AttackData attackData, Actor actor, Vector3 hitPoint, Vector3 hitVector)
- {
- actor.EvaluateAttackData(attackData, hitVector, hitPoint);
- }
- protected override void HitActor(Actor actor, Vector3 hitPoint, Vector3 hitVector)
- {
- if (baseAnim.GetCurrentAnimatorStateInfo(0).IsName("attack1"))
- {
- base.HitActor(actor, hitPoint, hitVector);
- }
- else if (baseAnim.GetCurrentAnimatorStateInfo(0).IsName("jump_attack"))
- {
- AnalyzeSpecialAttack(jumpAttack, actor, hitPoint, hitVector);
- }
- }
- public override void TakeDamage(float value, Vector3 hitVector, bool knockdown = false)
- {
- if (!isGrounded)
- {
- knockdown = true;
- }
- base.TakeDamage(value, hitVector, knockdown);
- }
- public override bool CanWalk()
- {
- return (isGrounded && !isAttackingAnim && !isJumpLandAnim && !isKnockedOut && !isHurtAnim);
- }
- protected override IEnumerator KnockdownRoutine()
- {
- body.useGravity = true;
- return base.KnockdownRoutine();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment