mini-refactoring
This commit is contained in:
parent
cd412f8249
commit
ef9caac601
|
@ -1,29 +1,25 @@
|
|||
package main
|
||||
|
||||
import "core:math"
|
||||
import "core:math/rand"
|
||||
import rl "vendor:raylib"
|
||||
|
||||
Vec3 :: [3]f32
|
||||
|
||||
Entity :: struct {
|
||||
position : Vec3,
|
||||
velocity : Vec3,
|
||||
size : Vec3,
|
||||
position: Vec3,
|
||||
velocity: Vec3,
|
||||
size: Vec3,
|
||||
}
|
||||
|
||||
PAL_INT :: [6]u32 {
|
||||
0x272744ff,
|
||||
0x494d7eff,
|
||||
0x8b6d9cff,
|
||||
0xc69fa5ff,
|
||||
0xf2d3abff,
|
||||
0xfbf5efff,
|
||||
}
|
||||
PAL_INT :: [6]u32{0x272744ff, 0x494d7eff, 0x8b6d9cff, 0xc69fa5ff, 0xf2d3abff, 0xfbf5efff}
|
||||
|
||||
PAL := [6]rl.Color{}
|
||||
|
||||
CAMERA : ^rl.Camera3D
|
||||
TEXTURES : Textures
|
||||
CAMERA: ^rl.Camera3D
|
||||
TEXTURES: Textures
|
||||
|
||||
discard_shader: rl.Shader // See shader below. Required for drawing sprites
|
||||
|
||||
main :: proc() {
|
||||
for v, i in PAL_INT {
|
||||
|
@ -38,27 +34,32 @@ main :: proc() {
|
|||
|
||||
discard_shader = rl.LoadShaderFromMemory(nil, alpha_discard)
|
||||
|
||||
// Floor checkerboard
|
||||
img := rl.GenImageChecked(64, 64, 8, 8, PAL[1], PAL[2])
|
||||
defer rl.UnloadImage(img)
|
||||
tex := rl.LoadTextureFromImage(img)
|
||||
defer rl.UnloadTexture(tex)
|
||||
rl.SetTextureFilter(tex, rl.TextureFilter.POINT)
|
||||
|
||||
// Floor mesh
|
||||
plane := rl.GenMeshPlane(32, 32, 16, 16)
|
||||
defer rl.UnloadMesh(plane)
|
||||
mat := rl.LoadMaterialDefault()
|
||||
rl.SetMaterialTexture(&mat, rl.MaterialMapIndex.ALBEDO, tex)
|
||||
|
||||
player_load_resources()
|
||||
defer player_unload_resources()
|
||||
player := player_create()
|
||||
CAMERA = &(player.camera)
|
||||
|
||||
rl.DisableCursor()
|
||||
CAMERA = &(player.camera)
|
||||
|
||||
torches := [10]Torch{}
|
||||
for &v, i in torches {
|
||||
torches[i] = Torch{position = Vec3{f32(i) * 2, 2, 0}}
|
||||
torches[i] = Torch {
|
||||
position = Vec3{rand.float32_range(-10, 10), 2, rand.float32_range(-10, 10)},
|
||||
}
|
||||
}
|
||||
|
||||
rl.DisableCursor()
|
||||
|
||||
for !rl.WindowShouldClose() {
|
||||
using rl
|
||||
|
@ -79,7 +80,6 @@ main :: proc() {
|
|||
torch_draw(&v)
|
||||
}
|
||||
|
||||
|
||||
EndShaderMode()
|
||||
|
||||
EndMode3D()
|
||||
|
@ -87,7 +87,6 @@ main :: proc() {
|
|||
}
|
||||
}
|
||||
|
||||
discard_shader : rl.Shader
|
||||
|
||||
alpha_discard :: `
|
||||
#version 330
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
package main
|
||||
|
||||
import "core:math"
|
||||
import "core:fmt"
|
||||
import "core:math"
|
||||
import rl "vendor:raylib"
|
||||
|
||||
Player :: struct {
|
||||
|
@ -15,15 +15,12 @@ Player :: struct {
|
|||
camera: rl.Camera3D,
|
||||
}
|
||||
|
||||
player_sword_tex : rl.Texture
|
||||
player_sword_mesh : rl.Mesh
|
||||
player_sword_mat : rl.Material
|
||||
player_sword_tex: rl.Texture
|
||||
player_sword_mesh: rl.Mesh
|
||||
player_sword_mat: rl.Material
|
||||
|
||||
player_load_resources :: proc() {
|
||||
player_sword_tex = TEXTURES["sword.png"] // rl.LoadTexture("sword.png")
|
||||
fmt.println(TEXTURES)
|
||||
fmt.println(player_sword_tex)
|
||||
rl.SetTextureFilter(player_sword_tex, rl.TextureFilter.POINT)
|
||||
player_sword_mesh = rl.GenMeshPlane(1.5, 3.6, 1, 1)
|
||||
player_sword_mat = rl.LoadMaterialDefault()
|
||||
player_sword_mat.shader = discard_shader
|
||||
|
@ -32,21 +29,22 @@ player_load_resources :: proc() {
|
|||
|
||||
player_unload_resources :: proc() {
|
||||
rl.UnloadMaterial(player_sword_mat)
|
||||
//rl.UnloadTexture(player_sword_tex)
|
||||
rl.UnloadMesh(player_sword_mesh)
|
||||
}
|
||||
|
||||
player_create :: proc() -> Player {
|
||||
return Player {
|
||||
return(
|
||||
Player {
|
||||
size = {0.5, 2, 0.5},
|
||||
cameraOffset = {0, 1.8, 0},
|
||||
camera = rl.Camera3D{
|
||||
camera = rl.Camera3D {
|
||||
up = {0, 1, 0},
|
||||
fovy = 80,
|
||||
target = {0, 0, 10},
|
||||
projection = rl.CameraProjection.PERSPECTIVE,
|
||||
},
|
||||
}
|
||||
} \
|
||||
)
|
||||
}
|
||||
|
||||
player_update :: proc(using player: ^Player, delta: f32) {
|
||||
|
@ -57,7 +55,7 @@ player_update :: proc(using player: ^Player, delta: f32) {
|
|||
direction -= mouseDelta.x * 0.003
|
||||
yaw -= mouseDelta.y * 0.003
|
||||
|
||||
yaw = Clamp(yaw, -math.PI/2+0.001, math.PI/2-0.001)
|
||||
yaw = Clamp(yaw, -math.PI / 2 + 0.001, math.PI / 2 - 0.001)
|
||||
|
||||
dir_right := direction + math.PI / 2
|
||||
forward := Vec3{math.sin(direction), 0, math.cos(direction)}
|
||||
|
@ -78,13 +76,11 @@ player_update :: proc(using player: ^Player, delta: f32) {
|
|||
velocity.xz = Vector3MoveTowards(velocity, motion * 100, 100 * delta).xz
|
||||
position += velocity * delta
|
||||
|
||||
|
||||
//bob_position = Vec3{math.sin_f32(f32(bob_timer * math.PI * 2 + math.PI/4)), math.sin_f32(f32(bob_timer*math.PI * 4)), 0} * bob_factor * 0.3
|
||||
bob_position = Vec3{0, math.sin_f32(f32(bob_timer*math.PI * 4)), 0} * bob_factor * 0.3
|
||||
bob_position = Vector3RotateByAxisAngle(bob_position, Vec3{0,1,0}, direction)
|
||||
|
||||
// camera positioning, targetting and bobbing
|
||||
bob_position = Vec3{0, math.sin_f32(f32(bob_timer * math.PI * 4)), 0} * bob_factor * 0.3
|
||||
bob_position = Vector3RotateByAxisAngle(bob_position, Vec3{0, 1, 0}, direction)
|
||||
camera.position = position + cameraOffset + bob_position
|
||||
target := Vec3{
|
||||
target := Vec3 {
|
||||
math.sin(direction) * math.cos(yaw),
|
||||
math.sin(yaw),
|
||||
math.cos(direction) * math.cos(yaw),
|
||||
|
@ -95,21 +91,18 @@ player_update :: proc(using player: ^Player, delta: f32) {
|
|||
player_draw :: proc(using player: ^Player) {
|
||||
using rl
|
||||
|
||||
|
||||
rlPushMatrix()
|
||||
offset := position + cameraOffset + (bob_position / 2)
|
||||
rlTranslatef(offset.x, offset.y, offset.z)
|
||||
rlRotatef(math.to_degrees(player.direction), 0, 1, 0)
|
||||
rlRotatef(math.to_degrees(-player.yaw), 1, 0, 0)
|
||||
rlTranslatef(-1, -0.3, 1)
|
||||
rlTranslatef(math.sin_f32(f32(bob_timer * math.PI * 2 + math.PI/4)) * bob_factor * 0.3, 0, 0)
|
||||
rlTranslatef(math.sin_f32(f32(bob_timer * math.PI * 2 + math.PI / 4)) * bob_factor * 0.3, 0, 0)
|
||||
rlRotatef(-90, 1, 0, 0)
|
||||
rlRotatef(-90, 0, 0, 1)
|
||||
rlRotatef(210, 0, 1, 0)
|
||||
rlScalef(0.3, 0.3, 0.3)
|
||||
DrawMesh(player_sword_mesh, player_sword_mat, rl.Matrix(1))
|
||||
// DrawCube({0, 0, 0}, 1,1,1, PAL[4])
|
||||
// DrawCube({0, 0, 0}, 1,1,1, PAL[4])
|
||||
|
||||
rlPopMatrix()
|
||||
}
|
||||
|
|
|
@ -1,18 +1,18 @@
|
|||
package main
|
||||
|
||||
import rl "vendor:raylib"
|
||||
import "core:slice"
|
||||
import "core:path/filepath"
|
||||
import "core:slice"
|
||||
import rl "vendor:raylib"
|
||||
|
||||
Textures :: map[string]rl.Texture
|
||||
tex_filepaths : rl.FilePathList
|
||||
tex_filepaths: rl.FilePathList
|
||||
|
||||
load_textures :: proc() -> Textures {
|
||||
result : Textures = make(map[string]rl.Texture)
|
||||
result: Textures = make(map[string]rl.Texture)
|
||||
|
||||
tex_filepaths := rl.LoadDirectoryFiles("./assets")
|
||||
|
||||
for i : u32 = 0; i < tex_filepaths.count; i += 1 {
|
||||
for i: u32 = 0; i < tex_filepaths.count; i += 1 {
|
||||
item := tex_filepaths.paths[i]
|
||||
name := filepath.base(string(item))
|
||||
result[name] = rl.LoadTexture(item)
|
||||
|
|
Loading…
Reference in New Issue