Core: Polymorphism works better then expected hmm...
This commit is contained in:
@@ -1,5 +1,3 @@
|
||||
/*@language_refactor: Remove tuples and simplify, Make var unpacking general*/
|
||||
|
||||
/*@language_todo: make polymorphism checking more robust*/
|
||||
|
||||
/*@language_todo: labeled block
|
||||
@@ -25,7 +23,7 @@ thing := 1
|
||||
*/
|
||||
|
||||
#import "raylib.core"
|
||||
#load "array.core"
|
||||
A :: #load "array.core"
|
||||
MAP :: #load "map.core"
|
||||
sqrtf :: #foreign (value: F32): F32
|
||||
|
||||
@@ -47,12 +45,12 @@ Mode := 0
|
||||
RectX :: 16
|
||||
RectY :: 16
|
||||
Dt: F32
|
||||
ANI_SetTiles: Array(ANI_SetTile)
|
||||
ANI_SetTiles: A.Array(ANI_SetTile)
|
||||
|
||||
MouseSelecting := false
|
||||
MouseSelectionPivot: Vector2
|
||||
MouseSelectionBox: Rectangle
|
||||
MouseSelectedActors: Array(*MAP.Actor) // @todo: ids
|
||||
MouseSelectedActors: A.Array(*MAP.Actor) // @todo: ids
|
||||
|
||||
main :: (): int
|
||||
MAP.Init()
|
||||
@@ -88,7 +86,6 @@ main :: (): int
|
||||
MouseP = GetMousePosition()
|
||||
Dt = GetFrameTime()
|
||||
map := &MAP.CurrentMap
|
||||
ai := GetIndex(&map.actors, map.actors.data)
|
||||
|
||||
MouseSelecting = false
|
||||
if IsMouseButtonDown(MOUSE_BUTTON_LEFT)
|
||||
@@ -144,9 +141,9 @@ main :: (): int
|
||||
|
||||
if Mode == 0
|
||||
if colliding && IsMouseButtonDown(MOUSE_BUTTON_LEFT)
|
||||
Add(&ANI_SetTiles, {true, {x,y}})
|
||||
A.Add(&ANI_SetTiles, {true, {x,y}})
|
||||
if colliding && IsMouseButtonDown(MOUSE_BUTTON_RIGHT)
|
||||
Add(&ANI_SetTiles, {false, {x,y}})
|
||||
A.Add(&ANI_SetTiles, {false, {x,y}})
|
||||
if colliding == true ;; color = {a = 100}
|
||||
|
||||
DrawRectangleRec(r2, color)
|
||||
@@ -181,7 +178,7 @@ main :: (): int
|
||||
|
||||
tile_it.t += Dt*8
|
||||
if remove
|
||||
UnorderedRemove(&ANI_SetTiles, tile_it)
|
||||
A.UnorderedRemove(&ANI_SetTiles, tile_it)
|
||||
tile_i -= 1
|
||||
|
||||
for i := 0, i < map.actors.len, i += 1
|
||||
@@ -232,7 +229,7 @@ main :: (): int
|
||||
MAP.SetTargetP(actor_it, p)
|
||||
|
||||
if MouseSelecting
|
||||
Reset(&MouseSelectedActors)
|
||||
A.Reset(&MouseSelectedActors)
|
||||
COLOR_SelectionBox := GREEN
|
||||
COLOR_SelectionBox.a = 255/2
|
||||
|
||||
@@ -241,7 +238,7 @@ main :: (): int
|
||||
actor_box := MAP.Rect(actor_it.p)
|
||||
|
||||
if CheckCollisionRecs(actor_box, MouseSelectionBox)
|
||||
Add(&MouseSelectedActors, actor_it)
|
||||
A.Add(&MouseSelectedActors, actor_it)
|
||||
|
||||
DrawRectangleRec(MouseSelectionBox, COLOR_SelectionBox)
|
||||
|
||||
|
||||
@@ -12,17 +12,17 @@ Map :: struct
|
||||
data: *Tile
|
||||
x: int
|
||||
y: int
|
||||
actors: Array(Actor)
|
||||
actors: A.Array(Actor)
|
||||
|
||||
Actor :: struct
|
||||
p: V2I
|
||||
target_p: V2I
|
||||
map: *Map
|
||||
|
||||
open_paths: Array(Path)
|
||||
close_paths: Array(Path)
|
||||
tiles_visited: Array(V2I)
|
||||
history: Array(Path)
|
||||
open_paths: A.Array(Path)
|
||||
close_paths: A.Array(Path)
|
||||
tiles_visited: A.Array(V2I)
|
||||
history: A.Array(Path)
|
||||
|
||||
Path :: struct
|
||||
value_to_sort_by: int // distance from target
|
||||
@@ -44,10 +44,10 @@ ScreenToMap :: (p: Vector2): V2I
|
||||
return result
|
||||
|
||||
AddActor :: (map: *Map, p: V2I): *Actor
|
||||
Add(&map.actors, {p, p, map})
|
||||
A.Add(&map.actors, {p, p, map})
|
||||
Assert(map.data[p.x + p.y * map.x] == 0)
|
||||
map.data[p.x + p.y * map.x] |= TILE_ACTOR_IS_STANDING
|
||||
actor := GetLast(&CurrentMap.actors)
|
||||
actor := A.GetLast(&CurrentMap.actors)
|
||||
return actor
|
||||
|
||||
ActorSetP :: (actor: *Actor, p: V2I)
|
||||
@@ -62,17 +62,17 @@ ActorSetP :: (actor: *Actor, p: V2I)
|
||||
*new_tile |= TILE_ACTOR_IS_STANDING
|
||||
actor.p = p
|
||||
|
||||
Reset(&actor.tiles_visited)
|
||||
Reset(&actor.history)
|
||||
Reset(&actor.open_paths)
|
||||
Reset(&actor.close_paths)
|
||||
A.Reset(&actor.tiles_visited)
|
||||
A.Reset(&actor.history)
|
||||
A.Reset(&actor.open_paths)
|
||||
A.Reset(&actor.close_paths)
|
||||
|
||||
SetTargetP :: (s: *Actor, p: V2I)
|
||||
s.target_p = p
|
||||
Reset(&s.tiles_visited)
|
||||
Reset(&s.history)
|
||||
Reset(&s.open_paths)
|
||||
Reset(&s.close_paths)
|
||||
A.Reset(&s.tiles_visited)
|
||||
A.Reset(&s.history)
|
||||
A.Reset(&s.open_paths)
|
||||
A.Reset(&s.close_paths)
|
||||
|
||||
GetRandomP :: (m: *Map): V2I
|
||||
result: V2I = {GetRandomValue(0, CurrentMap.x - 1), GetRandomValue(0, CurrentMap.y - 1)}
|
||||
@@ -124,7 +124,7 @@ InsertOpenPath :: (s: *Actor, p: V2I, came_from: V2I, ignore_blocks: bool = fals
|
||||
dx := s.target_p.x - p.x
|
||||
dy := s.target_p.y - p.y
|
||||
d := dx*dx + dy*dy
|
||||
InsertSortedDecreasing(&s.open_paths, {d, p, came_from})
|
||||
A.InsertSortedDecreasing(&s.open_paths, {d, p, came_from})
|
||||
|
||||
GetCloseP :: (s: *Actor, p: V2I): *Path
|
||||
for i := 0, i < s.close_paths.len, i += 1
|
||||
@@ -136,25 +136,25 @@ GetCloseP :: (s: *Actor, p: V2I): *Path
|
||||
|
||||
RecomputeHistory :: (s: *Actor)
|
||||
if s.close_paths.len > 1
|
||||
Reset(&s.history)
|
||||
it := GetLast(&s.close_paths)
|
||||
Add(&s.history, *it)
|
||||
A.Reset(&s.history)
|
||||
it := A.GetLast(&s.close_paths)
|
||||
A.Add(&s.history, *it)
|
||||
for i := 0,,i += 1
|
||||
if it.p.x == s.p.x && it.p.y == s.p.y ;; break
|
||||
if i > 512
|
||||
Reset(&s.history)
|
||||
A.Reset(&s.history)
|
||||
break
|
||||
it = GetCloseP(s, it.came_from)
|
||||
Add(&s.history, *it)
|
||||
Pop(&s.history)
|
||||
A.Add(&s.history, *it)
|
||||
A.Pop(&s.history)
|
||||
|
||||
MoveTowardsTarget :: (s: *Actor)
|
||||
tile := s.map.data + s.p.x + s.p.y * s.map.x
|
||||
if s.history.len > 0
|
||||
step := Pop(&s.history)
|
||||
step := A.Pop(&s.history)
|
||||
new_tile := s.map.data + step.p.x + step.p.y * s.map.x
|
||||
if *new_tile == 0
|
||||
Add(&s.tiles_visited, s.p)
|
||||
A.Add(&s.tiles_visited, s.p)
|
||||
s.p = step.p
|
||||
*tile &= ~TILE_ACTOR_IS_STANDING
|
||||
*new_tile |= TILE_ACTOR_IS_STANDING
|
||||
@@ -167,9 +167,9 @@ PathFindUpdate :: (map: *Map)
|
||||
|
||||
tile := s.map.data[it.p.x + it.p.y * s.map.x]
|
||||
if tile != 0
|
||||
Reset(&s.open_paths)
|
||||
Reset(&s.close_paths)
|
||||
Reset(&s.history)
|
||||
A.Reset(&s.open_paths)
|
||||
A.Reset(&s.close_paths)
|
||||
A.Reset(&s.history)
|
||||
break
|
||||
|
||||
PathFind(s)
|
||||
@@ -178,23 +178,23 @@ PathFindStep :: (s: *Actor, compute_history: bool = true): bool
|
||||
if s.open_paths.len == 0
|
||||
// Reset if we didnt find solution
|
||||
if s.close_paths.len != 0
|
||||
last := GetLast(&s.close_paths)
|
||||
last := A.GetLast(&s.close_paths)
|
||||
reached_target := last.p.x == s.target_p.x && last.p.y == s.target_p.y
|
||||
if reached_target == false
|
||||
Reset(&s.open_paths)
|
||||
Reset(&s.close_paths)
|
||||
Reset(&s.history)
|
||||
A.Reset(&s.open_paths)
|
||||
A.Reset(&s.close_paths)
|
||||
A.Reset(&s.history)
|
||||
|
||||
InsertOpenPath(s, s.p, s.p, ignore_blocks = true)
|
||||
|
||||
if s.close_paths.len != 0
|
||||
last := GetLast(&s.close_paths)
|
||||
last := A.GetLast(&s.close_paths)
|
||||
reached_target := last.p.x == s.target_p.x && last.p.y == s.target_p.y
|
||||
if reached_target
|
||||
return true
|
||||
|
||||
it := Pop(&s.open_paths)
|
||||
Add(&s.close_paths, it)
|
||||
it := A.Pop(&s.open_paths)
|
||||
A.Add(&s.close_paths, it)
|
||||
|
||||
|
||||
for y := -1, y <= 1, y += 1
|
||||
|
||||
Reference in New Issue
Block a user