-
Notifications
You must be signed in to change notification settings - Fork 0
/
level_editor.odin
345 lines (289 loc) · 8.5 KB
/
level_editor.odin
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
package main
import "base:builtin"
import "core:c/libc"
import "core:encoding/json"
import "core:fmt"
import "core:os"
import "core:strings"
import rl "vendor:raylib"
Editor_State :: struct {
geometry: [dynamic]rl.Rectangle,
selected_geometry: int,
sidebar_width: i32,
geometry_preview_size: rl.Vector2,
geometry_placement_size: rl.Vector2,
}
Entity :: struct {
position: rl.Vector2,
size: rl.Vector2,
type: EntityType,
}
EntityType :: enum {
WALL,
LEVELCHANGE,
}
Level :: struct {
entities: [dynamic]Entity,
}
level_background: rl.Texture2D
level: Level
editor_state: Editor_State
edit_mode: bool = false
init_editor :: proc() {
editor_state.geometry = make([dynamic]rl.Rectangle)
editor_state.selected_geometry = -1
editor_state.sidebar_width = 200
editor_state.geometry_preview_size = {15, 15}
editor_state.geometry_placement_size = {15, 15}
}
get_cursor_world_pos :: proc(camera: ^rl.Camera2D) -> rl.Vector2 {
mouse_pos := rl.GetMousePosition()
// Get world space position relative to camera
world_pos := rl.GetScreenToWorld2D(mouse_pos, camera^)
// Snap to grid
grid_x :=
f32(int(world_pos.x) / int(editor_state.geometry_placement_size.x)) *
editor_state.geometry_placement_size.x
grid_y :=
f32(int(world_pos.y) / int(editor_state.geometry_placement_size.y)) *
editor_state.geometry_placement_size.y
return rl.Vector2{grid_x, grid_y}
}
draw_editor_sidebar :: proc(camera: ^rl.Camera2D) {
posX := i32(camera.target.x) - rl.GetScreenWidth() / 2
posY := i32(camera.target.y) - rl.GetScreenHeight() / 2
rl.DrawRectangle(0, 0, editor_state.sidebar_width, rl.GetScreenHeight(), rl.LIGHTGRAY)
preview_rect := rl.Rectangle {
x = 20,
y = 20,
width = editor_state.geometry_preview_size.x,
height = editor_state.geometry_preview_size.y,
}
// increment size on wheel scroll
rl.DrawRectangle(
i32(preview_rect.x),
i32(preview_rect.y),
i32(preview_rect.width),
i32(preview_rect.height),
rl.RED,
)
}
handle_editor_input :: proc(camera: ^rl.Camera2D) {
if !edit_mode do return
mouse_pos := rl.GetMousePosition()
mouse_screen_x := mouse_pos.x - f32(editor_state.sidebar_width)
// Handle clicking in the sidebar
if mouse_pos.x < f32(editor_state.sidebar_width) {
if rl.IsMouseButtonPressed(.LEFT) {
if mouse_pos.y >= f32(10) &&
mouse_pos.y < f32(10 + editor_state.geometry_preview_size.y) {
editor_state.selected_geometry = 0
}
}
} else {
// Handle placing rectangles in the world
if editor_state.selected_geometry >= 0 && rl.IsMouseButtonPressed(.LEFT) {
world_pos := get_cursor_world_pos(camera)
new_entity := Entity {
position = world_pos,
size = editor_state.geometry_placement_size,
}
append(&level.entities, new_entity)
}
}
// Handle deletion with right click
if rl.IsMouseButtonPressed(.RIGHT) && mouse_screen_x > 0 {
world_pos := get_cursor_world_pos(camera)
for i := len(level.entities) - 1; i >= 0; i -= 1 {
entity := level.entities[i]
if world_pos.x >= entity.position.x &&
world_pos.x <= entity.position.x + entity.size.x &&
world_pos.y >= entity.position.y &&
world_pos.y <= entity.position.y + entity.size.y {
unordered_remove(&level.entities, i)
break
}
}
}
}
draw_editor_preview :: proc(camera: ^rl.Camera2D) {
if !edit_mode do return
if editor_state.selected_geometry >= 0 {
mouse_pos := rl.GetMousePosition()
if mouse_pos.x >= f32(editor_state.sidebar_width) {
world_pos := get_cursor_world_pos(camera)
// Draw semi-transparent preview rectangle at cursor position
preview_rect := rl.Rectangle {
x = world_pos.x,
y = world_pos.y,
width = editor_state.geometry_preview_size.x,
height = editor_state.geometry_preview_size.y,
}
mouse_wheel := rl.GetMouseWheelMove()
if (mouse_wheel > 0) {
scale_factor := 1.0 + (0.25 * libc.fabsf(mouse_wheel))
if (mouse_wheel < 0) do scale_factor = 1.0 / scale_factor
editor_state.geometry_preview_size.x += 1
editor_state.geometry_preview_size.y += 1
editor_state.geometry_placement_size.x += 1
editor_state.geometry_placement_size.y += 1
} else if (mouse_wheel < 0) {
editor_state.geometry_preview_size.x -= 1
editor_state.geometry_preview_size.y -= 1
editor_state.geometry_placement_size.x -= 1
editor_state.geometry_placement_size.y -= 1
}
// Convert world space rectangle to screen space for drawing
screen_rect := rl.GetWorldToScreen2D({preview_rect.x, preview_rect.y}, camera^)
rl.DrawRectangle(
i32(screen_rect.x),
i32(screen_rect.y),
i32(preview_rect.width),
i32(preview_rect.height),
rl.ColorAlpha(rl.RED, 0.5),
)
}
}
}
draw_level :: proc(camera: ^rl.Camera2D) {
// Draw the background first
rl.DrawTexture(level_background, 0, 0, rl.WHITE)
// Draw grid overlay
// grid_size := rl.Vector2 {
// editor_state.geometry_placement_size.x,
// editor_state.geometry_placement_size.y,
// }
//
// // Calculate grid boundaries based on screen size and camera
// screen_min := rl.GetScreenToWorld2D(rl.Vector2{0, 0}, camera^)
// screen_max := rl.GetScreenToWorld2D(
// rl.Vector2{f32(rl.GetScreenWidth()), f32(rl.GetScreenHeight())},
// camera^,
// )
//
// // Draw vertical lines
// for x := f32(int(screen_min.x / grid_size.x) * int(grid_size.x));
// x < screen_max.x;
// x += grid_size.x {
// start_pos := rl.Vector2{x, screen_min.y}
// end_pos := rl.Vector2{x, screen_max.y}
// rl.DrawLineV(start_pos, end_pos, rl.ColorAlpha(rl.WHITE, 0.5))
// }
//
// // Draw horizontal lines
// for y := f32(int(screen_min.y / grid_size.y) * int(grid_size.y));
// y < screen_max.y;
// y += grid_size.y {
// start_pos := rl.Vector2{screen_min.x, y}
// end_pos := rl.Vector2{screen_max.x, y}
// rl.DrawLineV(start_pos, end_pos, rl.ColorAlpha(rl.WHITE, 0.5))
// }
// Draw entities
for entity in level.entities {
rl.DrawRectangle(
i32(entity.position.x),
i32(entity.position.y),
i32(entity.size.x),
i32(entity.size.y),
rl.BLANK,
)
}
}
write_level :: proc() {
if level_data, err := json.marshal(level); err == nil {
os.write_entire_file("level.json", level_data)
}
delete(level.entities)
}
load_level :: proc(current_level: Levels) {
switch current_level {
case .HOMETOWN:
break
case .CAVE:
break
}
level_background = rl.LoadTexture("./assets/level_1.png")
if level_data, ok := os.read_entire_file("level.json"); ok {
if json.unmarshal(level_data, &level) != nil {
// return if something went wrong
// returns nil when successfully loaded level
rl.DrawText(
"LEVEL FAILED TO LOAD",
rl.GetScreenWidth() / 2,
rl.GetScreenHeight() / 2,
150,
rl.RED,
)
}
// draw level
}
}
editor_mode :: proc(camera: ^rl.Camera2D) {
if (rl.IsKeyPressed(.F3)) {
if camera.zoom == 4.0 {
camera.zoom = 1.0
} else {
camera.zoom = 4.0
}
}
if rl.IsKeyPressed(.F2) {
edit_mode = !edit_mode
}
}
check_collisions :: proc(entities: [dynamic]Entity, player: ^Player) {
player_rect := rl.Rectangle {
x = player.position.x,
y = player.position.y,
width = f32(player.sprite_sheet.width / player.sprite_cols),
height = f32(player.sprite_sheet.height / player.sprite_rows),
}
rl.DrawRectangle(
i32(player_rect.x),
i32(player_rect.y),
i32(player_rect.width),
i32(player_rect.height),
rl.BLANK,
)
for entity in entities {
entity_rect := rl.Rectangle {
x = entity.position.x,
y = entity.position.y,
width = entity.size.x,
height = entity.size.y,
}
rl.DrawRectangle(
i32(entity.position.x),
i32(entity.position.y),
i32(entity.size.x),
i32(entity.size.y),
rl.BLANK,
)
if rl.CheckCollisionRecs(player_rect, entity_rect) {
// Calculate collision depths on each axis
overlap_x := min(
player_rect.x + player_rect.width - entity_rect.x,
entity_rect.x + entity_rect.width - player_rect.x,
)
overlap_y := min(
player_rect.y + player_rect.height - entity_rect.y,
entity_rect.y + entity_rect.height - player_rect.y,
)
// Resolve collision by pushing back the player along the axis of least penetration
if overlap_x < overlap_y {
// Horizontal collision
if player_rect.x < entity_rect.x {
player.position.x = entity_rect.x - player_rect.width
} else {
player.position.x = entity_rect.x + entity_rect.width
}
} else {
// Vertical collision
if player_rect.y < entity_rect.y {
player.position.y = entity_rect.y - player_rect.height
} else {
player.position.y = entity_rect.y + entity_rect.height
}
}
}
}
}