-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
716 lines (617 loc) · 24.2 KB
/
Program.cs
File metadata and controls
716 lines (617 loc) · 24.2 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
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
using System;
using System.Collections.Generic;
using System.Numerics;
using System.Text.Json.Serialization;
using Raylib_cs;
using System.Linq;
using System.IO;
using System.Runtime.CompilerServices;
public enum PlayerState
{
Idle,
Walking,
Crafting,
Mining,
Riding
}
public enum MinerState
{
MovingUp,
Mining,
Returning,
Working,
Idle
}
public class Block
{
public static float currentDurabilityMultiplier = 1.0f;
public static int currentYieldBonus = 20;
public int X;
public int Y;
public int Size;
public int Dur;
public int Yield;
public string Mat = "Earth";
public Color Color;
public Vector2 Position => new Vector2(X, Y);
public Vector2 Center => new Vector2(X + Size / 2, Y + Size / 2);
public Block(int x, int y, int size, Color color, float durabilityMultiplier, int yieldBonus)
{
X = x;
Y = y;
Size = size;
Color = color;
Dur = (int)(100 * durabilityMultiplier);
Yield = 1 + yieldBonus;
}
public void Draw()
{
Raylib.DrawRectangle(X, Y, Size, Size, Color);
if (Program.IsBlockInMiningRange(this))
{
for (int i = 0; i < 3; i++)
{
Raylib.DrawRectangleLines(
X - i,
Y - i,
Size + (i * 2),
Size + (i * 2),
Color.Yellow
);
Raylib.DrawText(
Dur.ToString(),
X + Size / 2 - 10,
Y + Size / 2 - 15,
20,
Color.Black
);
Raylib.DrawText(
Mat.ToString(),
X + Size / 2 - 25,
Y + Size / 2 + 5,
15,
Color.Black
);
}
}
}
public bool OverlapsCircle(Vector2 pos, float radius)
{
float left = X;
float right = X + Size;
float top = Y;
float bottom = Y + Size;
float closestX = Math.Clamp(pos.X, left, right);
float closestY = Math.Clamp(pos.Y, top, bottom);
float dx = pos.X - closestX;
float dy = pos.Y - closestY;
return (dx * dx + dy * dy) <= (radius * radius);
}
}
public class Program
{
public static int refWidth = 600;
public static int refHeight = 800;
static int blockSize = 50;
static float nextSetStartY;
static byte lastBaseRed = 100;
static byte lastBaseGreen = 100;
static byte lastBaseBlue = 100;
private static SaveSystem saveSystem = new SaveSystem();
public static List<Block> blocks = new List<Block>();
public static Caravan caravan = null!;
public static Player player = null!;
public static Camera2D camera;
//public static int Earth = 0;
static float caravanY;
static float caravanSpeed = 40f;
static float distanceThreshold = 250f;
public static List<Miner> miners = new List<Miner>();
//public static Crusher crusher;
public static List<Crusher> crushers = new List<Crusher>();
//public static EarthPile earthPile;
public static List<EarthPile> earthPiles = new List<EarthPile>();
public static float minerProgress = 0;
public static float minerThreshold = 10;
public static int[] stoneCounts = new int[Enum.GetValues(typeof(StoneType)).Length];
public static StoneType? DraggedStoneType = null; // Track which stone type is being dragged
public static Forge? forge; // Add forge instance
public static ShovelStation? shovelStation; // Add shovel station instance
public static InfoPanel? infoPanel; // Add InfoPanel instance
// Flag to track if window size is being adjusted
private static bool isAdjustingWindowSize = false;
// Previous window size for comparison
private static int prevWindowWidth = 0;
private static int prevWindowHeight = 0;
// Method to maintain aspect ratio by adjusting window size
public static void MaintainAspectRatio()
{
// Don't process if we're already adjusting the window
if (isAdjustingWindowSize)
return;
int currentWidth = Raylib.GetScreenWidth();
int currentHeight = Raylib.GetScreenHeight();
// Only adjust if the window size has actually changed
if (currentWidth != prevWindowWidth || currentHeight != prevWindowHeight)
{
isAdjustingWindowSize = true;
float targetAspectRatio = (float)refHeight / refWidth; // 800/600 = 4/3
float currentAspectRatio = (float)currentHeight / currentWidth;
// Adjust window size to match the target aspect ratio
if (Math.Abs(currentAspectRatio - targetAspectRatio) > 0.01f) // Small threshold for floating point comparison
{
if (currentAspectRatio > targetAspectRatio)
{
// Window is too tall, adjust height
int newHeight = (int)(currentWidth * targetAspectRatio);
Raylib.SetWindowSize(currentWidth, newHeight);
}
else
{
// Window is too wide, adjust width
int newWidth = (int)(currentHeight / targetAspectRatio);
Raylib.SetWindowSize(newWidth, currentHeight);
}
}
prevWindowWidth = Raylib.GetScreenWidth();
prevWindowHeight = Raylib.GetScreenHeight();
isAdjustingWindowSize = false;
}
}
// This method is no longer needed as we'll use direct window size adjustment
/*
public void LockAspectRatio()
{
float targetAspectRatio = (float)3 / 4;
int currentWidth = Raylib.GetScreenWidth();
int currentHeight = Raylib.GetScreenHeight();
float currentAspectRatio = (float)currentWidth / currentHeight;
if (currentAspectRatio > targetAspectRatio)
{
int newWidth = (int)(currentHeight * targetAspectRatio);
int sideBarWidth = (currentWidth - newWidth) / 2;
Raylib.SetWindowSize(newWidth, currentHeight);
Raylib.SetWindowPosition(sideBarWidth, 0);
}
else
{
int newHeight = (int)(currentWidth / targetAspectRatio);
int topBarHeight = (currentHeight - newHeight) / 2;
Raylib.SetWindowSize(currentWidth, newHeight);
Raylib.SetWindowPosition(0, topBarHeight);
}
}
*/
// New method to calculate virtual mouse position
public static Vector2 GetVirtualMousePosition()
{
// Simply return the actual mouse position
// The camera already handles the scaling
return Raylib.GetMousePosition();
}
public static Vector2 GetMouseWorld()
{
// Get the actual mouse position in screen space
Vector2 mousePos = Raylib.GetMousePosition();
// Convert screen coordinates to world coordinates using the camera
return Raylib.GetScreenToWorld2D(mousePos, camera);
}
public static void CheckAndRemoveDestroyedBlocks()
{
List<Block> blocksToRemove = new List<Block>();
foreach (var block in blocks)
{
if (block.Dur <= 0)
{
blocksToRemove.Add(block);
}
}
foreach (var block in blocksToRemove)
{
blocks.Remove(block);
OnBlockDestroyed();
}
}
public static void OnBlockDestroyed()
{
minerProgress += Random.Shared.Next(1, 10);
}
public static void Main()
{
Raylib.SetConfigFlags(ConfigFlags.ResizableWindow | ConfigFlags.VSyncHint);
Raylib.InitWindow(refWidth, refHeight, "Yearn");
Raylib.SetTargetFPS(60);
Raylib.SetWindowMinSize(300, 400); // Set minimum window size
// Initialize previous window size
prevWindowWidth = refWidth;
prevWindowHeight = refHeight;
stoneCounts = new int[Enum.GetValues(typeof(StoneType)).Length];
Vector2 playerStartPos = new Vector2(refWidth * 0.5f, refHeight * 0.8f);
caravan = new Caravan(refWidth, refHeight);
crushers.Add(new Crusher(caravan, StoneType.Earth, (StoneType)1, 100, 50, 50, 200, 0));
player = new Player(playerStartPos, caravan);
// Initialize the forge
forge = new Forge(caravan);
// Initialize the shovel station
shovelStation = new ShovelStation(caravan);
// Initialize the InfoPanel
infoPanel = InfoPanel.GetInstance();
camera = new Camera2D
{
Target = playerStartPos,
Offset = new Vector2(refWidth / 2, refHeight / 2),
Rotation = 0f,
Zoom = 1f
};
caravanY = caravan.GetY();
saveSystem = new SaveSystem();
if (File.Exists("gamestate.json"))
{
saveSystem.LoadGame();
minerThreshold = 10 * (miners.Count) * 10;
}
else
{
miners.Add(new Miner(
new Vector2(refWidth * 0.3f, refHeight * 0.9f),
caravan,
1,
255f,
Names.GetUniqueName()
));
}
int cols = refWidth / blockSize;
int rows = (refHeight / 2 + 20 * blockSize) / blockSize;
byte baseRed = 100, baseGreen = 100, baseBlue = 100;
for (int y = 0; y < rows; y++)
{
for (int x = 0; x < cols; x++)
{
baseRed = (byte)Math.Clamp(baseRed + Raylib.GetRandomValue(-5, 5), 50, 150);
baseGreen = (byte)Math.Clamp(baseGreen + Raylib.GetRandomValue(-5, 5), 50, 150);
baseBlue = (byte)Math.Clamp(baseBlue + Raylib.GetRandomValue(-5, 5), 50, 150);
Color c = new Color((byte)baseRed, (byte)baseGreen, (byte)baseBlue, (byte)255);
int blockX = x * blockSize;
int blockY = (refHeight / 2) - y * blockSize;
blocks.Add(new Block(
blockX,
blockY,
blockSize,
c,
Block.currentDurabilityMultiplier,
Block.currentYieldBonus));
}
}
nextSetStartY = 400 - (rows - 1) * blockSize;
lastBaseRed = 100;
lastBaseGreen = 100;
lastBaseBlue = 100;
while (!Raylib.WindowShouldClose())
{
// Maintain aspect ratio by adjusting window size
MaintainAspectRatio();
float dt = Raylib.GetFrameTime();
// Scale camera to match window size
camera.Zoom = (float)Raylib.GetScreenWidth() / refWidth;
camera.Offset = new Vector2(Raylib.GetScreenWidth() / 2f, Raylib.GetScreenHeight() / 2f);
saveSystem.Update(dt);
// Create piles for stone types that don't have one yet
for (int i = 0; i < stoneCounts.Length; i++)
{
if (stoneCounts[i] > 0 && !earthPiles.Any(p => p.StoneType == (StoneType)i))
{
earthPiles.Add(new EarthPile(caravan, 50, 50, (StoneType)i));
}
}
// Remove piles for stone types that are empty
earthPiles.RemoveAll(p => Program.stoneCounts[(int)p.StoneType] <= 0);
// Update each earth pile
foreach (var pile in earthPiles)
{
pile.Update(camera);
}
// Update the forge
if (forge != null)
forge.Update(dt);
// Update the shovel station
if (shovelStation != null)
shovelStation.Update(dt);
foreach (var crusher in crushers)
{
crusher.Update(dt);
// Handle crusher clicks to assign miners
Vector2 mouseWorld = GetMouseWorld();
if (Raylib.IsMouseButtonPressed(MouseButton.Left) && crusher.CheckClick(mouseWorld))
{
// Find an available miner (not currently working)
var availableMiner = miners.FirstOrDefault(m => m.CurrentState != MinerState.Working);
if (availableMiner != null)
{
crusher.AssignMiner(availableMiner);
}
}
}
if (minerProgress >= minerThreshold)
{
Vector2 SpawnPos = new Vector2(Random.Shared.Next(0, refWidth), caravan.Y + Random.Shared.Next(-1000, -500));
minerProgress = 0;
minerThreshold = 10 * (miners.Count) * 10;
miners.Add(new Miner(
SpawnPos,
caravan,
Random.Shared.Next(1, 10),
Random.Shared.Next(75, 200),
Names.GetUniqueName()
));
}
if (Raylib.IsKeyPressed(KeyboardKey.E))
{
miners.Add(new Miner(
GetMouseWorld(),
caravan,
Random.Shared.Next(1, 10),
Random.Shared.Next(75, 200),
Names.GetUniqueName()
));
}
if (Raylib.IsKeyPressed(KeyboardKey.C))
{
int newCrusherID = crushers.Count;
int numStoneTypes = Enum.GetValues(typeof(StoneType)).Length;
crushers.Add(new Crusher(
caravan,
(StoneType)(crushers.Count % numStoneTypes),
(StoneType)((crushers.Count + 1) % numStoneTypes),
100,
50,
50,
(newCrusherID * 70) +200,
newCrusherID
));
}
if (Raylib.IsMouseButtonDown(MouseButton.Left))
{
// Use virtual mouse position instead
Vector2 mouseWorld = GetMouseWorld();
player.SetTarget(mouseWorld);
}
// oh my god send help -- update, jippity pulled through
if (Raylib.IsMouseButtonPressed(MouseButton.Left))
{
// Use virtual mouse position instead
Vector2 mouseWorld = GetMouseWorld();
// Reset InfoPanel state at the beginning of each frame
infoPanel.Hide();
// Process miner clicks (each miner is processed twice in separate passes)
foreach (var miner in miners)
{
miner.CheckClick(mouseWorld);
}
foreach (var miner in miners)
{
miner.CheckClick(mouseWorld);
}
Crusher crusherToCreateNextTier = null;
foreach (var crusher in crushers)
{
if (crusher.CheckUpgradeClick(mouseWorld, out int upgradeIndex))
{
switch (upgradeIndex)
{
case 0:
crusher.UpgradeHopper();
break;
case 1:
crusher.UpgradeConversion();
break;
}
}
else if (crusher.CheckNextTierClick(mouseWorld))
{
// Store the crusher that needs to create a next tier
// We'll handle this after the loop to avoid collection modification during enumeration
crusherToCreateNextTier = crusher;
}
else if (crusher.CheckClick(mouseWorld))
{
if (miners.Count > 0)
{
int index = Random.Shared.Next(miners.Count);
miners[index].CurrentState = MinerState.Working;
}
}
}
// Create the next tier crusher if needed (outside the loop to avoid collection modification during enumeration)
if (crusherToCreateNextTier != null)
{
crusherToCreateNextTier.CreateNextTierCrusher();
}
// Process caravan click: if the caravan was clicked, set the player to Crafting and target the caravan center;
// otherwise, set the player to Walking and target the mouse position.
if (caravan.CheckClick(mouseWorld))
{
player.CurrentState = PlayerState.Crafting;
player.IsMoving = false;
player.SetTarget(caravan.Center);
}
else if (forge != null && forge.CheckClick(mouseWorld))
{
// Handle forge click if needed
}
else if (shovelStation != null && shovelStation.CheckClick(mouseWorld))
{
// Handle shovel station click if needed
}
else
{
player.CurrentState = PlayerState.Walking;
player.SetTarget(mouseWorld);
}
}
if (player.Position.Y >= caravan.Y - 100)
{
player.Position = new Vector2(player.Position.X, caravan.Y - 100);
}
if (blocks.Count < 300)
{
GenerateNewBlockSet();
}
if (player.Position.Y < nextSetStartY + 800 || miners.Any(miner => miner.Position.Y < nextSetStartY + 800))
{
GenerateNewBlockSet();
}
player.Update(dt, blocks);
foreach (var m in miners)
{
m.Update(dt, blocks);
}
foreach (var crusher in crushers)
{
crusher.Update(dt);
}
MoveCaravanUpIfNeeded(dt);
UpdateCamera(dt);
// Begin drawing directly to the screen (no render texture needed)
Raylib.BeginDrawing();
Raylib.ClearBackground(Color.RayWhite);
// Begin 2D mode with camera
Raylib.BeginMode2D(camera);
Raylib.DrawCircleV(player.TargetPosition, 5, Color.Red);
Raylib.DrawLineV(player.Position, player.TargetPosition, Color.Red);
foreach (var b in blocks)
{
b.Draw();
}
caravan.Draw();
foreach (var crusher in crushers)
{
crusher.Draw();
}
// Draw the forge
if (forge != null)
forge.Draw();
// Draw the shovel station
if (shovelStation != null)
shovelStation.Draw();
// Draw each earth pile
foreach (var pile in earthPiles)
{
pile.Draw(camera);
}
player.Draw(dt);
foreach (var m in miners)
{
m.Draw(dt);
}
Raylib.EndMode2D();
// Draw UI elements directly to the screen
Raylib.DrawText($"Player Pos: {player.Position.X:F2}, {player.Position.Y:F2}", 10, 10, 20, Color.Black);
Raylib.DrawText($"Player State: {player.CurrentState}", 10, 30, 20, Color.Black);
Raylib.DrawText($"Blocks: {blocks.Count}", 10, 50, 20, Color.Black);
Raylib.DrawText($"Miners: {miners.Count}", 10, 70, 20, Color.Black);
Raylib.DrawText($"Caravan Y: {caravan.Y}", 10, 90, 20, Color.Black);
Raylib.DrawText($"Miner Progress: {minerProgress}", 10, 130, 20, Color.Black);
Raylib.DrawText($"Miner Threshold: {minerThreshold}", 10, 150, 20, Color.Black);
Raylib.DrawText($"CurrentYieldBonus: {Block.currentYieldBonus}", 10, 170, 20, Color.Black);
Raylib.DrawText($"CurrentDurabilityMultiplier: {Block.currentDurabilityMultiplier}", 10, 190, 20, Color.Black);
string scoreText = $"Earth: {stoneCounts[(int)StoneType.Earth]}";
Vector2 scoreSize = Raylib.MeasureTextEx(Raylib.GetFontDefault(), scoreText, 30, 1);
float screenCenterX = Raylib.GetScreenWidth() / 2f;
Raylib.DrawText(
scoreText,
(int)(screenCenterX - scoreSize.X / 2),
Raylib.GetScreenHeight() - 50,
30,
Color.DarkGray
);
// Draw the InfoPanel (should be drawn last so it appears on top of everything else)
if (infoPanel != null)
{
infoPanel.Draw();
}
Raylib.EndDrawing();
}
Raylib.CloseWindow();
}
public static void GenerateNewBlockSet()
{
const int newRows = 28;
const int cols = 12;
byte baseRed = lastBaseRed;
byte baseGreen = lastBaseGreen;
byte baseBlue = lastBaseBlue;
float startY = nextSetStartY - blockSize;
Block.currentDurabilityMultiplier *= 1.1f;
Block.currentYieldBonus += (Block.currentYieldBonus / 10) + 1;
for (int y = 0; y < newRows; y++)
{
for (int x = 0; x < cols; x++)
{
baseRed = (byte)Math.Clamp(baseRed + Raylib.GetRandomValue(-5, 5), 50, 150);
baseGreen = (byte)Math.Clamp(baseGreen + Raylib.GetRandomValue(-5, 5), 50, 150);
baseBlue = (byte)Math.Clamp(baseBlue + Raylib.GetRandomValue(-5, 5), 50, 150);
int blockX = x * blockSize;
int blockY = (int)(startY - y * blockSize);
blocks.Add(new Block(
blockX,
blockY,
blockSize,
new Color((byte)baseRed, (byte)baseGreen, (byte)baseBlue, (byte)255),
Block.currentDurabilityMultiplier,
Block.currentYieldBonus
));
}
}
lastBaseRed = baseRed;
lastBaseGreen = baseGreen;
lastBaseBlue = baseBlue;
nextSetStartY = startY - (newRows - 1) * blockSize;
}
static void UpdateCamera(float dt)
{
float smoothSpeed = 5f * dt;
Vector2 desiredPosition = player.CurrentState == PlayerState.Crafting
? caravan.Center
: player.Position;
if (player.CurrentState != PlayerState.Crafting)
{
float minX = refWidth / 2;
float maxX = refWidth / 2;
desiredPosition.X = Math.Clamp(desiredPosition.X, minX, maxX);
float maxY = caravan.Y - caravan.height * 2;
desiredPosition.Y = Math.Min(desiredPosition.Y, maxY);
}
camera.Target = Vector2.Lerp(camera.Target, desiredPosition, smoothSpeed);
}
static void MoveCaravanUpIfNeeded(float dt)
{
float caravanTopEdge = caravanY - caravan.height;
float minDist = float.MaxValue;
Block closest = null;
foreach (var b in blocks)
{
float blockBottomY = b.Y + b.Size;
if (blockBottomY < caravanTopEdge)
{
float dist = caravanTopEdge - blockBottomY;
if (dist < minDist)
{
minDist = dist;
closest = b;
}
}
}
if (closest != null && minDist > distanceThreshold)
{
caravanY -= caravanSpeed * dt;
}
caravan.SetY(caravanY);
}
public static bool IsBlockInMiningRange(Block block)
{
if (player == null) return false;
Vector2 center = new Vector2(block.X + block.Size * 0.5f, block.Y + block.Size * 0.5f);
float dist = Vector2.Distance(player.Position, center);
return dist <= Player.MINING_RANGE;
}
}