3D only
This commit is contained in:
+61
-28
@@ -1,6 +1,5 @@
|
||||
package main
|
||||
|
||||
import "core:fmt"
|
||||
import rl "vendor:raylib"
|
||||
|
||||
Graphics :: union {
|
||||
@@ -44,11 +43,11 @@ GfxTexture :: struct {}
|
||||
|
||||
GfxAnimatedSprite :: struct {
|
||||
pos: rl.Vector2,
|
||||
rect: rl.Rectangle,
|
||||
tex: ^rl.Texture2D,
|
||||
frame: u32,
|
||||
atlas: ^Atlas,
|
||||
frame: i32,
|
||||
speed: f32,
|
||||
current_time: f32,
|
||||
update: proc(g: ^GfxAnimatedSprite, dt: f32),
|
||||
}
|
||||
|
||||
PlayerTexFiles :: enum {
|
||||
@@ -60,33 +59,65 @@ PlayerTexFiles :: enum {
|
||||
ATTACK,
|
||||
}
|
||||
|
||||
|
||||
GraphicsAssetStore :: struct {
|
||||
playerTex: map[PlayerTexFiles]rl.Texture2D,
|
||||
Atlas :: struct {
|
||||
tex: rl.Texture2D,
|
||||
frame_size: rl.Vector2,
|
||||
cols: i32,
|
||||
rows: i32,
|
||||
frames: i32,
|
||||
get_offset: proc(a: ^Atlas, f: i32) -> rl.Vector2,
|
||||
}
|
||||
|
||||
gfx_get :: proc(g: ^GraphicsAssetStore, f: PlayerTexFiles) -> ^rl.Texture2D {
|
||||
|
||||
GraphicsAssetStore :: struct {
|
||||
playerTex: map[PlayerTexFiles]Atlas,
|
||||
}
|
||||
|
||||
player_atlas_get :: proc(g: ^GraphicsAssetStore, f: PlayerTexFiles) -> ^Atlas {
|
||||
return &g.playerTex[f]
|
||||
}
|
||||
|
||||
gfx_init :: proc() -> GraphicsAssetStore {
|
||||
st := GraphicsAssetStore{}
|
||||
st.playerTex = make(map[PlayerTexFiles]rl.Texture2D)
|
||||
|
||||
st.playerTex[.IDLE] = rl.LoadTexture("assets/3rdparty/player_idle.png")
|
||||
st.playerTex = make(map[PlayerTexFiles]Atlas)
|
||||
|
||||
st.playerTex[.IDLE] = init_atlas(
|
||||
texture_path = "assets/3rdparty/player_idle.png",
|
||||
size = rl.Vector2{64, 64},
|
||||
)
|
||||
return st
|
||||
}
|
||||
|
||||
init_atlas :: proc(texture_path: cstring, size: rl.Vector2) -> Atlas {
|
||||
atlas := Atlas {
|
||||
tex = rl.LoadTexture(texture_path),
|
||||
frame_size = size,
|
||||
get_offset = get_offset,
|
||||
}
|
||||
|
||||
atlas.rows = atlas.tex.height / (i32(size.y))
|
||||
atlas.cols = atlas.tex.width / i32(size.x)
|
||||
atlas.frames = atlas.cols * atlas.rows
|
||||
return atlas
|
||||
}
|
||||
|
||||
get_offset :: proc(a: ^Atlas, f: i32) -> rl.Vector2 {
|
||||
|
||||
col := f % a.cols
|
||||
row := f / a.cols
|
||||
|
||||
return rl.Vector2{f32(col) * a.frame_size.x, f32(row) * a.frame_size.y}
|
||||
}
|
||||
|
||||
gfx_free :: proc(g: GraphicsAssetStore) {
|
||||
for _, tex in g.playerTex {
|
||||
rl.UnloadTexture(tex)
|
||||
rl.UnloadTexture(tex.tex)
|
||||
}
|
||||
delete(g.playerTex)
|
||||
}
|
||||
|
||||
gfx_draw :: proc(pos: rl.Vector2, g: ^Graphics) {
|
||||
#partial switch &v in g {
|
||||
#partial switch v in g {
|
||||
case GfxCircle:
|
||||
rl.DrawCircleV(pos + v.pos, v.radius, v.color)
|
||||
case GfxRect:
|
||||
@@ -96,29 +127,31 @@ gfx_draw :: proc(pos: rl.Vector2, g: ^Graphics) {
|
||||
case GfxCircleLines:
|
||||
rl.DrawCircleLinesV(pos + v.pos, v.radius, v.color)
|
||||
case GfxAnimatedSprite:
|
||||
fmt.println(pos.x, pos.y)
|
||||
//TODO: Count from L to R row-wize
|
||||
v.rect.x = v.rect.width * f32(v.frame)
|
||||
v.rect.y = v.rect.height * f32(v.frame)
|
||||
frame_offset := v.atlas->get_offset(v.frame)
|
||||
rl.DrawTexturePro(
|
||||
v.tex^,
|
||||
v.rect,
|
||||
v.atlas^.tex,
|
||||
rl.Rectangle {
|
||||
pos.x - v.rect.width * 3 / 2,
|
||||
pos.y - v.rect.height * 3 / 2,
|
||||
v.rect.width * 3,
|
||||
v.rect.height * 3,
|
||||
frame_offset.x,
|
||||
frame_offset.y,
|
||||
v.atlas.frame_size.x,
|
||||
v.atlas.frame_size.y,
|
||||
},
|
||||
rl.Rectangle {
|
||||
pos.x - v.atlas.frame_size.x * 3 / 2,
|
||||
pos.y - v.atlas.frame_size.y * 3 / 2,
|
||||
v.atlas.frame_size.x * 3,
|
||||
v.atlas.frame_size.y * 3,
|
||||
},
|
||||
rl.Vector2{0, 0},
|
||||
0,
|
||||
rl.WHITE,
|
||||
)
|
||||
v.current_time += rl.GetFrameTime()
|
||||
if v.current_time > v.speed {
|
||||
v.current_time = v.current_time - v.speed
|
||||
v.frame += 1
|
||||
}
|
||||
// v.current_time += rl.GetFrameTime()
|
||||
// if v.current_time > v.speed {
|
||||
// v.current_time = v.current_time - v.speed
|
||||
// v.frame += 1
|
||||
// }
|
||||
|
||||
// rl.DrawTextureRec(v.tex, v.rect, rl.Vector2{pos.x, pos.y}, rl.Color{})
|
||||
}
|
||||
}
|
||||
|
||||
+1
-1
@@ -4,7 +4,7 @@ import rl "vendor:raylib"
|
||||
|
||||
SCREEN_W :: 1280
|
||||
SCREEN_H :: 720
|
||||
TARGET_FPS :: 60
|
||||
TARGET_FPS :: 120 // test edit
|
||||
|
||||
main :: proc() {
|
||||
rl.InitWindow(SCREEN_W, SCREEN_H, "ARPG Demo")
|
||||
|
||||
+19
-3
@@ -66,10 +66,10 @@ make_player :: proc(gfx: ^GraphicsAssetStore) -> Entity {
|
||||
0 = GfxCircle{pos = {}, radius = 14, color = {80, 220, 100, 255}},
|
||||
1 = GfxAnimatedSprite {
|
||||
pos = {},
|
||||
rect = {0, 0, 64, 64},
|
||||
tex = gfx_get(gfx, .IDLE),
|
||||
atlas = player_atlas_get(gfx, .IDLE),
|
||||
frame = 0,
|
||||
speed = 1,
|
||||
speed = 0.5,
|
||||
update = update_player_sprite,
|
||||
},
|
||||
}
|
||||
p.hp = p.base_max_hp
|
||||
@@ -78,6 +78,15 @@ make_player :: proc(gfx: ^GraphicsAssetStore) -> Entity {
|
||||
return Entity{v = p}
|
||||
}
|
||||
|
||||
update_player_sprite :: proc(g: ^GfxAnimatedSprite, dt: f32) {
|
||||
g.current_time += dt
|
||||
if g.current_time > g.speed {
|
||||
g.current_time = g.current_time - g.speed
|
||||
g.frame += 1
|
||||
if g.frame > 3 do g.frame = 0
|
||||
}
|
||||
}
|
||||
|
||||
// ─── Итоговые статы из экипировки ───
|
||||
|
||||
player_total_dmg :: proc(p: ^Player) -> i32 {
|
||||
@@ -233,4 +242,11 @@ player_update :: proc(state: ^GameState, p: ^Player, h: Handle, dt: f32) {
|
||||
for &s in p.skills {
|
||||
if s.timer > 0 do s.timer -= dt
|
||||
}
|
||||
|
||||
for &gfx in p.gfx {
|
||||
#partial switch &v in gfx {
|
||||
case GfxAnimatedSprite:
|
||||
v->update(dt)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user