-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
158 lines (127 loc) · 5.2 KB
/
Program.cs
File metadata and controls
158 lines (127 loc) · 5.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
using System.Numerics;
using Raylib_cs;
namespace RaylibCsExamples.Community.Core.Camera2dDemo;
internal sealed class Program
{
private static void Main(string[] args)
{
const int MaxBuildings = 100;
const int screenWidth = 800;
const int screenHeight = 450;
Raylib.InitWindow(screenWidth, screenHeight, "raylib [core] example - 2d camera");
var player = new Rectangle(400, 280, 40, 40);
var buildings = new (Rectangle rectangle, Color color)[MaxBuildings];
var spacing = 0;
for (var i = 0; i < MaxBuildings; i++)
{
var buildingHeight = Random.Shared.Next(50, 200);
var buildingWidth = Random.Shared.Next(100, 800);
buildings[i] = buildings[i] with
{
rectangle = new(
x: -6000 + spacing,
y: screenHeight - 130 - buildingHeight,
width: buildingWidth,
height: buildingHeight
),
color = new(
Random.Shared.Next(200, 240),
Random.Shared.Next(200, 240),
Random.Shared.Next(200, 240)
)
};
spacing += buildingWidth;
}
var camera = new Camera2D()
{
Target = new(player.X + 20, player.Y + 20),
Offset = new(screenWidth / 2, screenHeight / 2),
Rotation = 0.0f,
Zoom = 1.0f
};
Raylib.SetTargetFPS(60);
while (!Raylib.WindowShouldClose())
{
// Update
//----------------------------------------------------------------------------------
// Player movement
if (Raylib.IsKeyDown(KeyboardKey.Right))
{
player.X += 2;
}
else if (Raylib.IsKeyDown(KeyboardKey.Left))
{
player.X -= 2;
}
// Camera3D target follows player
camera.Target = new Vector2(player.X + 20, player.Y + 20);
// Camera3D rotation controls
if (Raylib.IsKeyDown(KeyboardKey.A))
{
camera.Rotation--;
}
else if (Raylib.IsKeyDown(KeyboardKey.S))
{
camera.Rotation++;
}
// Limit camera rotation to 80 degrees (-40 to 40)
if (camera.Rotation > 40)
{
camera.Rotation = 40;
}
else if (camera.Rotation < -40)
{
camera.Rotation = -40;
}
// Camera3D zoom controls
camera.Zoom += (float)Raylib.GetMouseWheelMove() * 0.05f;
if (camera.Zoom > 3.0f)
{
camera.Zoom = 3.0f;
}
else if (camera.Zoom < 0.1f)
{
camera.Zoom = 0.1f;
}
// Camera3D reset (zoom and rotation)
if (Raylib.IsKeyPressed(KeyboardKey.R))
{
camera.Zoom = 1.0f;
camera.Rotation = 0.0f;
}
Raylib.BeginDrawing();
Raylib.BeginMode2D(camera);
Raylib.ClearBackground(Color.RayWhite);
Raylib.DrawRectangle(-6000, 320, 13000, 8000, Color.DarkGray);
foreach (var (rectangle, color) in buildings)
{
Raylib.DrawRectangleRec(rectangle, color);
}
Raylib.DrawRectangleRec(player, Color.Red);
Raylib.DrawRectangle((int)camera.Target.X, -500, 1, screenHeight * 4, Color.Green);
Raylib.DrawLine(
-screenWidth * 10,
(int)camera.Target.Y,
screenWidth * 10,
(int)camera.Target.Y,
Color.Green
);
Raylib.EndMode2D();
Raylib.DrawText("SCREEN AREA", 640, 10, 20, Color.Red);
Raylib.DrawRectangle(posX: 0, posY: 0, width: screenWidth, height: 5, color: Color.Red);
Raylib.DrawRectangle(posX: 0, posY: 5, width: 5, height: screenHeight - 10, color: Color.Red);
Raylib.DrawRectangle(posX: screenWidth - 5, posY: 5, width: 5, height: screenHeight - 10, color: Color.Red);
Raylib.DrawRectangle(posX: 0, posY: screenHeight - 5, width: screenWidth, height: 5, color: Color.Red);
Raylib.DrawRectangle(posX: 10, posY: 10, width: 250, height: 113, color: Raylib.ColorAlpha(Color.SkyBlue, 0.5f));
Raylib.DrawRectangleLines(10, 10, 250, 113, Color.Blue);
Raylib.DrawText(text: "Free 2d camera controls:", posX: 20, posY: 20, fontSize: 10, color: Color.Black);
Raylib.DrawText(text: "- Right/Left to move Offset", posX: 40, posY: 40, fontSize: 10, color: Color.DarkGray);
Raylib.DrawText(text: "- Mouse Wheel to Zoom in-out", posX: 40, posY: 60, fontSize: 10, color: Color.DarkGray);
Raylib.DrawText(text: "- A / S to Rotate", posX: 40, posY: 80, fontSize: 10, color: Color.DarkGray);
Raylib.DrawText(text: "- R to reset Zoom and Rotation", posX: 40, posY: 100, fontSize: 10, color: Color.DarkGray);
Raylib.EndDrawing();
}
Raylib.CloseWindow();
}
}