-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
306 lines (286 loc) · 13.9 KB
/
Program.cs
File metadata and controls
306 lines (286 loc) · 13.9 KB
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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
using Mogol.Util;
using Raylib_cs;
using System;
using System.Data;
using System.Linq;
namespace Mogol {
internal class Program {
private const bool DEBUG_BUTTONS = false;
public readonly static Viewport ViewWindow = new Viewport( 1600, 900 );
public readonly static Viewport ViewGame = new Viewport( 0, 0, ViewWindow.Width, ViewWindow.Width / 2 );
public readonly static Viewport ViewCtrls = new Viewport( 0, ViewGame.Height, ViewWindow.Width, ViewWindow.Height - ViewGame.Height );
public static int MeasureText( string text ) => Raylib.MeasureText( text, FontHeight );
public static int FontHeight => ViewWindow.Height / 45;
public static int FontWidth => MeasureText( "M" );
public static Wolfram World = new Wolfram( 160, 80, new Rule( 3 ), new Rule( 2, 3 ) );
public static readonly float CELL_WIDTH = ViewGame.Width / World.Width;
public static readonly float CELL_HEIGHT = ViewGame.Height / World.Height;
private static int MouseX => (int)( ( Raylib.GetMouseX( ) - ViewGame.X ) / CELL_WIDTH );
private static int MouseY => (int)( ( Raylib.GetMouseY( ) - ViewGame.Y ) / CELL_HEIGHT );
private static bool Playing = false;
private static bool ShowHelp = false;
private static int BrushSize = 0;
private static int TPS = 10;
private static bool Transparent = false;
private static readonly Color ColorDebugOutline = Color.RED;
//private static readonly Color ColorGameBackground = Color.BLACK;
//private static readonly Color ColorGameCell = Color.RAYWHITE;
private static readonly Color ColorGameCursor = Color.DARKGRAY;
private static readonly Color ColorHelpBackground = new Color( 0, 0, 0, 223 );
private static readonly Color ColorHelpText = Color.LIGHTGRAY;
private static readonly Color ColorHelpTextActive = Color.RAYWHITE;
private static readonly Color ColorCtrlsBackground = Color.DARKGRAY;
private static readonly Color ColorCtrlsText = Color.GRAY;
private static readonly Color ColorCtrlsTextActive = Color.LIGHTGRAY;
private static readonly Color ColorPauseButton = new Color( 127, 127, 127, 223 );
private static void Main( ) {
Raylib.SetConfigFlags( ConfigFlag.FLAG_WINDOW_TRANSPARENT );
Raylib.InitWindow( ViewWindow.Width, ViewWindow.Height, "Mogol" );
Raylib.SetTargetFPS( int.MaxValue );
float tickTimer = 0f;
while ( !Raylib.WindowShouldClose( ) ) {
Update( );
if ( Playing ) {
float tickTime = 1f / TPS;
tickTimer += Raylib.GetFrameTime( );
if ( tickTimer >= tickTime ) {
FixedUpdate( );
tickTimer -= tickTime;
}
}
Raylib.BeginDrawing( );
Draw( );
Raylib.EndDrawing( );
}
Raylib.CloseWindow( );
}
private static bool MouseInGame => !( MouseX < 0 || MouseX >= World.Width || MouseY < 0 || MouseY >= World.Height );
static int maxBrushSize = ( World.Width > World.Height ? World.Width : World.Height ) / 2 - 1;
private static void Update( ) {
ShowHelp ^= Raylib.IsKeyPressed( KeyboardKey.KEY_F1 );
Playing ^= Raylib.IsKeyPressed( KeyboardKey.KEY_SPACE );
BrushSize += Raylib.GetMouseWheelMove( );
BrushSize = BrushSize < 0 ? 0 : BrushSize > maxBrushSize ? maxBrushSize : BrushSize;
if ( Raylib.IsKeyPressed( KeyboardKey.KEY_O ) )
World.Swap( );
if ( Raylib.IsKeyPressed( KeyboardKey.KEY_C ) )
World.Clear( );
if ( Raylib.IsKeyPressed( KeyboardKey.KEY_R ) )
World.Randomize( );
if ( !Playing && Raylib.IsKeyPressed( KeyboardKey.KEY_P ) )
World.Update( );
if ( ShowHelp || !MouseInGame )
return;
int set = Raylib.IsMouseButtonDown( MouseButton.MOUSE_LEFT_BUTTON ) ? 1 : Raylib.IsMouseButtonDown( MouseButton.MOUSE_RIGHT_BUTTON ) ? 0 : -1;
if ( set < 0 )
return;
int from = -(int)( BrushSize / 2f + 0.5f ), to = BrushSize / 2;
for ( int x = from; x <= to; x++ ) {
for ( int y = from; y <= to; y++ ) {
World.Set( MouseX + x, MouseY + y, set );
}
}
}
private static void FixedUpdate( ) => World.Update( );
private static void DrawCell( int x, int y ) => Raylib.DrawRectangle( ViewGame.Left + (int)( x * CELL_WIDTH ), ViewGame.Top + (int)( y * CELL_HEIGHT ), (int)CELL_WIDTH, (int)CELL_HEIGHT, World.GetColor( x, y ) );
private static void Draw( ) {
int mn = 9;
int wd = FontWidth * 16;
// draw game
Raylib.ClearBackground( Transparent ? Color.BLANK : World.Colors[0] );
for ( int x = 0; x < World.Width; x++ ) {
for ( int y = 0; y < World.Height; y++ ) {
if ( World.Get( x, y ) % World.Colors.Length != 0 )
DrawCell( x, y );
}
}
if ( !ShowHelp && ViewGame.Inside( Raylib.GetMouseX( ), Raylib.GetMouseY( ) ) ) {
int cursorX = (int)( (int)( MouseX - BrushSize / 2f ) * CELL_WIDTH );
int cursorY = (int)( (int)( MouseY - BrushSize / 2f ) * CELL_HEIGHT );
int cursorWidth = (int)( ( BrushSize + 1 ) * CELL_WIDTH );
cursorWidth = cursorX + cursorWidth > ViewGame.Width ? ViewGame.Width - cursorX : cursorWidth;
cursorWidth = cursorX < 0 ? cursorWidth + cursorX : cursorWidth;
int cursorHeight = (int)( ( BrushSize + 1 ) * CELL_HEIGHT );
cursorHeight = cursorY + cursorHeight > ViewGame.Height ? ViewGame.Height - cursorY : cursorHeight;
cursorHeight = cursorY < 0 ? cursorHeight + cursorY : cursorHeight;
Raylib.DrawRectangleLines( ViewGame.Left + ( cursorX < 0 ? 0 : cursorX ), ViewGame.Top + ( cursorY < 0 ? 0 : cursorY ), cursorWidth, cursorHeight, ColorGameCursor );
}
if ( !Playing ) {
Raylib.DrawRectangle( ViewGame.Right - 112, 32, ViewGame.Top + 32, 80, ColorPauseButton );
Raylib.DrawRectangle( ViewGame.Right - 64, ViewGame.Top + 32, 32, 80, ColorPauseButton );
}
//draw help
if ( ShowHelp ) {
int j = 1;
int x = ViewGame.Left + mn;
int y = ViewGame.Top + mn;
Raylib.DrawRectangle( ViewGame.Left + mn / 2, ViewGame.Top + mn / 2, FontWidth * 64, mn + mn + FontHeight * 15, ColorHelpBackground );
DrawText( "L/R click to set/unset cells", x, y + FontHeight * ++j, ColorHelpText );
DrawText( $"L/R click \"{TPS}tps\" to increase/decrease tickrate", x, y + FontHeight * ++j, ColorHelpText );
DrawText( "Scrollwheel to change brush size", x, y + FontHeight * ++j, ColorHelpText );
DrawText( "<R> to randomize cells", x, y + FontHeight * ++j, ColorHelpText );
DrawText( "<C> to clear cells", x, y + FontHeight * ++j, ColorHelpText );
DrawText( "<P> to step one generation (if paused)", x, y + FontHeight * ++j, ColorHelpText );
DrawText( "<W> to toggle wrapping", x, y + FontHeight * ++j, ColorHelpText );
DrawText( "<Space> to toggle pause", x, y + FontHeight * ++j, ColorHelpText );
j++;
DrawText( "You can modify the game rules in realtime", x, y + FontHeight * ++j, ColorHelpText );
DrawText( "Click a digit to toggle its state in the rule", x, y + FontHeight * ++j, ColorHelpText );
DrawText( "For more information Google \"2D Cellular Automation\"", x, y + FontHeight * ++j, ColorHelpText );
DrawText( "Please direct feedback to Zeaga#5406", x, y + FontHeight * ++j, ColorHelpText );
j = 0;
x += FontWidth * 36;
DrawText( "Interesting rules (click to set):", x, y + FontHeight * ++j, ColorHelpText );
x += FontWidth * 4;
DrawText( "Amoeba (S1358/B357)", x, y + FontHeight * ++j, ColorHelpTextActive, ( ) => {
World.SurviveRule.Set( 1, 3, 5, 8 );
World.BirthRule.Set( 3, 5, 7 );
} );
DrawText( "Brian's Brain (S-/B2)", x, y + FontHeight * ++j, ColorHelpTextActive, ( ) => {
World.SurviveRule.Set( );
World.BirthRule.Set( 2 );
} );
DrawText( "Conway's Game of Life (S23/B3)", x, y + FontHeight * ++j, ColorHelpTextActive, ( ) => {
World.SurviveRule.Set( 2, 3 );
World.BirthRule.Set( 3 );
} );
DrawText( "Day & Night (S34678/B3678)", x, y + FontHeight * ++j, ColorHelpTextActive, ( ) => {
World.SurviveRule.Set( 3, 4, 6, 7, 8 );
World.BirthRule.Set( 3, 6, 7, 8 );
World.Wrap = true;
} );
DrawText( "High Life (S23/B36)", x, y + FontHeight * ++j, ColorHelpTextActive, ( ) => {
World.SurviveRule.Set( 2, 3 );
World.BirthRule.Set( 3, 6 );
} );
DrawText( "Majority (S5678/B5678)", x, y + FontHeight * ++j, ColorHelpTextActive, ( ) => {
World.SurviveRule.Set( 4, 5, 6, 7, 8 );
World.BirthRule.Set( 5, 6, 7, 8 );
World.Wrap = true;
} );
DrawText( "Walled Cities (S2345/B45678)", x, y + FontHeight * ++j, ColorHelpTextActive, ( ) => {
World.SurviveRule.Set( 2, 3, 4, 5 );
World.BirthRule.Set( 4, 5, 6, 7, 8 );
} );
}
DrawText( "<F1> to toggle help", ViewGame.Left + mn, ViewGame.Top + mn, ColorHelpText );
// draw ctrls
Raylib.DrawRectangle( ViewCtrls.X, ViewCtrls.Y, ViewCtrls.Width, ViewCtrls.Height, ColorCtrlsBackground );
int yi = -1;
int xi = 0;
string text = $"{TPS}tps";
int length = FontWidth * text.Length;
DrawText( $"{ Raylib.GetFPS( )}fps", ViewCtrls.Left + mn + wd * xi, ViewCtrls.Top + mn + FontHeight * ++yi, ( ) => World.Clear( ) );
DrawText( $"{BrushSize + 1}px", ViewCtrls.Left + mn, ViewCtrls.Top + mn + FontHeight * ++yi );
DrawRule( 'S', ViewCtrls.Left + mn, ViewCtrls.Top + mn + FontHeight * ++yi, 1 );
DrawRule( 'B', ViewCtrls.Left + mn, ViewCtrls.Top + mn + FontHeight * ++yi, 0 );
yi = -1;
xi++;
DrawText( "Clear", ViewCtrls.Left + mn + wd * xi, ViewCtrls.Top + mn + FontHeight * ++yi, ( ) => World.Clear( ) );
DrawText( "Randomize", ViewCtrls.Left + mn + wd * xi, ViewCtrls.Top + mn + FontHeight * ++yi, ( ) => World.Randomize( ) );
DrawText( "Step", ViewCtrls.Left + mn + wd * xi, ViewCtrls.Top + mn + FontHeight * ++yi, ( ) => { if ( !Playing ) World.Update( ); } );
DrawText( "Reset", ViewCtrls.Left + mn + wd * xi, ViewCtrls.Top + mn + FontHeight * ++yi, ( ) => {
World.SurviveRule.Set( 2, 3 );
World.BirthRule.Set( 3 );
World.EdgeCell = 0;
Playing = false;
ShowHelp = false;
BrushSize = 0;
TPS = 10;
Transparent = false;
World.Clear( );
} );
yi = -1;
xi++;
DrawText( $"Transparent", ViewCtrls.Left + mn + wd * xi, ViewCtrls.Top + mn + FontHeight * ++yi, Transparent ? ColorCtrlsTextActive : ColorCtrlsText, ( ) => Transparent ^= true );
DrawText( World.Wrap ? "Wrap" : World.EdgeCell == 0 ? "Off" : "On", ViewCtrls.Left + mn + wd * xi, ViewCtrls.Top + mn + FontHeight * ++yi, World.EdgeCell != 0 ? ColorCtrlsTextActive : ColorCtrlsText, ( ) => {
World.EdgeCell++;
if ( World.EdgeCell > 1 )
World.EdgeCell = -1;
}, ( ) => {
World.EdgeCell--;
if ( World.EdgeCell < -1 )
World.EdgeCell = 1;
} );
DrawText( text, ViewCtrls.Left + mn + wd * xi, ViewCtrls.Top + mn + FontHeight * ++yi, ( ) => {
int exp = TPS.ToString( ).Length - 1;
if ( Raylib.IsKeyDown( KeyboardKey.KEY_LEFT_CONTROL ) )
exp--;
if ( Raylib.IsKeyDown( KeyboardKey.KEY_LEFT_SHIFT ) )
exp--;
TPS += (int)Math.Pow( 10, exp > 0 ? exp : 0 );
}, ( ) => {
if ( TPS <= 2 ) {
TPS = 1;
return;
}
int exp = ( TPS - 1 ).ToString( ).Length - 1;
if ( Raylib.IsKeyDown( KeyboardKey.KEY_LEFT_CONTROL ) )
exp--;
if ( Raylib.IsKeyDown( KeyboardKey.KEY_LEFT_SHIFT ) )
exp--;
TPS -= (int)Math.Pow( 10, exp > 0 ? exp : 0 );
} );
}
private static bool DrawText( string text, int x, int y ) => DrawText( text, x, y, ColorCtrlsText );
private static bool DrawText( string text, int x, int y, Action action ) => DrawText( text, x, y, ColorCtrlsText, action );
private static bool DrawText( string text, int x, int y, Action leftAction, Action rightAction ) => DrawText( text, x, y, ColorCtrlsText, leftAction, rightAction );
private static bool DrawText( string text, int x, int y, Color color, Action leftAction, Action rightAction ) {
Raylib.DrawText( text, x, y, FontHeight, color );
bool clicked = AreaClicked( x, y, MeasureText( text ), FontHeight, out MouseButton button );
if ( !clicked )
return false;
if ( button == MouseButton.MOUSE_LEFT_BUTTON )
leftAction( );
else if ( button == MouseButton.MOUSE_RIGHT_BUTTON )
rightAction( );
else
return false;
return true;
}
private static bool DrawText( string text, int x, int y, Color color, Action action ) {
Raylib.DrawText( text, x, y, FontHeight, color );
bool clicked = AreaClicked( x, y, MeasureText( text ), FontHeight );
if ( clicked )
action( );
return clicked;
}
private static bool DrawText( string text, int x, int y, Color color ) {
Raylib.DrawText( text, x, y, FontHeight, color );
return AreaClicked( x, y, MeasureText( text ), FontHeight );
}
private static void DrawRule( char prefix, int posX, int posY, int ruleId ) {
Raylib.DrawText( $"{prefix}:", posX, posY, FontHeight, ColorCtrlsText );
posX += FontWidth * 2;
for ( int i = 0; i <= 8; i++ ) {
int x = posX + i * FontWidth;
Raylib.DrawText( $"{8 - i}", x, posY, FontHeight, World.Rules[ruleId][8 - i] ? ColorCtrlsTextActive : ColorCtrlsText );
if ( AreaClicked( x, posY, FontWidth - 1, FontHeight ) )
World.Rules[ruleId][8 - i] ^= true;
}
}
private static bool AreaClicked( int x, int y, int width, int height, MouseButton button = MouseButton.MOUSE_LEFT_BUTTON ) {
#pragma warning disable CS0162
if ( DEBUG_BUTTONS )
Raylib.DrawRectangleLines( x, y, width, height, ColorDebugOutline );
#pragma warning restore CS0162
return Raylib.IsMouseButtonPressed( button ) && Raylib.GetMouseX( ) >= x && Raylib.GetMouseX( ) < x + width && Raylib.GetMouseY( ) >= y && Raylib.GetMouseY( ) < y + height;
}
private static bool AreaClicked( int x, int y, int width, int height, out MouseButton button ) {
#pragma warning disable CS0162
if ( DEBUG_BUTTONS )
Raylib.DrawRectangleLines( x, y, width, height, ColorDebugOutline );
#pragma warning restore CS0162
button = MouseButton.MOUSE_LEFT_BUTTON;
bool found = false;
foreach ( MouseButton mouseButton in Enum.GetValues( typeof( MouseButton ) ).Cast<MouseButton>( ) ) {
if ( Raylib.IsMouseButtonPressed( mouseButton ) ) {
button = mouseButton;
found = true;
break;
}
}
return found && Raylib.GetMouseX( ) >= x && Raylib.GetMouseX( ) < x + width && Raylib.GetMouseY( ) >= y && Raylib.GetMouseY( ) < y + height;
}
}
}