Skip to content

Commit 038381b

Browse files
committed
First Major Release
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.
1 parent 35ad660 commit 038381b

File tree

16 files changed

+517
-162
lines changed

16 files changed

+517
-162
lines changed

Creatures/Creature.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ protected float JmpMoveSpd
6565
/// <summary>
6666
/// Base health points. Calculated using minimum HP and stat points.
6767
/// </summary>
68-
private int _baseMaxHp = 10;
68+
[SerializeField] private int _baseMaxHp = 40;
6969
/// <summary>
7070
/// Maximum health. Calculated using base HP and 'effect modifiers' (e.g. armor).
7171
/// </summary>

Creatures/CreatureAnimManager.cs

Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
using System.Collections;
2+
using System.Collections.Generic;
3+
using UnityEngine;
4+
5+
public class CreatureAnimManager : MonoBehaviour
6+
{
7+
protected CharacterController cc;
8+
protected Animator anim;
9+
protected float defaultColliderHeight;
10+
11+
public int StunLevel { get; set; } = 0;
12+
public bool IsStunned { get; protected set; } = false;
13+
public bool DoingAttk { get; protected set; }
14+
public bool InAttkState { get; protected set; } = false;
15+
public bool DoingOffhandAttack { protected set; get; }
16+
/// <summary>
17+
/// Penalty to prevent/limit rotation when committed to an attack.
18+
/// </summary>
19+
public float AttkRotationPenalty { get; protected set; }
20+
/// <summary>
21+
/// Percentage of attack damage done based on attack animation curves.
22+
/// </summary>
23+
public float AttkDmgPercentage => (int)anim.GetFloat("AttkDmg");
24+
/// <summary>
25+
/// Bonus damage that can be given by attack animations.
26+
/// </summary>
27+
public float ComboBonusDmg => (int)anim.GetFloat("ComboBonusDmg");
28+
/// <summary>
29+
/// Last AttkType attack passed to animator that wasn't none.
30+
/// </summary>
31+
public AttkType CurValidAttk { get; protected set; }
32+
33+
34+
35+
public void DoStun(Transform stunSrc, Transform stunTarget, int stunStrength1to3)
36+
{
37+
Stun stun = new Stun(stunSrc, stunTarget, Mathf.Clamp(stunStrength1to3, 1, 3));
38+
StunLevel = stun.StunLev;
39+
Debug.Log(stun.StunMoveDir);
40+
anim.SetInteger("StunDir", (int)stun.StunMoveDir);
41+
}
42+
43+
44+
protected struct Stun
45+
{
46+
public int StunLev { get; private set; }
47+
public Direction StunMoveDir { get; private set; }
48+
49+
50+
public Stun(Transform stunSrc, Transform stunTarget, int stunLevel_1to3) : this()
51+
{
52+
StunLev = stunLevel_1to3;
53+
StunMoveDir = CalcStunMoveDir(stunSrc, stunTarget);
54+
}
55+
56+
private Direction CalcStunMoveDir(Transform src, Transform target)
57+
{
58+
Direction direction = Direction.None;
59+
Vector3 srcToTarget = (target.position - src.position).normalized;
60+
61+
62+
Direction vertDir = GetVertDir(srcToTarget, target, .1f);
63+
Direction horzDir = GetHorzDir(srcToTarget, target, .2f);
64+
65+
direction = (horzDir > vertDir) ? horzDir : vertDir;
66+
67+
if(direction == Direction.None)
68+
direction += (int)GetVertDir(srcToTarget, target, 0f);
69+
70+
Debug.Log(direction);
71+
return direction;
72+
}
73+
74+
private Direction GetVertDir(Vector3 srcToTarget, Transform target, float threshold)
75+
{
76+
if (Vector3.Dot(srcToTarget, target.forward) < -threshold)
77+
return Direction.Fwd;
78+
else if (Vector3.Dot(srcToTarget, target.forward) > threshold)
79+
return Direction.Back;
80+
return Direction.None;
81+
}
82+
83+
private Direction GetHorzDir(Vector3 srcToTarget, Transform target, float threshold)
84+
{
85+
if (Vector3.Dot(srcToTarget, target.right) < -threshold)
86+
return Direction.Right;
87+
else if (Vector3.Dot(srcToTarget, target.right) > threshold)
88+
return Direction.Left;
89+
return Direction.None;
90+
}
91+
}
92+
93+
94+
95+
96+
97+
private void Awake()
98+
{
99+
cc = GetComponent<CharacterController>();
100+
anim = GetComponent<Animator>();
101+
defaultColliderHeight = cc.height;
102+
}
103+
}

