Redid the world -> screen funcs.
Had to rework these they were wrong initially. Added comments to make it clear what I am doing.
This commit is contained in:
+40
-12
@@ -55,20 +55,41 @@ map_init :: proc() -> Game_Map {
|
||||
}
|
||||
}
|
||||
|
||||
world_to_grid :: proc(pixel_pos: rl.Vector2, camera: rl.Camera2D) -> Grid_Pos {
|
||||
grid_space := rl.GetScreenToWorld2D(pixel_pos, camera)
|
||||
|
||||
// @TODO: I wonder if this floor and casting is expensive, look for a smarter way later
|
||||
pos_x := int(math.floor(grid_space.x))
|
||||
pos_y := int(math.floor(grid_space.y))
|
||||
|
||||
return Grid_Pos{pos_x, pos_y}
|
||||
// requires raw window-space
|
||||
// i.e mouse position clicked in window get that space in screen
|
||||
// returns screen-space pos, scaled to game resolution.
|
||||
window_to_screen :: proc(window_pos: rl.Vector2) -> rl.Vector2 {
|
||||
// window_width / game_width will give the scale back
|
||||
// invert it to scale down back to the render size
|
||||
screen_x := window_pos.x / (WINDOW_WIDTH / GAME_WIDTH)
|
||||
screen_y := window_pos.y / (WINDOW_HEIGHT / GAME_HEIGHT)
|
||||
return rl.Vector2{screen_x, screen_y}
|
||||
}
|
||||
|
||||
// requires screen-space pos i.e mouse pos, scaled from window -> render size
|
||||
// returns world-space position (accounts for camera and its zoom)
|
||||
// remember uesd with raw input coordinates, not something already in world space (like player.pos).
|
||||
screen_to_world :: proc(screen_pos: rl.Vector2, camera: rl.Camera2D) -> rl.Vector2 {
|
||||
return rl.GetScreenToWorld2D(screen_pos, camera)
|
||||
}
|
||||
|
||||
// @TODO: Do I need this?? Maybe for debug view??
|
||||
world_to_screen :: proc(world_pos: rl.Vector2, camera: rl.Camera2D) -> rl.Vector2 {
|
||||
return rl.GetWorldToScreen2D(world_pos, camera)
|
||||
}
|
||||
|
||||
// requires world-space position i.e player.pos, or output of screen_to_world
|
||||
// returns grid col/row not considering camera
|
||||
world_to_grid :: proc(world_pos: rl.Vector2) -> Grid_Pos {
|
||||
col := int(math.floor(world_pos.x / TILE_SIZE))
|
||||
row := int(math.floor(world_pos.y / TILE_SIZE))
|
||||
return Grid_Pos{col, row}
|
||||
}
|
||||
|
||||
// returns world-space position grid pos top-left corner opposite of world_to_grid.
|
||||
grid_to_world :: proc(col: int, row: int) -> rl.Vector2 {
|
||||
world_x := f32(col * TILE_SIZE)
|
||||
world_y := f32(row * TILE_SIZE)
|
||||
|
||||
return rl.Vector2{world_x, world_y}
|
||||
}
|
||||
|
||||
@@ -77,9 +98,16 @@ draw_map :: proc(current_map: Game_Map) {
|
||||
row := index / current_map.width
|
||||
column := index % current_map.width
|
||||
|
||||
color := rl.LIGHTGRAY
|
||||
if value == 1 do color = rl.DARKGRAY
|
||||
if value == 2 do color = rl.GREEN
|
||||
tile := Tile_Type(value)
|
||||
color: rl.Color
|
||||
switch tile {
|
||||
case .FLOOR:
|
||||
color = rl.LIGHTGRAY
|
||||
case .WALL:
|
||||
color = rl.DARKGRAY
|
||||
case .PICKUP:
|
||||
color = rl.GREEN
|
||||
}
|
||||
|
||||
rect := grid_to_world(column, row)
|
||||
rl.DrawRectangle(i32(rect.x), i32(rect.y), TILE_SIZE, TILE_SIZE, color)
|
||||
|
||||
Reference in New Issue
Block a user