Terrain and balls

This commit is contained in:
Joseph Ferano 2024-10-03 10:02:11 +07:00
parent 88f893e933
commit 4c94de258a
3 changed files with 264 additions and 0 deletions

3
.gitignore vendored
View File

@ -3,3 +3,6 @@
/floodfill /floodfill
/p5/ /p5/
/gaming-pads /gaming-pads
/balls
/base
/terrain

144
balls.odin Normal file
View File

@ -0,0 +1,144 @@
package balls
import rl "vendor:raylib"
import "core:math"
import "core:math/rand"
import "core:fmt"
Vec2 :: [2]f32
Rect :: rl.Rectangle
Img :: rl.Image
Tex :: rl.Texture
Key :: rl.KeyboardKey
Color :: rl.Color
SCREEN_WIDTH : i32 = 1600
SCREEN_HEIGHT : i32 = 1000
BW := f32(SCREEN_WIDTH)
BH := f32(SCREEN_HEIGHT)
BALL_R :f32 = 15
Ball :: struct {
pos: Vec2,
accel: Vec2,
vel: Vec2
}
GameState :: struct {
dt: f32,
keymap: map[string]Key,
balls: [dynamic]Ball,
}
init_state :: proc() -> ^GameState {
keymap := make(map[string]Key)
keymap["Left"] = .LEFT
keymap["Right"] = .RIGHT
keymap["Up"] = .UP
keymap["Down"] = .DOWN
state := GameState {
keymap = keymap,
}
for i in 0..<30 {
append(&state.balls, Ball {
pos = {
f32(rl.GetRandomValue(i32(BALL_R), i32(BW - BALL_R))),
f32(rl.GetRandomValue(i32(BALL_R), i32(BH - BALL_R)))
}
})
}
cloned := new_clone(state)
return cloned
}
get_rand_angle :: proc(min: i32, max: i32) -> f32 { return f32(rl.GetRandomValue(min, max)) * rl.DEG2RAD }
player_input :: proc(using s: ^GameState) {
if rl.IsKeyDown(s.keymap["Right"]) {
for &ball in balls {
ball.vel.x += 0.3
}
}
if rl.IsKeyDown(s.keymap["Left"]) {
for &ball in balls {
ball.vel.x -= 0.3
}
}
if rl.IsKeyPressed(.SPACE) {
for &ball in balls {
ball.vel.y -= f32(rl.GetRandomValue(8, 12))
}
}
}
gravity := Vec2 {0,0.3}
damping :f32 = 0.7
update :: proc(using s: ^GameState) {
for &ball in balls {
if rl.Vector2LengthSqr(ball.vel) < 1 && ball.pos.y == BH - BALL_R {
ball.vel *= 0
continue
}
// Forces
ball.accel += gravity
ball.vel += ball.accel
ball.accel *= 0
ball.pos += ball.vel
if ball.pos.y > BH - BALL_R {
ball.vel.y *= -damping
ball.pos.y = BH - BALL_R
}
if ball.pos.y < BALL_R {
ball.vel.y *= -damping
ball.pos.y = BALL_R
}
if ball.pos.x > BW - BALL_R {
ball.vel.x *= -damping
ball.pos.x = BW - BALL_R
}
if ball.pos.x < BALL_R {
ball.vel.x *= -damping
ball.pos.x = BALL_R
}
}
}
draw :: proc(using s: ^GameState) {
for ball in balls {
rl.DrawCircleLinesV(ball.pos, BALL_R, rl.BLACK)
}
}
main :: proc() {
rl.SetTraceLogLevel(.ERROR)
rl.InitWindow(SCREEN_WIDTH, SCREEN_HEIGHT, "Boids")
rl.SetTargetFPS(60)
state := init_state()
defer {
free(state)
}
for !rl.WindowShouldClose() {
state.dt = rl.GetFrameTime()
player_input(state)
update(state)
rl.BeginDrawing()
rl.ClearBackground(rl.WHITE)
draw(state)
rl.EndDrawing()
}
rl.CloseWindow()
}

117
terrain.odin Normal file
View File

@ -0,0 +1,117 @@
package terrain
import rl "vendor:raylib"
import rlgl "vendor:raylib/rlgl"
import "core:math"
import "core:math/rand"
import "core:fmt"
Vec2 :: [2]f32
Vec3 :: [3]f32
Rect :: rl.Rectangle
Img :: rl.Image
Tex :: rl.Texture
Key :: rl.KeyboardKey
Color :: rl.Color
SCREEN_WIDTH : i32 = 1600
SCREEN_HEIGHT : i32 = 1000
BW := f32(SCREEN_WIDTH)
BH := f32(SCREEN_HEIGHT)
points :[]Vec3
GameState :: struct {
dt: f32,
keymap: map[string]Key,
cam: rl.Camera3D,
}
init_state :: proc() -> ^GameState {
keymap := make(map[string]Key)
keymap["Left"] = .A
keymap["Right"] = .D
keymap["Up"] = .W
keymap["Down"] = .S
state := GameState {
keymap = keymap,
cam = {
position = { 35.0, 35.0, 35.0 },
target = { 0.0, -5.0, 0.0 },
up = { 0.0, 1.0, 0.0 },
fovy = 15.0,
projection = rl.CameraProjection.PERSPECTIVE,
},
}
rlgl.EnableWireMode()
// rows := 1
// cols := 50
// for r in 0..<rows {
// for c in 0..<cols {
// row := c % 2 == 0 ? c : c + 1
// points[c + r * cols] = {}
// }
// }
points = {
{0,0,0},{0,0,1},
{1,0,0},{1,0,0},{1,0,1},
{2,0,0},{2,0,1},
{3,0,0},{3,0,1},
{4,0,0},{4,0,1},
{5,0,0},{5,0,1},
}
cloned := new_clone(state)
return cloned
}
get_rand_angle :: proc(min: i32, max: i32) -> f32 { return f32(rl.GetRandomValue(min, max)) * rl.DEG2RAD }
player_input :: proc(using s: ^GameState) {
horizontal: f32
vertical: f32
if rl.IsKeyDown(s.keymap["Right"]) { horizontal = 1 }
if rl.IsKeyDown(s.keymap["Left"]) { horizontal = -1 }
if rl.IsKeyDown(s.keymap["Up"]) { vertical = -1 }
if rl.IsKeyDown(s.keymap["Down"]) { vertical = 1 }
if rl.IsMouseButtonPressed(rl.MouseButton.LEFT) {
}
}
update :: proc(using s: ^GameState) {
}
draw :: proc(using s: ^GameState) {
rl.DrawTriangleStrip3D(raw_data(points), 6, rl.BLACK)
}
main :: proc() {
rl.SetTraceLogLevel(.ERROR)
rl.InitWindow(SCREEN_WIDTH, SCREEN_HEIGHT, "Boids")
rl.SetTargetFPS(60)
state := init_state()
defer {
free(state)
}
for !rl.WindowShouldClose() {
state.dt = rl.GetFrameTime()
player_input(state)
update(state)
rl.BeginDrawing()
rl.ClearBackground(rl.WHITE)
rl.BeginMode3D(state.cam)
draw(state)
rl.EndMode3D()
rl.EndDrawing()
}
rl.CloseWindow()
}