Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
66 changes: 62 additions & 4 deletions Assets/Fonts/Kenney Future SDF.asset

Large diffs are not rendered by default.

42 changes: 33 additions & 9 deletions Assets/Scenes/GameScene.unity
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ NavMeshSettings:
serializedVersion: 2
m_ObjectHideFlags: 0
m_BuildSettings:
serializedVersion: 2
serializedVersion: 3
agentTypeID: 0
agentRadius: 0.5
agentHeight: 2
Expand All @@ -117,7 +117,7 @@ NavMeshSettings:
cellSize: 0.16666667
manualTileSize: 0
tileSize: 256
accuratePlacement: 0
buildHeightMesh: 0
maxJobWorkers: 0
preserveTilesOutsideBounds: 0
debug:
Expand All @@ -128,6 +128,7 @@ PrefabInstance:
m_ObjectHideFlags: 0
serializedVersion: 2
m_Modification:
serializedVersion: 3
m_TransformParent: {fileID: 0}
m_Modifications:
- target: {fileID: 3873808189371141831, guid: de25749838c0a5a438314548ca5c239f, type: 3}
Expand Down Expand Up @@ -235,6 +236,9 @@ PrefabInstance:
value: 0
objectReference: {fileID: 0}
m_RemovedComponents: []
m_RemovedGameObjects: []
m_AddedGameObjects: []
m_AddedComponents: []
m_SourcePrefab: {fileID: 100100000, guid: de25749838c0a5a438314548ca5c239f, type: 3}
--- !u!114 &559994231 stripped
MonoBehaviour:
Expand Down Expand Up @@ -367,13 +371,13 @@ Transform:
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 629913881}
serializedVersion: 2
m_LocalRotation: {x: 0, y: 0, z: 0, w: 1}
m_LocalPosition: {x: -0.00612866, y: 3.2835584, z: -2.5137057}
m_LocalScale: {x: 1, y: 1, z: 1}
m_ConstrainProportionsScale: 0
m_Children: []
m_Father: {fileID: 0}
m_RootOrder: 5
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
--- !u!1 &705507993
GameObject:
Expand Down Expand Up @@ -461,13 +465,13 @@ Transform:
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 705507993}
serializedVersion: 2
m_LocalRotation: {x: 0.7071068, y: 0, z: 0, w: 0.7071068}
m_LocalPosition: {x: 0, y: 15, z: 0}
m_LocalScale: {x: 1, y: 1, z: 1}
m_ConstrainProportionsScale: 0
m_Children: []
m_Father: {fileID: 0}
m_RootOrder: 1
m_LocalEulerAnglesHint: {x: 90, y: 0, z: 0}
--- !u!1 &963194225
GameObject:
Expand Down Expand Up @@ -510,9 +514,17 @@ Camera:
m_projectionMatrixMode: 1
m_GateFitMode: 2
m_FOVAxisMode: 0
m_Iso: 200
m_ShutterSpeed: 0.005
m_Aperture: 16
m_FocusDistance: 10
m_FocalLength: 50
m_BladeCount: 5
m_Curvature: {x: 2, y: 11}
m_BarrelClipping: 0.25
m_Anamorphism: 0
m_SensorSize: {x: 36, y: 24}
m_LensShift: {x: 0, y: 0}
m_FocalLength: 50
m_NormalizedViewPortRect:
serializedVersion: 2
x: 0
Expand Down Expand Up @@ -546,13 +558,13 @@ Transform:
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 963194225}
serializedVersion: 2
m_LocalRotation: {x: 0.7071068, y: 0, z: 0, w: 0.7071068}
m_LocalPosition: {x: 0, y: 10, z: 0}
m_LocalScale: {x: 1, y: 1, z: 1}
m_ConstrainProportionsScale: 0
m_Children: []
m_Father: {fileID: 0}
m_RootOrder: 0
m_LocalEulerAnglesHint: {x: 90, y: 0, z: 0}
--- !u!114 &963194229
MonoBehaviour:
Expand Down Expand Up @@ -688,13 +700,13 @@ Transform:
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 973737935}
serializedVersion: 2
m_LocalRotation: {x: 0, y: 0, z: 0, w: 1}
m_LocalPosition: {x: -0.00612866, y: 3.2835584, z: -2.5137057}
m_LocalScale: {x: 1, y: 1, z: 1}
m_ConstrainProportionsScale: 0
m_Children: []
m_Father: {fileID: 0}
m_RootOrder: 6
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
--- !u!1 &1389773580
GameObject:
Expand Down Expand Up @@ -756,13 +768,13 @@ Transform:
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 1389773580}
serializedVersion: 2
m_LocalRotation: {x: 0, y: 0, z: 0, w: 1}
m_LocalPosition: {x: 0, y: 0, z: 0}
m_LocalScale: {x: 1, y: 1, z: 1}
m_ConstrainProportionsScale: 0
m_Children: []
m_Father: {fileID: 0}
m_RootOrder: 4
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
--- !u!1 &2053379262
GameObject:
Expand Down Expand Up @@ -798,6 +810,7 @@ MonoBehaviour:
playerScriptableObject: {fileID: 11400000, guid: 4240caefbf2e2f64eb8046b7c92646e0, type: 2}
levelScriptableObjects:
- {fileID: 11400000, guid: bada358cc71564c49831629438d87724, type: 2}
- {fileID: 11400000, guid: 3ceefc333a7c35a4d8743f64f88754db, type: 2}
sfxSource: {fileID: 629913882}
bgMusicSource: {fileID: 973737936}
--- !u!4 &2053379264
Expand All @@ -807,11 +820,22 @@ Transform:
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 2053379262}
serializedVersion: 2
m_LocalRotation: {x: 0, y: 0, z: 0, w: 1}
m_LocalPosition: {x: 0, y: 0, z: 0}
m_LocalScale: {x: 1, y: 1, z: 1}
m_ConstrainProportionsScale: 0
m_Children: []
m_Father: {fileID: 0}
m_RootOrder: 2
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
--- !u!1660057539 &9223372036854775807
SceneRoots:
m_ObjectHideFlags: 0
m_Roots:
- {fileID: 963194228}
- {fileID: 705507995}
- {fileID: 2053379264}
- {fileID: 559994230}
- {fileID: 1389773583}
- {fileID: 629913883}
- {fileID: 973737937}
8 changes: 4 additions & 4 deletions Assets/Scripts/Enemy/EnemyController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@ public class EnemyController

protected int currentHealth;
protected EnemyState currentState;
protected NavMeshAgent Agent => enemyView.Agent;
protected EnemyScriptableObject Data => enemyScriptableObject;
protected Quaternion Rotation => enemyView.transform.rotation;
protected Vector3 Position => enemyView.transform.position;
public NavMeshAgent Agent => enemyView.Agent;
public EnemyScriptableObject Data => enemyScriptableObject;
public Quaternion Rotation => enemyView.transform.rotation;
public Vector3 Position => enemyView.transform.position;


public EnemyController(EnemyScriptableObject enemyScriptableObject)
Expand Down
3 changes: 3 additions & 0 deletions Assets/Scripts/Enemy/EnemyService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,9 @@ public EnemyController CreateEnemy(EnemyScriptableObject enemyScriptableObject)
case EnemyType.OnePunchMan:
enemy = new OnePunchManController(enemyScriptableObject);
break;
case EnemyType.PatrolMan:
enemy = new PatrolManController(enemyScriptableObject);
break;
default:
enemy = new EnemyController(enemyScriptableObject);
break;
Expand Down
2 changes: 1 addition & 1 deletion Assets/Scripts/Enemy/EnemyType.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
public enum EnemyType
{
OnePunchMan,
DashMan,
PatrolMan,
Hitman,
Robot
}
Expand Down
92 changes: 8 additions & 84 deletions Assets/Scripts/Enemy/OnePunchMan/OnePunchManController.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using UnityEngine;
using StatePattern.StateMachine;
using StatePattern.Enemy.Bullet;
using StatePattern.Main;
using StatePattern.Player;
Expand All @@ -7,108 +8,31 @@ namespace StatePattern.Enemy
{
public class OnePunchManController : EnemyController
{
private bool isIdle;
private bool isRotating;
private bool isShooting;
private float idleTimer;
private float shootTimer;
private float targetRotation;
private PlayerController target;

private OnePunchManStateMachine stateMachine;

public OnePunchManController(EnemyScriptableObject enemyScriptableObject) : base(enemyScriptableObject)
{
enemyView.SetController(this);
InitializeVariables();
CreateStateMachine();
stateMachine.ChangeState(States.IDLE);
}

private void InitializeVariables()
{
isIdle = true;
isRotating = false;
isShooting = false;
idleTimer = enemyScriptableObject.IdleTime;
shootTimer = enemyScriptableObject.RateOfFire;
}
private void CreateStateMachine() => stateMachine = new OnePunchManStateMachine(this);

public override void UpdateEnemy()
{
if (currentState == EnemyState.DEACTIVE)
return;

if(isIdle && !isRotating && !isShooting)
{
idleTimer -= Time.deltaTime;
if(idleTimer <= 0)
{
isIdle = false;
isRotating = true;
targetRotation = (Rotation.eulerAngles.y + 180) % 360;
}
}

if(!isIdle && isRotating && !isShooting)
{
SetRotation(CalculateRotation());
if(IsRotationComplete())
{
isIdle = true;
isRotating = false;
ResetTimer();
}
}

if(!isIdle && !isRotating && isShooting)
{
Quaternion desiredRotation = CalculateRotationTowardsPlayer();
SetRotation(RotateTowards(desiredRotation));

if(IsFacingPlayer(desiredRotation))
{
shootTimer -= Time.deltaTime;
if (shootTimer <= 0)
{
shootTimer = enemyScriptableObject.RateOfFire;
Shoot();
}
}

}

}

private void ResetTimer() => idleTimer = enemyScriptableObject.IdleTime;

private Vector3 CalculateRotation() => Vector3.up * Mathf.MoveTowardsAngle(Rotation.eulerAngles.y, targetRotation, enemyScriptableObject.RotationSpeed * Time.deltaTime);

private bool IsRotationComplete() => Mathf.Abs(Mathf.Abs(Rotation.eulerAngles.y) - Mathf.Abs(targetRotation)) < Data.RotationThreshold;

private bool IsFacingPlayer(Quaternion desiredRotation) => Quaternion.Angle(Rotation, desiredRotation) < Data.RotationThreshold;

private Quaternion CalculateRotationTowardsPlayer()
{
Vector3 directionToPlayer = target.Position - Position;
directionToPlayer.y = 0f;
return Quaternion.LookRotation(directionToPlayer, Vector3.up);
stateMachine.Update();
}

private Quaternion RotateTowards(Quaternion desiredRotation) => Quaternion.LerpUnclamped(Rotation, desiredRotation, enemyScriptableObject.RotationSpeed / 30 * Time.deltaTime);

public override void PlayerEnteredRange(PlayerController targetToSet)
{
base.PlayerEnteredRange(targetToSet);
isIdle = false;
isRotating = false;
isShooting = true;
target = targetToSet;
shootTimer = 0;
stateMachine.ChangeState(States.SHOOTING);
}

public override void PlayerExitedRange()
{
isIdle = true;
isRotating = false;
isShooting = false;
}
public override void PlayerExitedRange() => stateMachine.ChangeState(States.IDLE);
}
}
45 changes: 45 additions & 0 deletions Assets/Scripts/Enemy/OnePunchMan/OnePunchManStateMachine.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
using StatePattern.StateMachine;
using System.Collections.Generic;

namespace StatePattern.Enemy
{
public class OnePunchManStateMachine : IStateMachine
{
private OnePunchManController Owner;
private IState currentState;
protected Dictionary<States, IState> States = new Dictionary<States, IState>();

public OnePunchManStateMachine(OnePunchManController Owner)
{
this.Owner = Owner;
CreateStates();
SetOwner();
}

private void CreateStates()
{
States.Add(StateMachine.States.IDLE, new IdleState(this));
States.Add(StateMachine.States.ROTATING, new RotatingState(this));
States.Add(StateMachine.States.SHOOTING, new ShootingState(this));
}

private void SetOwner()
{
foreach(IState state in States.Values)
{
state.Owner = Owner;
}
}

public void Update() => currentState?.Update();

protected void ChangeState(IState newState)
{
currentState?.OnStateExit();
currentState = newState;
currentState?.OnStateEnter();
}

public void ChangeState(States newState) => ChangeState(States[newState]);
}
}
11 changes: 11 additions & 0 deletions Assets/Scripts/Enemy/OnePunchMan/OnePunchManStateMachine.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions Assets/Scripts/Enemy/PatrolMan.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading