make enemy move
This commit is contained in:
@@ -1,6 +1,37 @@
|
||||
package game
|
||||
|
||||
import "core:math"
|
||||
Vec2 :: struct {
|
||||
x, y: f32,
|
||||
}
|
||||
|
||||
grid_to_center :: proc(gx, gy: int) -> Vec2 {
|
||||
return Vec2{f32(gx * TILE_SIZE + TILE_SIZE / 2), f32(gy * TILE_SIZE + TILE_SIZE / 2)}
|
||||
}
|
||||
screen_to_grid :: proc(px, py: f32) -> (int, int) {
|
||||
gx := int(px) / TILE_SIZE
|
||||
gy := int(py) / TILE_SIZE
|
||||
return gx, gy
|
||||
}
|
||||
|
||||
distance :: proc(a, b: Vec2) -> f32 {
|
||||
dx := b.x - a.x
|
||||
dy := b.y - a.y
|
||||
return math.sqrt(dx * dx + dy + dy)
|
||||
}
|
||||
|
||||
move_toward :: proc(pos: ^Vec2, target: Vec2, speed, dt: f32) {
|
||||
dist := distance(pos^, target)
|
||||
if dist < 1.0 do return
|
||||
|
||||
step := speed * dt
|
||||
if step >= dist {
|
||||
pos^ = target
|
||||
} else {
|
||||
dx := target.x - pos.x
|
||||
dy := target.y - pos.y
|
||||
pos.x += dx / dist * step
|
||||
pos.y += dy / dist * step
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user