Creatures/CreatureModifyableProperties.cs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,21 @@
77
/// </summary>
88
public class CreatureModifyableProperties : MonoBehaviour
99
{
10+
private CreatureAnimManager anim;
1011
public Creature Creature { get; private set; }
1112
public Health Health { get; private set; }
1213
private void Awake()
1314
{
1415
Creature = GetComponent<Creature>();
15-
Health = GetComponent<Health>();
16+
Health = GetComponent<Health>();
17+
anim = GetComponent<CreatureAnimManager>();
18+
}
19+
20+
public bool InStunState => anim.IsStunned;
21+
22+
public void SetAnimStunned(Transform stunSrc, int StunStrength1to3)
23+
{
24+
anim.DoStun(stunSrc, transform, StunStrength1to3);
1625
}
1726

1827
/// <summary>

Creatures/Health.cs

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,21 +5,31 @@
55
public class Health : MonoBehaviour
66
{
77
private Creature self;
8+
private HumanoidAnim anim;
89
public int CurHP { get; private set; }
910

1011
void Start()
1112
{
1213
self = GetComponent<Creature>();
14+
anim = GetComponent<HumanoidAnim>();
1315
CurHP = self.MaxHealth;
1416
}
1517

1618

1719
public void TakeDamage(int amount)
18-
{
20+
{
21+
if (amount < 0)
22+
{
23+
Debug.LogError("ERROR - Health damage amount was < 0. All damage must be positive to be applied.");
24+
return;
25+
}
1926
if(amount > 0)
20-
Debug.Log(gameObject.name + " was damaged! (" + amount + "). Remainder " + (CurHP - amount) + ".");
21-
CurHP -= Mathf.Abs(amount);
22-
if (CurHP <= 0) Destroy(gameObject);
27+
{
28+
Debug.Log(gameObject.name + " was damaged! (" + amount + "). Remainder " + (CurHP - amount) + ".");
29+
}
30+
31+
CurHP -= amount;
32+
if (CurHP <= 0) Destroy(gameObject);
2333
}
2434

2535

Creatures/Humanoid.cs

Lines changed: 39 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,16 +36,51 @@ private float JmpAmount
3636
public bool IsAttacking { get; private set; }
3737

3838
void Update()
39-
{
40-
39+
{
4140
SetMaxSpdTerrainMod();
4241

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

4544
MoveHumanoid();
4645
}
4746

47+
48+
49+
public void PreventClipping(string otherMaskLayer, int numChecks)
50+
{
51+
RaycastHit hit;
52+
LayerMask otherLayer = LayerMask.NameToLayer(otherMaskLayer);
53+
54+
//Bottom of controller. Slightly above ground so it doesn't bump into slanted platforms. (Adjust to your needs)
55+
Vector3 p1 = transform.position + Vector3.up * 0.25f;
56+
//Top of controller
57+
Vector3 p2 = p1 + Vector3.up * cControl.height;
58+
59+
//Check around the character in a 360, 10 times (increase if more accuracy is needed)
60+
for (int i = 0; i < numChecks; i += 36)
61+
{
62+
//Check if anything with the platform layer touches this object
63+
if (Physics.CapsuleCast(p1, p2, 0, new Vector3(Mathf.Cos(i), 0, Mathf.Sin(i)), out hit, distance, 1 << otherLayer))
64+
{
65+
//If the object is touched by a platform, move the object away from it
66+
cControl.Move(hit.normal * (distance - hit.distance));
67+
}
68+
}
69+
70+
//[Optional] Check the players feet and push them up if something clips through their feet.
71+
//(Useful for vertical moving platforms)
72+
if (Physics.Raycast(transform.position + Vector3.up, -Vector3.up, out hit, 1, 1 << otherLayer))
73+
{
74+
cControl.Move(Vector3.up * (1 - hit.distance));
75+
}
76+
}
4877

78+
//Distance is slightly larger than the
79+
float distance => cControl.radius + 0.2f;
80+
81+
//First add a Layer name to all platforms (I used MovingPlatform)
82+
//Now this script won't run on regular objects, only platforms.
83+
// => gameObject.tag == "Enemy" ? LayerMask.NameToLayer("Player") : LayerMask.NameToLayer("Enemy");
4984

5085

5186
public void SetMoveState(moveEnum moveState)
@@ -157,9 +192,9 @@ private void SetCurSpdAccel()
157192

158193

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

165200

0 commit comments

Comments
 (0)