-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLocation.cs
135 lines (110 loc) · 3.71 KB
/
Location.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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
using System.Collections.Generic;
using UnityEngine;
public class Location {
public readonly int x;
public readonly int y;
public readonly int capacity;
public int sugar { get; private set; }
public Location north { get; private set; }
public Location south { get; private set; }
public Location east { get; private set; }
public Location west { get; private set; }
public GameObject gameObject;
public Agent agent;
public Location (int x, int y, int capacity) {
this.x = x;
this.y = y;
this.capacity = capacity;
sugar = capacity;
InitGameObject();
}
public void Destroy () {
Object.Destroy(gameObject);
}
public void SetNeighbors (
Location north,
Location south,
Location east,
Location west
) {
this.north = north;
this.south = south;
this.east = east;
this.west = west;
}
public void Step () {
sugar = Mathf.Min(sugar + Simulation.Parameters.SUGAR_GROWTH_RATE, capacity);
}
public void Render () {
gameObject.transform.localScale = Mathf.Sqrt(sugar) * Vector3.one / 25;
}
public int Harvest () {
int sugar = this.sugar;
this.sugar = 0;
return sugar;
}
public List<Location> GetNeighboringLocations () {
List<Location> locations = new List<Location>();
locations.Add(north);
locations.Add(south);
locations.Add(east);
locations.Add(west);
return locations;
}
public List<List<Location>> GetAllLocationsInSight (int distance) {
List<List<Location>> allLocations = new List<List<Location>>();
allLocations.Add(GetNorthernLocations(distance));
allLocations.Add(GetSouthernLocations(distance));
allLocations.Add(GetEasternLocations(distance));
allLocations.Add(GetWesternLocations(distance));
return allLocations;
}
private List<Location> GetNorthernLocations (int distance) {
List<Location> locations = new List<Location>();
Location that = this;
do {
locations.Add(that.north);
that = that.north;
} while ( --distance > 0 );
return locations;
}
private List<Location> GetSouthernLocations (int distance) {
List<Location> locations = new List<Location>();
Location that = this;
do {
locations.Add(that.south);
that = that.south;
} while ( --distance > 0 );
return locations;
}
private List<Location> GetEasternLocations (int distance) {
List<Location> locations = new List<Location>();
Location that = this;
do {
locations.Add(that.east);
that = that.east;
} while ( --distance > 0 );
return locations;
}
private List<Location> GetWesternLocations (int distance) {
List<Location> locations = new List<Location>();
Location that = this;
do {
locations.Add(that.west);
that = that.west;
} while ( --distance > 0 );
return locations;
}
private void InitGameObject () {
gameObject = GameObject.CreatePrimitive(PrimitiveType.Plane);
gameObject.name = "Sugar" + x + "," + y;
gameObject.transform.localScale = Vector3.zero;
gameObject.transform.localPosition = new Vector3(y, -0.95f, x);
Object.Destroy(gameObject.GetComponent<Collider>());
Renderer renderer = gameObject.GetComponent<Renderer>();
renderer.receiveShadows = false;
renderer.shadowCastingMode = UnityEngine.Rendering.ShadowCastingMode.Off;
renderer.sharedMaterial = Materials.SUGAR;
}
}