-
-
Notifications
You must be signed in to change notification settings - Fork 138
Commit
Feature: turn based example
- Loading branch information
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
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,26 @@ | ||
using CrashKonijn.Agent.Core; | ||
using CrashKonijn.Goap.Runtime; | ||
using UnityEngine; | ||
|
||
namespace TurnBased.Actions | ||
{ | ||
public class TurnWanderAction : GoapActionBase<TurnWanderAction.Data> | ||
{ | ||
public class Data : IActionData | ||
{ | ||
public ITarget Target { get; set; } | ||
} | ||
|
||
public override void Created() { } | ||
|
||
public override void Start(IMonoAgent agent, Data data) { } | ||
|
||
public override IActionRunState Perform(IMonoAgent agent, Data data, IActionContext context) | ||
{ | ||
Debug.Log($"Performing wander action for {agent}"); | ||
return ActionRunState.Stop; | ||
} | ||
|
||
public override void End(IMonoAgent agent, Data data) { } | ||
} | ||
} |
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,68 @@ | ||
using CrashKonijn.Agent.Core; | ||
using CrashKonijn.Goap.Runtime; | ||
using UnityEngine; | ||
|
||
namespace CrashKonijn.Goap.Demos.TurnBased | ||
{ | ||
[GoapId("Wander-9cf982ae-4314-4d83-96c8-e7ff0616a798")] | ||
public class WanderAction : GoapActionBase<WanderAction.Data> | ||
{ | ||
// This method is called when the action is created | ||
// This method is optional and can be removed | ||
public override void Created() | ||
{ | ||
} | ||
|
||
// This method is called every frame before the action is performed | ||
// If this method returns false, the action will be stopped | ||
// This method is optional and can be removed | ||
public override bool IsValid(IActionReceiver agent, Data data) | ||
{ | ||
return true; | ||
} | ||
|
||
// This method is called when the action is started | ||
// This method is optional and can be removed | ||
public override void Start(IMonoAgent agent, Data data) | ||
{ | ||
} | ||
|
||
// This method is called once before the action is performed | ||
// This method is optional and can be removed | ||
public override void BeforePerform(IMonoAgent agent, Data data) | ||
{ | ||
} | ||
|
||
// This method is called every frame while the action is running | ||
// This method is required | ||
public override IActionRunState Perform(IMonoAgent agent, Data data, IActionContext context) | ||
{ | ||
return ActionRunState.Completed; | ||
} | ||
|
||
// This method is called when the action is completed | ||
// This method is optional and can be removed | ||
public override void Complete(IMonoAgent agent, Data data) | ||
{ | ||
} | ||
|
||
// This method is called when the action is stopped | ||
// This method is optional and can be removed | ||
public override void Stop(IMonoAgent agent, Data data) | ||
{ | ||
} | ||
|
||
// This method is called when the action is completed or stopped | ||
// This method is optional and can be removed | ||
public override void End(IMonoAgent agent, Data data) | ||
{ | ||
} | ||
|
||
// The action class itself must be stateless! | ||
// All data should be stored in the data class | ||
public class Data : IActionData | ||
{ | ||
public ITarget Target { get; set; } | ||
} | ||
} | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
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,53 @@ | ||
using CrashKonijn.Goap.Core; | ||
using CrashKonijn.GOAP.Demos.TurnBased.Interfaces; | ||
using CrashKonijn.Goap.Runtime; | ||
using UnityEngine; | ||
|
||
namespace CrashKonijn.GOAP.Demos.TurnBased.Behaviours | ||
{ | ||
public class GameBehaviour : MonoBehaviour | ||
{ | ||
private IGrid grid; | ||
private TurnBehaviour turns; | ||
private IGoap goapRunner; | ||
|
||
[SerializeField] | ||
private GameObject agentPrefab; | ||
|
||
|
||
private void Awake() | ||
{ | ||
this.grid = FindObjectOfType<GridBehaviour>(); | ||
this.goapRunner = FindObjectOfType<GoapBehaviour>(); | ||
this.turns = this.GetComponent<TurnBehaviour>(); | ||
} | ||
|
||
private void Start() | ||
{ | ||
this.SpawnAgent("TurnBased", Color.red); | ||
this.SpawnAgent("TurnBased", Color.blue); | ||
} | ||
|
||
private void SpawnAgent(string setId, Color color) | ||
{ | ||
var actionProvider = Instantiate(this.agentPrefab, this.GetRandomPosition(), Quaternion.identity).GetComponent<GoapActionProvider>(); | ||
|
||
actionProvider.AgentType = this.goapRunner.GetAgentType(setId); | ||
actionProvider.gameObject.SetActive(true); | ||
|
||
actionProvider.gameObject.transform.name = $"agent {actionProvider.GetInstanceID()}"; | ||
|
||
var spriteRenderer = actionProvider.GetComponentInChildren<SpriteRenderer>(); | ||
spriteRenderer.color = color; | ||
|
||
this.turns.Register(actionProvider); | ||
} | ||
|
||
private Vector3 GetRandomPosition() | ||
{ | ||
var tile = this.grid.GetWalkableTiles().Random(); | ||
|
||
return new Vector3(tile.X, 0.5f, tile.Y); | ||
} | ||
} | ||
} |
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,72 @@ | ||
using System.Linq; | ||
using CrashKonijn.GOAP.Demos.TurnBased.Interfaces; | ||
using UnityEngine; | ||
|
||
namespace CrashKonijn.GOAP.Demos.TurnBased.Behaviours | ||
{ | ||
public class GridBehaviour : MonoBehaviour, IGrid | ||
{ | ||
public Transform tilePrefab; | ||
public int width = 10; | ||
public int height = 10; | ||
private ITile[,] grid; | ||
private ITile[] tiles; | ||
private IPathfinding pathfinding; | ||
|
||
private void Awake() | ||
{ | ||
this.grid = new ITile[this.width, this.height]; | ||
this.tiles = new ITile[this.width * this.height]; | ||
this.pathfinding = this.GetComponent<PathfindingBehaviour>(); | ||
|
||
for (var x = 0; x < this.width; x++) | ||
{ | ||
for (var y = 0; y < this.height; y++) | ||
{ | ||
var tile = this.CreateTile(x, y); | ||
this.grid[x, y] = tile; | ||
this.tiles[x + y * this.width] = tile; | ||
} | ||
} | ||
} | ||
|
||
private ITile CreateTile(int x, int y) | ||
{ | ||
var instance = Instantiate(this.tilePrefab, this.transform, true); | ||
instance.name = $"Tile {x} {y}"; | ||
instance.position = new Vector3(x, 0, y); | ||
|
||
var tile = instance.GetComponent<ITile>(); | ||
|
||
tile.Initialize(x, y, this); | ||
tile.SetWalkable(Random.Range(0, 100) >= 10); | ||
|
||
return tile; | ||
} | ||
|
||
public ITile[,] GetGrid() | ||
{ | ||
return this.grid; | ||
} | ||
|
||
public ITile[] GetWalkableTiles() | ||
{ | ||
return this.tiles.Where(x => x.IsWalkable).ToArray(); | ||
} | ||
|
||
public ITile GetTile(Vector3 position) | ||
{ | ||
return this.grid[Mathf.RoundToInt(position.x), Mathf.RoundToInt(position.z)]; | ||
} | ||
|
||
public ITile[] GetPath(ITile from, ITile to) | ||
{ | ||
return this.pathfinding.FindPath(this.grid, from, to); | ||
} | ||
|
||
public ITile[] GetPath(Vector3 from, Vector3 to) | ||
{ | ||
return this.pathfinding.FindPath(this.grid, this.GetTile(from), this.GetTile(to)); | ||
} | ||
} | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.