Skip to content

Commit

Permalink
Added Change Tile action, and improved overall system
Browse files Browse the repository at this point in the history
  • Loading branch information
DiogoDeAndrade committed Nov 2, 2024
1 parent 5d58183 commit 76f074a
Show file tree
Hide file tree
Showing 51 changed files with 2,946 additions and 135 deletions.
249 changes: 249 additions & 0 deletions Assets/OkapiKit/Scripts/Actions/ActionChangeTile.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,249 @@
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Tilemaps;

namespace OkapiKit
{
[AddComponentMenu("Okapi/Action/Change Tile")]
public class ActionChangeTile : Action
{
public enum VicinityType { Single, FourWay, EightWay, Circle };

[SerializeField] private VicinityType vicinityType = VicinityType.Single;
[SerializeField] private float radius = 1.0f;
[SerializeField] private Transform target;
[SerializeField] private Hypertag[] tags;
[SerializeField] private TileConverterRule[] rules;

private GridObject gridObject;

public override void Execute()
{
if (!enableAction) return;
if (!EvaluatePreconditions()) return;
if (gridObject == null) return;

var positions = GetTargetPositions();
foreach (var position in positions)
{
Fill(position);
}
}

public override string GetActionTitle() => "Change Tile";

public override string GetRawDescription(string ident, GameObject gameObject)
{
string desc = GetPreconditionsString(gameObject);

string targetPos = "";
if ((tags != null) && (tags.Length > 0))
{
targetPos = "all positions tagged with {";
for (int i = 0; i < tags.Length; i++)
{
if (i != 0) desc += ",";
desc += $"[{tags[i]}]";
}
targetPos += "}";
}
else if (target != null)
{
targetPos = $"at {target.name}";
}
else
{
targetPos = $"at this positon";
}

switch (vicinityType)
{
case VicinityType.Single:
desc += $"changes tiles {targetPos} according to the given rules:\n";
break;
case VicinityType.FourWay:
desc += $"changes tiles {targetPos}, and positions up, down, left and right, up to {radius} cells away, according to the given rules:\n";
break;
case VicinityType.EightWay:
desc += $"changes tiles {targetPos}, and positions all around, up to {radius} cells away, according to the given rules:\n";
break;
case VicinityType.Circle:
desc += $"changes tiles {targetPos}, in circle of {radius} cells away, according to the given rules:\n";
break;
default:
break;
}

if ((rules != null) && (rules.Length > 0))
{
for (int i = 0; i < rules.Length; i++)
{
var rule = rules[i];
if (rule.source == null)
{
if (rule.dest == null)
{
desc += $" {i + 1}. Invalid rule";
}
else
{
desc += $" {i + 1}. None => {rule.dest.name}";
}
}
else
{
if (rule.dest == null)
{
desc += $" {i + 1}. {rule.source.name} => Deleted";
}
else
{
desc += $" {i + 1}. {rule.source.name} => {rule.dest.name}";
}
}
if (i < rules.Length - 1) desc += "\n";
}
}
else
{
desc += " Rules are not defined";
}
return desc;
}

protected override void CheckErrors()
{
base.CheckErrors();

if (gridObject == null)
{
gridObject = GetComponent<GridObject>();
if (gridObject == null)
{
_logs.Add(new LogEntry(LogEntry.Type.Error, "No Grid Object found!", "Change Tile action requires a Grid Object to be executed!"));
}
}

if ((tags == null) || (tags.Length == 0))
{
if (transform == null)
{
_logs.Add(new LogEntry(LogEntry.Type.Error, "Tags not defined!", "Tags (or a target object) is necessary to indicate the target of the operation."));
}
}
else
{
int index = 0;
foreach (var tag in tags)
{
if (tag == null)
{
_logs.Add(new LogEntry(LogEntry.Type.Error, $"Tag slot is empty in tag list (index={index})!", "Empty tags are useless, fill it in, or delete it"));
}
index++;
}
}

if ((rules == null) || (rules.Length == 0))
{
_logs.Add(new LogEntry(LogEntry.Type.Warning, $"Missing rules for operation!", "Need to specify what kind of transformations will be done"));
}
else
{
int index = 0;
foreach (var rule in rules)
{
if (rule == null)
{
_logs.Add(new LogEntry(LogEntry.Type.Error, $"Undefined rule at slot {index}!", "Empty tags are useless, fill it in, or delete it"));
}
else if ((rule.source == null) && (rule.dest == null))
{
_logs.Add(new LogEntry(LogEntry.Type.Error, $"Invalid rule at slot {index}!", "Rule is [NONE] to [NONE], which is invalid!"));
}
index++;
}
}
}

protected override void Awake()
{
base.Awake();

gridObject = GetComponent<GridObject>();
}

List<Vector3> GetTargetPositions()
{
List<Vector3> ret = new();
if ((tags == null) || (tags.Length == 0))
{
if (target != null)
{
ret.Add(target.position);
return ret;
}
else
{
ret.Add(transform.position);
return ret;
}
}

var objs = gameObject.FindObjectsOfTypeWithHypertag<Transform>(tags);

foreach (var obj in objs)
{
ret.Add(obj.position);
}
return ret;
}

public void Fill(Vector3 worldPos)
{
Vector2Int gridPos = gridObject.WorldToGrid(worldPos);
int r = Mathf.FloorToInt(radius);
switch (vicinityType)
{
case VicinityType.Single:
gridObject.RunRules(worldPos, rules);
break;
case VicinityType.FourWay:
gridObject.RunRules(gridPos, rules);
for (int i = 1; i <= r; i++)
{
gridObject.RunRules(new Vector2Int(gridPos.x + i, gridPos.y), rules);
gridObject.RunRules(new Vector2Int(gridPos.x - i, gridPos.y), rules);
gridObject.RunRules(new Vector2Int(gridPos.x, gridPos.y + i), rules);
gridObject.RunRules(new Vector2Int(gridPos.x, gridPos.y - i), rules);
}
break;
case VicinityType.EightWay:
for (int y = gridPos.y - r; y <= gridPos.y + r; y++)
{
for (int x = gridPos.x - r; x <= gridPos.x + r; x++)
{
gridObject.RunRules(new Vector2Int(x, y), rules);
}
}
break;
case VicinityType.Circle:
for (int y = gridPos.y - r; y <= gridPos.y + r; y++)
{
for (int x = gridPos.x - r; x <= gridPos.x + r; x++)
{
if (Vector2.Distance(new Vector2(x, y), new Vector2(gridPos.x, gridPos.y)) < radius)
{
gridObject.RunRules(new Vector2Int(x, y), rules);
}
}
}
break;
default:
break;
}
}
}
};
11 changes: 11 additions & 0 deletions Assets/OkapiKit/Scripts/Actions/ActionChangeTile.cs.meta

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

