Skip to content

Commit

Permalink
First Major Release
Browse files Browse the repository at this point in the history
Advanced weapons and damage system,  fixed attack manager, improved animator and more.

In this release is all the code for an advanced humanoid player and a basic humanoid enemy. Both can harm, stun and kill each other. The system also supports basic allied AI.
  • Loading branch information
TCPHijinks committed Jan 23, 2020
1 parent 35ad660 commit 038381b
Show file tree
Hide file tree
Showing 16 changed files with 517 additions and 162 deletions.
2 changes: 1 addition & 1 deletion Creatures/Creature.cs
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ protected float JmpMoveSpd
/// <summary>
/// Base health points. Calculated using minimum HP and stat points.
/// </summary>
private int _baseMaxHp = 10;
[SerializeField] private int _baseMaxHp = 40;
/// <summary>
/// Maximum health. Calculated using base HP and 'effect modifiers' (e.g. armor).
/// </summary>
Expand Down
103 changes: 103 additions & 0 deletions Creatures/CreatureAnimManager.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class CreatureAnimManager : MonoBehaviour
{
protected CharacterController cc;
protected Animator anim;
protected float defaultColliderHeight;

public int StunLevel { get; set; } = 0;
public bool IsStunned { get; protected set; } = false;
public bool DoingAttk { get; protected set; }
public bool InAttkState { get; protected set; } = false;
public bool DoingOffhandAttack { protected set; get; }
/// <summary>
/// Penalty to prevent/limit rotation when committed to an attack.
/// </summary>
public float AttkRotationPenalty { get; protected set; }
/// <summary>
/// Percentage of attack damage done based on attack animation curves.
/// </summary>
public float AttkDmgPercentage => (int)anim.GetFloat("AttkDmg");
/// <summary>
/// Bonus damage that can be given by attack animations.
/// </summary>
public float ComboBonusDmg => (int)anim.GetFloat("ComboBonusDmg");
/// <summary>
/// Last AttkType attack passed to animator that wasn't none.
/// </summary>
public AttkType CurValidAttk { get; protected set; }



public void DoStun(Transform stunSrc, Transform stunTarget, int stunStrength1to3)
{
Stun stun = new Stun(stunSrc, stunTarget, Mathf.Clamp(stunStrength1to3, 1, 3));
StunLevel = stun.StunLev;
Debug.Log(stun.StunMoveDir);
anim.SetInteger("StunDir", (int)stun.StunMoveDir);
}


protected struct Stun
{
public int StunLev { get; private set; }
public Direction StunMoveDir { get; private set; }


public Stun(Transform stunSrc, Transform stunTarget, int stunLevel_1to3) : this()
{
StunLev = stunLevel_1to3;
StunMoveDir = CalcStunMoveDir(stunSrc, stunTarget);
}

private Direction CalcStunMoveDir(Transform src, Transform target)
{
Direction direction = Direction.None;
Vector3 srcToTarget = (target.position - src.position).normalized;


Direction vertDir = GetVertDir(srcToTarget, target, .1f);
Direction horzDir = GetHorzDir(srcToTarget, target, .2f);

direction = (horzDir > vertDir) ? horzDir : vertDir;

if(direction == Direction.None)
direction += (int)GetVertDir(srcToTarget, target, 0f);

Debug.Log(direction);
return direction;
}

private Direction GetVertDir(Vector3 srcToTarget, Transform target, float threshold)
{
if (Vector3.Dot(srcToTarget, target.forward) < -threshold)
return Direction.Fwd;
else if (Vector3.Dot(srcToTarget, target.forward) > threshold)
return Direction.Back;
return Direction.None;
}

private Direction GetHorzDir(Vector3 srcToTarget, Transform target, float threshold)
{
if (Vector3.Dot(srcToTarget, target.right) < -threshold)
return Direction.Right;
else if (Vector3.Dot(srcToTarget, target.right) > threshold)
return Direction.Left;
return Direction.None;
}
}





private void Awake()
{
cc = GetComponent<CharacterController>();
anim = GetComponent<Animator>();
defaultColliderHeight = cc.height;
}
}
11 changes: 10 additions & 1 deletion Creatures/CreatureModifyableProperties.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,21 @@
/// </summary>
public class CreatureModifyableProperties : MonoBehaviour
{
private CreatureAnimManager anim;
public Creature Creature { get; private set; }
public Health Health { get; private set; }
private void Awake()
{
Creature = GetComponent<Creature>();
Health = GetComponent<Health>();
Health = GetComponent<Health>();
anim = GetComponent<CreatureAnimManager>();
}

public bool InStunState => anim.IsStunned;

public void SetAnimStunned(Transform stunSrc, int StunStrength1to3)
{
anim.DoStun(stunSrc, transform, StunStrength1to3);
}

/// <summary>
Expand Down
18 changes: 14 additions & 4 deletions Creatures/Health.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,31 @@
public class Health : MonoBehaviour
{
private Creature self;
private HumanoidAnim anim;
public int CurHP { get; private set; }

void Start()
{
self = GetComponent<Creature>();
anim = GetComponent<HumanoidAnim>();
CurHP = self.MaxHealth;
}


public void TakeDamage(int amount)
{
{
if (amount < 0)
{
Debug.LogError("ERROR - Health damage amount was < 0. All damage must be positive to be applied.");
return;
}
if(amount > 0)
Debug.Log(gameObject.name + " was damaged! (" + amount + "). Remainder " + (CurHP - amount) + ".");
CurHP -= Mathf.Abs(amount);
if (CurHP <= 0) Destroy(gameObject);
{
Debug.Log(gameObject.name + " was damaged! (" + amount + "). Remainder " + (CurHP - amount) + ".");
}

CurHP -= amount;
if (CurHP <= 0) Destroy(gameObject);
}


Expand Down
43 changes: 39 additions & 4 deletions Creatures/Humanoid.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,16 +36,51 @@ private float JmpAmount
public bool IsAttacking { get; private set; }

void Update()
{

{
SetMaxSpdTerrainMod();

SetCurSpdAccel(); // Calc and apply cur accel.

MoveHumanoid();
}



public void PreventClipping(string otherMaskLayer, int numChecks)
{
RaycastHit hit;
LayerMask otherLayer = LayerMask.NameToLayer(otherMaskLayer);

//Bottom of controller. Slightly above ground so it doesn't bump into slanted platforms. (Adjust to your needs)
Vector3 p1 = transform.position + Vector3.up * 0.25f;
//Top of controller
Vector3 p2 = p1 + Vector3.up * cControl.height;

//Check around the character in a 360, 10 times (increase if more accuracy is needed)
for (int i = 0; i < numChecks; i += 36)
{
//Check if anything with the platform layer touches this object
if (Physics.CapsuleCast(p1, p2, 0, new Vector3(Mathf.Cos(i), 0, Mathf.Sin(i)), out hit, distance, 1 << otherLayer))
{
//If the object is touched by a platform, move the object away from it
cControl.Move(hit.normal * (distance - hit.distance));
}
}

//[Optional] Check the players feet and push them up if something clips through their feet.
//(Useful for vertical moving platforms)
if (Physics.Raycast(transform.position + Vector3.up, -Vector3.up, out hit, 1, 1 << otherLayer))
{
cControl.Move(Vector3.up * (1 - hit.distance));
}
}

//Distance is slightly larger than the
float distance => cControl.radius + 0.2f;

//First add a Layer name to all platforms (I used MovingPlatform)
//Now this script won't run on regular objects, only platforms.
// => gameObject.tag == "Enemy" ? LayerMask.NameToLayer("Player") : LayerMask.NameToLayer("Enemy");


public void SetMoveState(moveEnum moveState)
Expand Down Expand Up @@ -157,9 +192,9 @@ private void SetCurSpdAccel()


// Accel if cur spd slower than max, decel if too fast.
if (Grounded && CurSpeed <= DynamicMaxSpd)
if (Grounded && CurSpeed <= DynamicMaxSpd && BaseMoveStateMaxSpd != (int)moveEnum.Idle)
CurSpeed += moveAccel * Time.deltaTime;
else if (CurSpeed > DynamicMaxSpd)
else if (CurSpeed > DynamicMaxSpd || BaseMoveStateMaxSpd == (int)moveEnum.Idle)
CurSpeed -= (moveAccel * 1.25f) * Time.deltaTime;


Expand Down
Loading

0 comments on commit 038381b

Please sign in to comment.