-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMain.cs
153 lines (120 loc) · 3.72 KB
/
Main.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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
using Godot;
using System;
public class Main : Node2D
{
[Export]
int gridWidth = 188;
[Export]
int gridHeight = 188;
float weight = 0.6f;
int speed = 80;
float resetTime = 5f;
float elapsedTime = 0f;
Point currentPoint = new Point(89, 89);
Point previousPoint = new Point(0, 0);
int numberOfPoints = 5;
Point[] points;
TileMap map;
Random rand;
TextEdit pointsEdit;
TextEdit weightEdit;
CheckBox ignoreLastCB;
bool isDrawing = true;
bool ignoreLast = true;
public override void _Ready()
{
map = GetNode<TileMap>("TileMap");
pointsEdit = GetNode<TextEdit>("Control/TextureRect/V1/H1/PointsEdit");
weightEdit = GetNode<TextEdit>("Control/TextureRect/V2/H2/WeightEdit");
ignoreLastCB = GetNode<CheckBox>("Control/TextureRect/H3/IgnoreLast");
rand = new Random();
ResetAndRandomize();
}
// // Called every frame. 'delta' is the elapsed time since the previous frame.
public override void _Process(float delta)
{
elapsedTime += delta;
if (Input.IsActionJustPressed("reset"))
{
ResetAndRandomize();
}
if (elapsedTime > resetTime)
{
isDrawing = !isDrawing;
elapsedTime -= resetTime;
}
if (isDrawing)
{
DrawShape(1);
}
else DrawShape(0);
}
public void DrawShape(int cellValue)
{
for (int i = 0; i < speed; i++)
{
int next = rand.Next(points.Length);
if (previousPoint != points[next] || ignoreLast)
{
SetHalfWayPointToPoint(points[next]);
}
previousPoint = points[next];
map.SetCell(currentPoint.xPos, currentPoint.yPos, cellValue);
}
}
public void ResetAndRandomize()
{
numberOfPoints = pointsEdit.Text.ToInt();
weight = weightEdit.Text.ToFloat();
ignoreLast = ignoreLastCB.Pressed;
currentPoint = new Point(gridWidth / 2, gridHeight / 2);
previousPoint = new Point(0, 0);
points = new Point[numberOfPoints];
isDrawing = true;
elapsedTime = 0;
for (int x = 0; x < gridWidth; x++)
{
for (int y = 0; y < gridHeight; y++)
{
map.SetCell(x, y, 0);
}
}
float startAngle = (float)GD.RandRange(0, 360);
float startAngleInRadians = Mathf.Deg2Rad(startAngle);
float angle = 360 / points.Length;
float angleInRadians = Mathf.Deg2Rad(angle);
int length = 80;
int xStart = gridWidth / 2;
int yStart = gridHeight / 2;
for (int i = 0; i < points.Length; i++)
{
int myX = xStart + (int)(length * Mathf.Cos(startAngle + (angleInRadians * i)));
int myY = yStart + (int)(length * Mathf.Sin(startAngle + (angleInRadians * i)));
points[i] = new Point(myX, myY);
map.SetCell(points[i].xPos, points[i].yPos, 2);
}
}
void SetHalfWayPointToPoint(Point dest)
{
int middleX = Mathf.FloorToInt(Mathf.Lerp(currentPoint.xPos, dest.xPos, weight));
int middleY = Mathf.FloorToInt(Mathf.Lerp(currentPoint.yPos, dest.yPos, weight));
currentPoint.xPos = middleX;
currentPoint.yPos = middleY;
}
Point GetRandomPoint()
{
int x = Mathf.FloorToInt((float)GD.RandRange(0, gridWidth));
int y = Mathf.FloorToInt((float)GD.RandRange(0, gridHeight));
return new Point(x, y);
}
class Point
{
public int xPos;
public int yPos;
public Point(int x, int y)
{
xPos = x;
yPos = y;
}
}
}