Skip to content

Instantly share code, notes, and snippets.

@markdekuijer
Last active November 8, 2018 21:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save markdekuijer/574d9c394cbaf0adb072ae7a78b24d63 to your computer and use it in GitHub Desktop.
Save markdekuijer/574d9c394cbaf0adb072ae7a78b24d63 to your computer and use it in GitHub Desktop.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.AI;
using System;
public class CharacterBehaviour : MonoBehaviour
{
[Header("On Startup")]
[SerializeField] private Camera cam;
[SerializeField] private NavMeshAgent agent;
[SerializeField] private CharacterAttack characterAttack;
public AnimatorHook anim;
public CharacterMovement characterMovement;
[Header("States")]
[SerializeField] private bool isStatic;
[SerializeField] private bool isSlowed;
[SerializeField] private bool isDead;
[SerializeField] private bool isApproachingEnemy;
public bool isMoving;
public bool isAttacking;
[Header("Variables")]
[SerializeField] private bool isRangedCharacter;
[SerializeField] private bool hasGoal;
[SerializeField] private GameObject interactionGoal;
[SerializeField] private float interactionthreshold = 0.1f;
//this is being static so the item drops wouldn't need alot of refferences
//TODO clean this up if I continue this project
public static CharStats characterStats;
public static WeaponStats currentWeaponStats;
public static ArmoreStats currentArmoreStats;
public static Vector3 currentPosition;
public WeaponStats stats;
public int currentLevel;
private int exp;
private void Awake()
{
//temporarily hardcoded duo to only having made 1 character
//and no option menu except for sound
//all duo to time limitations and long term motivation on a solo project
Application.targetFrameRate = 60;
string statsString = "ArcherLevel";
statsString += currentLevel.ToString();
characterStats = Resources.Load<CharStats>("ScriptableStuff/CharacterStats/" + statsString);
currentWeaponStats = Resources.Load<WeaponStats>("ScriptableStuff/Bows/Lvl1_BowCommon_V1");
characterMovement.Init(agent, cam, this);
characterAttack.Init(this);
}
void Update ()
{
GetBaseInput();
HandleGoals();
HandleAnimations();
characterAttack.Tick();
}
#region Input
public void GetBaseInput()
{
if (GameUIManager.Paused)
return;
currentPosition = transform.position;
RaycastHit hit;
Ray ray = cam.ScreenPointToRay(Input.mousePosition);
if (Physics.Raycast(ray, out hit))
{
if (Input.GetMouseButtonDown(0))
HandleDirectInput(hit);
if (Input.GetMouseButtonDown(1))
HandleSecondaryInput(hit);
HandleIndirectInput();
}
if (isApproachingEnemy)
{
HandleTargets();
}
GetAbbilityInput(hit);
}
//the possibility of abbilitys being NULL is here
//this was made on purpose so it was easier to make
//abbilitys in a rapid speed and test them without
//selecting multiple(or a pre-set)
public void GetAbbilityInput(RaycastHit hit)
{
if (Input.GetKeyDown(KeyCode.Alpha1))
{
if(characterAttack.abbilityAttacks[0] != null)
characterAttack.abbilityAttacks[0].Init(hit.point);
}
if (Input.GetKeyDown(KeyCode.Alpha2))
{
if (characterAttack.abbilityAttacks[1] != null)
characterAttack.abbilityAttacks[1].Init(hit.point);
}
if (Input.GetKeyDown(KeyCode.Alpha3))
{
if (characterAttack.abbilityAttacks[2] != null)
characterAttack.abbilityAttacks[2].Init(hit.point);
}
if (Input.GetKeyDown(KeyCode.Alpha4))
{
if (characterAttack.abbilityAttacks[3] != null)
characterAttack.abbilityAttacks[3].Init(hit.point);
}
for (int i = 0; i < characterAttack.abbilityAttacks.Length; i++)
{
if (characterAttack.abbilityAttacks[i] == null)
continue;
characterAttack.abbilityAttacks[i].Tick();
}
}
public void HandleDirectInput(RaycastHit hit)
{
if (hit.transform.gameObject.CompareTag("Ally"))
{
hasGoal = true;
interactionGoal = hit.transform.gameObject;
characterMovement.SetMoveTarget(hit.point);
}
else if(Input.GetKey(KeyCode.LeftShift) || hit.transform.gameObject.CompareTag("Enemy"))
{
hasGoal = false;
interactionGoal = null;
if (isRangedCharacter)
{
if (Input.GetKey(KeyCode.LeftShift))
{
//easy fix to get certain positions but not the cleanest
//(fix) get a list of needed (vector3)positions instead
GameObject g = ObjectPooler.SharedInstance.GetPooledObject(4);
g.transform.position = hit.point;
characterAttack.target = g;
g.SetActive(false);
}
else
{
characterAttack.target = hit.transform.gameObject;
}
characterAttack.InitAttack(0);
}
else
{
isApproachingEnemy = true;
}
}
else if(hit.transform.gameObject.CompareTag("IgnoreMovement"))
{
return;
}
else
{
characterMovement.SetMoveTarget(hit.point);
isApproachingEnemy = false;
hasGoal = false;
interactionGoal = null;
}
}
public void HandleSecondaryInput(RaycastHit hit)
{
hasGoal = false;
interactionGoal = null;
if (isRangedCharacter)
{
//easy fix to get certain positions but not the cleanest
//(fix) get a list of needed (vector3)positions instead
GameObject g = ObjectPooler.SharedInstance.GetPooledObject(4);
g.transform.position = hit.point;
characterAttack.target = g;
g.SetActive(false);
characterAttack.InitAttack(1);
}
}
public void HandleIndirectInput()
{
//displayGraphics
}
#endregion
#region Goals
//Goals such as autowalk->talk to person
//realtime cutscene or other step-based events
public void HandleGoals()
{
if (hasGoal)
{
if (interactionGoal != null)
{
HandleInteractionGoal();
return;
}
Debug.LogError("No Remaining Goal. have to fix goals somewhere");
hasGoal = false;
}
}
public void HandleTargets()
{
characterAttack.HandleAttackTarget();
return;
}
public void HandleInteractionGoal()
{
if(Vector3.Distance(transform.position, interactionGoal.transform.position) < interactionthreshold)
{
print(interactionGoal.name + " " + Time.time);
StopMovement();
hasGoal = false;
interactionGoal = null;
//activateGoalPurpose (missions,dialog,enteringRooms,ect)
}
}
public void StopMovement()
{
characterMovement.SetMoveTarget(transform.position);
}
#endregion
public void HandleAnimations()
{
if (agent.velocity.magnitude <= 3f)
isMoving = false;
else
isMoving = true;
}
//Calculate the damage based of weaponStats
//I've tried to do this with more stat values
//but game ballance didn't come out great so I kept it simpel
public float CalculateAADamage(float dmg)
{
float returnDmg = 0;
returnDmg = (dmg * currentWeaponStats.damage);
return returnDmg;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment