Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Game Update : #5

Open
wants to merge 2 commits into
base: Patrol-Man-Setup
Choose a base branch
from
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
3 changes: 2 additions & 1 deletion Assets/Scenes/GameScene.unity
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ RenderSettings:
m_ReflectionIntensity: 1
m_CustomReflection: {fileID: 0}
m_Sun: {fileID: 705507994}
m_IndirectSpecularColor: {r: 0.17276844, g: 0.21589246, b: 0.2978263, a: 1}
m_IndirectSpecularColor: {r: 0.172768, g: 0.21589167, b: 0.29782546, a: 1}
m_UseRadianceAmbientProbe: 0
--- !u!157 &3
LightmapSettings:
Expand Down Expand Up @@ -798,6 +798,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 Down
2 changes: 1 addition & 1 deletion Assets/Scripts/Enemy/EnemyController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public class EnemyController

protected int currentHealth;
protected EnemyState currentState;
protected NavMeshAgent Agent => enemyView.Agent;
public NavMeshAgent Agent => enemyView.Agent;
public EnemyScriptableObject Data => enemyScriptableObject;
public Quaternion Rotation => enemyView.transform.rotation;
public Vector3 Position => enemyView.transform.position;
Expand Down
2 changes: 1 addition & 1 deletion Assets/Scripts/Enemy/OnePunchMan/IState.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ namespace StatePattern.Enemy
{
public interface IState
{
public OnePunchManController Owner { get; set; }
public EnemyController Owner { get; set; }
public void OnStateEnter();
public void Update();
public void OnStateExit();
Expand Down
19 changes: 19 additions & 0 deletions Assets/Scripts/Enemy/OnePunchMan/IStateMachine.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

namespace StatePattern.StateMachine
{
public interface IStateMachine
{
public void ChangeState(States newState);
}
public enum States
{
IDLE,
ROTATING,
SHOOTING,
PATROLLING,
CHASING
}
}
11 changes: 11 additions & 0 deletions Assets/Scripts/Enemy/OnePunchMan/IStateMachine.cs.meta

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

5 changes: 4 additions & 1 deletion Assets/Scripts/Enemy/OnePunchMan/OnePunchManStateMachine.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Collections.Generic;
using StatePattern.StateMachine;

namespace StatePattern.Enemy
{
Expand Down Expand Up @@ -46,6 +47,8 @@ public enum OnePunchManStates
{
IDLE,
ROTATING,
SHOOTING
SHOOTING,
PATROLLING,
CHASING
}
}
8 changes: 8 additions & 0 deletions Assets/Scripts/Enemy/PatrolManStateMachine.meta

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

39 changes: 39 additions & 0 deletions Assets/Scripts/Enemy/PatrolManStateMachine/PatrolManController.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
using StatePattern.Player;
using StatePattern.StateMachine;
using StatePattern.Main;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

namespace StatePattern.Enemy
{
public class PatrolManController : EnemyController
{
private PatrolManStateMachine stateMachine;

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

private void CreateStateMachine() => stateMachine = new PatrolManStateMachine(this);

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

stateMachine.Update();
}

public override void PlayerEnteredRange(PlayerController targetToSet)
{
base.PlayerEnteredRange(targetToSet);
stateMachine.ChangeState(States.CHASING);
}

public override void PlayerExitedRange() => stateMachine.ChangeState(States.IDLE);
}
}

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

Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
using StatePattern.StateMachine;
using System.Collections.Generic;
using UnityEngine;

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

public PatrolManStateMachine(PatrolManController Owner)
{
this.Owner = Owner;
CreateStates();
SetOwner();
}

private void CreateStates()
{
States.Add(StateMachine.States.IDLE, new IdleState(this));
States.Add(StateMachine.States.PATROLLING, new PatrollingState(this));
States.Add(StateMachine.States.CHASING, new ChasingState(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]);
}
}

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

50 changes: 50 additions & 0 deletions Assets/Scripts/Enemy/States/ChasingState.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
using StatePattern.Main;
using StatePattern.Player;
using StatePattern.StateMachine;
using System.Collections;
using UnityEngine;

namespace StatePattern.Enemy
{
public class ChasingState : IState
{
public EnemyController Owner { get; set; }
private IStateMachine stateMachine;
private PlayerController target;

public ChasingState(IStateMachine stateMachine) => this.stateMachine = stateMachine;

public void OnStateEnter()
{
SetTarget();
SetStoppingDistance();
}

public void Update()
{
MoveTowardsTarget();
if (ReachedTarget())
{
ResetPath();
stateMachine.ChangeState(States.SHOOTING);
}
}

public void OnStateExit() => target = null;


private void SetTarget() => target = GameService.Instance.PlayerService.GetPlayer();

private void SetStoppingDistance() => Owner.Agent.stoppingDistance = Owner.Data.PlayerStoppingDistance;

private bool MoveTowardsTarget() => Owner.Agent.SetDestination(target.Position);

private bool ReachedTarget() => Owner.Agent.remainingDistance <= Owner.Agent.stoppingDistance;

private void ResetPath()
{
Owner.Agent.isStopped = true;
Owner.Agent.ResetPath();
}
}
}
11 changes: 11 additions & 0 deletions Assets/Scripts/Enemy/States/ChasingState.cs.meta

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

Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using StatePattern.StateMachine;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
Expand All @@ -6,19 +7,19 @@ namespace StatePattern.Enemy
{
public class IdleState : IState
{
public OnePunchManController Owner { get; set; }
private OnePunchManStateMachine stateMachine;
public EnemyController Owner { get; set; }
private IStateMachine stateMachine;
private float timer;

public IdleState(OnePunchManStateMachine stateMachine) => this.stateMachine = stateMachine;
public IdleState(IStateMachine stateMachine) => this.stateMachine = stateMachine;

public void OnStateEnter() => ResetTimer();

public void Update()
{
timer -= Time.deltaTime;
if (timer <= 0)
stateMachine.ChangeState(OnePunchManStates.ROTATING);
stateMachine.ChangeState(States.ROTATING);
}

public void OnStateExit() => timer = 0;
Expand Down
55 changes: 55 additions & 0 deletions Assets/Scripts/Enemy/States/PatrollingState.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
using StatePattern.StateMachine;
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

namespace StatePattern.Enemy
{
public class PatrollingState : IState
{
public EnemyController Owner { get; set; }
private IStateMachine stateMachine;
private int currentPatrollingIndex = -1;
private Vector3 destination;

public PatrollingState(IStateMachine stateMachine) => this.stateMachine = stateMachine;

public void OnStateEnter()
{
SetNextWayPointIndex();
destination = GetDestination();
MoveTowardsDestination();
}

public void Update()
{
if (ReachedDestination())
stateMachine.ChangeState(States.IDLE);
}

public void OnStateExit() { }

private void SetNextWayPointIndex()
{
if (currentPatrollingIndex == Owner.Data.PatrollingPoints.Count - 1)
{
currentPatrollingIndex = 0;
}
else
{
currentPatrollingIndex++;
}
}
private Vector3 GetDestination() => Owner.Data.PatrollingPoints[currentPatrollingIndex];

private void MoveTowardsDestination()
{
Owner.Agent.isStopped = false;

Owner.Agent.SetDestination(destination);
}

private bool ReachedDestination() => Owner.Agent.remainingDistance <= Owner.Agent.stoppingDistance;
}
}
11 changes: 11 additions & 0 deletions Assets/Scripts/Enemy/States/PatrollingState.cs.meta

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

Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using StatePattern.StateMachine;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
Expand All @@ -6,19 +7,19 @@ namespace StatePattern.Enemy
{
public class RotatingState : IState
{
public OnePunchManController Owner { get; set; }
private OnePunchManStateMachine stateMachine;
public EnemyController Owner { get; set; }
private IStateMachine stateMachine;
private float targetRotation;

public RotatingState(OnePunchManStateMachine stateMachine) => this.stateMachine = stateMachine;
public RotatingState(IStateMachine stateMachine) => this.stateMachine = stateMachine;

public void OnStateEnter() => targetRotation = (Owner.Rotation.eulerAngles.y + 180) % 360;

public void Update()
{
Owner.SetRotation(CalculateRotation());
if (IsRotationComplete())
stateMachine.ChangeState(OnePunchManStates.IDLE);
stateMachine.ChangeState(States.IDLE);
}

public void OnStateExit() => targetRotation = 0;
Expand Down
Loading