80 changes: 44 additions & 36 deletions Assets/OkapiKit/Scripts/Actions/ActionSetAnimationParameter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -189,45 +189,53 @@ protected override void CheckErrors()

for (int i = 0; i < anm.parameterCount; i++)
{
var param = anm.GetParameter(i);
if (param.name == animationParameter)
try
{
found = true;
pType = param.type.ToString();
switch (param.type)
var param = anm.GetParameter(i);
if (param.name == animationParameter)
{
case AnimatorControllerParameterType.Float:
if ((valueType == ValueType.Float) || (valueType == ValueType.VelocityX) || (valueType == ValueType.VelocityY))
{
rightType = true;
}
else if (valueType == ValueType.Value)
{
if (variable) rightType = (variable.type == Variable.Type.Float);
else if (valueHandler) rightType = (valueHandler.GetValueType() == Variable.Type.Float);
}
break;
case AnimatorControllerParameterType.Int:
if (valueType == ValueType.Int)
{
rightType = true;
}
else if (valueType == ValueType.Value)
{
if (variable) rightType = (variable.type == Variable.Type.Integer);
else if (valueHandler) rightType = (valueHandler.GetValueType() == Variable.Type.Integer);
}
break;
case AnimatorControllerParameterType.Bool:
rightType = (valueType == ValueType.Boolean);
break;
case AnimatorControllerParameterType.Trigger:
rightType = (valueType == ValueType.Trigger);
break;
default:
break;
found = true;
pType = param.type.ToString();
switch (param.type)
{
case AnimatorControllerParameterType.Float:
if ((valueType == ValueType.Float) || (valueType == ValueType.VelocityX) || (valueType == ValueType.VelocityY))
{
rightType = true;
}
else if (valueType == ValueType.Value)
{
if (variable) rightType = (variable.type == Variable.Type.Float);
else if (valueHandler) rightType = (valueHandler.GetValueType() == Variable.Type.Float);
}
break;
case AnimatorControllerParameterType.Int:
if (valueType == ValueType.Int)
{
rightType = true;
}
else if (valueType == ValueType.Value)
{
if (variable) rightType = (variable.type == Variable.Type.Integer);
else if (valueHandler) rightType = (valueHandler.GetValueType() == Variable.Type.Integer);
}
break;
case AnimatorControllerParameterType.Bool:
rightType = (valueType == ValueType.Boolean);
break;
case AnimatorControllerParameterType.Trigger:
rightType = (valueType == ValueType.Trigger);
break;
default:
break;
}
break;
}
break;
}
catch
{
// Unity throws an exception sometimes, I think it's a bug on the animator when we list the parameters, since we ask for the number of parameters
// and he complains that the parameter doesn't exist with an error like "index must be between 0 and 2", and the index is 0.
}
}

Expand Down
Loading

0 comments on commit 76f074a

Please sign in to comment.