-
Notifications
You must be signed in to change notification settings - Fork 6
/
GrassKnower.cs
60 lines (52 loc) · 2.05 KB
/
GrassKnower.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
using System;
using System.Collections.Generic;
using UnityEngine;
namespace GrassyKnight
{
// Responsible for knowing what is grass and what is not
abstract class GrassKnower {
public abstract bool IsGrass(GameObject gameObject);
}
// Uses a heuristic algorithm to detect whether something is grass. Will
// not always be right, but does an OK job.
class HeuristicGrassKnower : GrassKnower {
// All the grass in the game have some child component with grass in
// its name. Ex: GrassBehavior or GrassSpriteRenderer.
private static bool HasGrassyComponent(GameObject gameObject) {
foreach (Component component in gameObject.GetComponents<Component>())
{
if (component.GetType().Name.ToLower().Contains("grass"))
{
return true;
}
}
return false;
}
public override bool IsGrass(GameObject gameObject) {
return
// If the game calls it grass, we will too
gameObject.name.ToLower().Contains("grass") &&
// Filter out _some_ unhittable grass (this won't catch all
// such grass).
gameObject.GetComponentsInChildren<Collider2D>().Length > 0 &&
// Check if there's a grassy component. There's at least one
// floor tile that has grass in its name that this skips for
// us.
HasGrassyComponent(gameObject);
}
}
// GrassKnower that just has a big list of grass it uses to know what's
// grass.
class CuratedGrassKnower : GrassKnower {
public override bool IsGrass(GameObject gameObject) {
GrassKey k = GrassKey.FromGameObject(gameObject);
return GrassList.AllGrass.Contains(k);
}
public HashSet<GrassKey> GetAllGrassKeys() {
return GrassList.AllGrass;
}
public (GrassKey, GrassKey)[] GetAliases() {
return GrassList.Aliases;
}
}
}