-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProgram.cs
143 lines (111 loc) · 4.17 KB
/
Program.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
// I hereby waive this project under the public domain - see UNLICENSE for details.
/// <summary>
/// Retrieves configuration settings from a TOML file if it exists; otherwise, returns a default configuration.
/// </summary>
/// <param name="file">The name of the configuration file (defaults to "config.toml").</param>
/// <returns>A Config object populated with values from the file, or a default Config instance if the file is not found.</returns>
static Config Settings(string file)
{
var cfgPath = Path.Combine(Tracer.AppDirectory, file);
if (!File.Exists(cfgPath))
{
Tracer.WriteLine("Config file not found. Switching to defaults.");
var config = new Config
{
Width = 600,
Height = 450
};
return config;
}
Tracer.WriteLine($"Discovered config file: {cfgPath}");
var toml = File.ReadAllText(cfgPath);
var model = Toml.ToModel<Config>(toml);
return model;
}
// Update and Draw
unsafe int Game()
{
var config = Settings("config.toml");
InitWindow(config.Width, config.Height, "PlayBark");
var pos = new Vector3(0.2f, 0.4f, 0.2f);
var target = new Vector3(0.0f, 0.0f, 0.0f);
var up = new Vector3(0.0f, 1.0f, 0.0f);
var camera = World3D.Camera(pos, target, up, CameraProjection.Perspective);
var imMap = LoadImage("resources/cubicmap.png");
var cubicmap = LoadTextureFromImage(imMap);
var model = World3D.CubicMap(imMap);
var texture = LoadTexture("resources/cubicmap_atlas.png");
// Set map diffuse texture
Raylib.SetMaterialTexture(ref model, 0, MaterialMapIndex.Albedo, ref texture);
// Get image map data to be used for collission
var mapPixels = LoadImageColors(imMap);
UnloadImage(imMap);
var mapPosition = new Vector3(-16.0f, 0.0f, -8.0f);
var playerPosition = camera.Position;
SetTargetFPS(60);
while (!WindowShouldClose())
{
// Update
var oldCamPos = camera.Position;
UpdateCamera(ref camera, CameraMode.FirstPerson);
var playerPos = new Vector2(camera.Position.X, camera.Position.Z);
var playerRadius = 0.1f;
var playerCellX = (int)(playerPos.X - mapPosition.X + 0.5f);
var playerCellY = (int)(playerPos.Y - mapPosition.Z + 0.5f);
// Out-of-limits security check
if (playerCellX < 0)
{
playerCellX = 0;
}
else if (playerCellX >= cubicmap.Width)
{
playerCellX = cubicmap.Width - 1;
}
if (playerCellY < 0)
{
playerCellY = 0;
}
else if (playerCellY >= cubicmap.Height)
{
playerCellY = cubicmap.Height - 1;
}
for (var y = 0; y < cubicmap.Height; y++)
{
for (var x = 0; y < cubicmap.Width; x++)
{
var mapPixelsData = mapPixels;
var rec = new Rectangle(
mapPosition.X - x - 0.5f + x * 1.0f,
mapPosition.Y - y - 0.5f + x * 1.0f,
1.0f,
1.0f
);
var collision = CheckCollisionCircleRec(playerPos, playerRadius, rec);
if ((mapPixelsData[y * cubicmap.Width + x].R == 255) && collision)
{
// Collision detected, reset camera position
camera.Position = oldCamPos;
}
}
}
BeginDrawing();
ClearBackground(Color.RayWhite);
BeginMode3D(camera);
DrawModel(model, mapPosition, 1.0f, Color.White);
EndMode3D();
DrawTextureEx(cubicmap, new Vector2(GetScreenWidth() - cubicmap.Width * 4 - 20, 20), 0.0f, 4.0f, Color.White);
DrawRectangleLines(GetScreenWidth() - cubicmap.Width * 4 - 20, 20, cubicmap.Width * 4, cubicmap.Height * 4, Color.Green);
// Draw player position radar
DrawRectangle(GetScreenWidth() - cubicmap.Width * 4 - 20 + playerCellX * 4, 20 + playerCellY * 4, 4, 4, Color.Red);
DrawFPS(10, 10);
EndDrawing();
}
UnloadImageColors(mapPixels);
UnloadTexture(cubicmap);
UnloadTexture(texture);
UnloadModel(model);
CloseWindow();
return 0;
}
// Entry point
Game();