Skip to content

Commit f095e81

Browse files
committed
Port model to Godot
1 parent 73a2830 commit f095e81

File tree

11 files changed

+1135
-0
lines changed

11 files changed

+1135
-0
lines changed
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
using System;
2+
using Godot;
3+
4+
/*
5+
* RATING: 5 stars
6+
* Has unit tests
7+
* CODE REVIEW: 4/16/22
8+
*/
9+
namespace PJ
10+
{
11+
/// <summary>
12+
/// Limits a degree angle to a direct axis angle
13+
/// (used for 4-way, N-way movement in 2D space)
14+
/// </summary>
15+
public class AngleAxisLimiter2D
16+
{
17+
public int axisLimit = 0;
18+
19+
public AngleAxisLimiter2D(int axisLimit)
20+
{
21+
this.axisLimit = axisLimit;
22+
}
23+
24+
public Angle LimitAngle(Angle angle)
25+
{
26+
if (axisLimit <= 0) { return angle; }
27+
28+
if (angle.Degrees < 0)
29+
{
30+
return Angle.DegreesAngle(0);
31+
}
32+
33+
// If there are 4 axes available, then angles -45 to 45 are up (90 degrees total)
34+
var sliceAngle = 360.0f / axisLimit;
35+
var result = Mathf.Round(angle.Degrees / sliceAngle);
36+
return Angle.DegreesAngle(result * sliceAngle);
37+
}
38+
}
39+
}
Lines changed: 172 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,172 @@
1+
using System.Collections;
2+
using System.Collections.Generic;
3+
using Godot;
4+
using PJ;
5+
6+
/*
7+
* RATING: 5 stars
8+
* Simple types
9+
* CODE REVIEW: 4/14/22
10+
*/
11+
namespace PJ
12+
{
13+
/// <summary>
14+
/// 8-way direction of movement on a board/map
15+
/// </summary>
16+
public enum MapDirection
17+
{
18+
Northwest,
19+
North,
20+
Northeast,
21+
East,
22+
South,
23+
Southeast,
24+
Southwest,
25+
West
26+
}
27+
28+
/// <summary>
29+
/// Axis limits for map direction movement
30+
/// </summary>
31+
public enum MapDirectionAxisLimit
32+
{
33+
FourWay,
34+
35+
EightWay
36+
}
37+
38+
/// <summary>
39+
/// A rectangle in 2D space
40+
/// </summary>
41+
public struct Rect2I
42+
{
43+
public Vector2I position;
44+
public Vector2I size;
45+
46+
public Rect2I(Vector2I position, Vector2I size)
47+
{
48+
this.position = position;
49+
this.size = size;
50+
}
51+
}
52+
53+
/// <summary>
54+
/// Axial direction of travel
55+
/// </summary>
56+
public enum AxialDirection
57+
{
58+
Right,
59+
Left
60+
}
61+
62+
public enum AxialType
63+
{
64+
AxialAny, // Any axial tile that touches the origin tile
65+
AxialEdge // Any axial tile that has an edge touching the origin tile (no square diagonal)
66+
}
67+
}
68+
69+
70+
namespace PJ
71+
{
72+
public static class Extensions_MapDirection
73+
{
74+
/// <summary>
75+
/// Returns the opposite direction
76+
/// </summary>
77+
public static MapDirection Opposite(this MapDirection direction)
78+
{
79+
switch (direction)
80+
{
81+
case MapDirection.Northwest:
82+
return MapDirection.Southeast;
83+
case MapDirection.North:
84+
return MapDirection.South;
85+
case MapDirection.Northeast:
86+
return MapDirection.Southwest;
87+
case MapDirection.Southeast:
88+
return MapDirection.Northwest;
89+
case MapDirection.South:
90+
return MapDirection.North;
91+
case MapDirection.Southwest:
92+
return MapDirection.Northeast;
93+
}
94+
95+
return MapDirection.North;
96+
}
97+
98+
/// <summary>
99+
/// Returns offset in matrix space (top-left is 0, 0)
100+
/// </summary>
101+
public static Vector2I Offset(this MapDirection state)
102+
{
103+
switch (state)
104+
{
105+
case MapDirection.Northwest:
106+
return new Vector2I(-1, -1);
107+
case MapDirection.North:
108+
return new Vector2I(0, -1);
109+
case MapDirection.Northeast:
110+
return new Vector2I(1, -1);
111+
case MapDirection.East:
112+
return new Vector2I(1, 0);
113+
case MapDirection.Southeast:
114+
return new Vector2I(1, 1);
115+
case MapDirection.South:
116+
return new Vector2I(0, 1);
117+
case MapDirection.Southwest:
118+
return new Vector2I(-1, 1);
119+
case MapDirection.West:
120+
return new Vector2I(-1, 0);
121+
}
122+
123+
return new Vector2I(0, 0);
124+
}
125+
}
126+
127+
public static class MapUtils
128+
{
129+
/// <summary>
130+
/// Translate a degree angle to a map direction
131+
/// </summary>
132+
public static MapDirection AngleToMapDirection(Angle angle, MapDirectionAxisLimit axisLimit)
133+
{
134+
var degreeAngle = angle.Degrees;
135+
var axisLimitNumber = 4;
136+
switch (axisLimit)
137+
{
138+
case MapDirectionAxisLimit.EightWay:
139+
axisLimitNumber = 8;
140+
break;
141+
}
142+
143+
var angleAxisLimiter = new AngleAxisLimiter2D(axisLimitNumber);
144+
var limitedAngle = angleAxisLimiter.LimitAngle(Angle.DegreesAngle(degreeAngle));
145+
146+
var result = MapDirection.North;
147+
148+
switch (Mathf.RoundToInt(limitedAngle.Degrees))
149+
{
150+
case 0:
151+
case 360:
152+
return MapDirection.North;
153+
case 45:
154+
return MapDirection.Northeast;
155+
case 90:
156+
return MapDirection.East;
157+
case 135:
158+
return MapDirection.Southeast;
159+
case 180:
160+
return MapDirection.South;
161+
case 225:
162+
return MapDirection.Southwest;
163+
case 270:
164+
return MapDirection.West;
165+
case 315:
166+
return MapDirection.Northwest;
167+
}
168+
169+
return result;
170+
}
171+
}
172+
}

0 commit comments

Comments
 (0)