update drawing items and attempt at adding sound FX
This commit is contained in:
parent
5b4dab6292
commit
01dec4e5c1
5 changed files with 119 additions and 75 deletions
BIN
assets/sounds/pickup_sound.mp3
Normal file
BIN
assets/sounds/pickup_sound.mp3
Normal file
Binary file not shown.
BIN
assets/sounds/pickup_sound.ogg
Normal file
BIN
assets/sounds/pickup_sound.ogg
Normal file
Binary file not shown.
BIN
assets/sounds/sell_sound.mp3
Normal file
BIN
assets/sounds/sell_sound.mp3
Normal file
Binary file not shown.
BIN
assets/tomato_item.png
Normal file
BIN
assets/tomato_item.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 242 B |
194
src/main.odin
194
src/main.odin
|
|
@ -1,5 +1,6 @@
|
||||||
package game
|
package game
|
||||||
|
|
||||||
|
import t "../tween"
|
||||||
import sa "core:container/small_array"
|
import sa "core:container/small_array"
|
||||||
import "core:fmt"
|
import "core:fmt"
|
||||||
import "core:math"
|
import "core:math"
|
||||||
|
|
@ -26,48 +27,28 @@ world_mouse: rl.Vector2
|
||||||
screen_game_area: rl.Rectangle
|
screen_game_area: rl.Rectangle
|
||||||
game_render_buffer_area := rl.Rectangle{0, 0, GAME_SCREEN_WIDTH, -GAME_SCREEN_HEIGHT}
|
game_render_buffer_area := rl.Rectangle{0, 0, GAME_SCREEN_WIDTH, -GAME_SCREEN_HEIGHT}
|
||||||
|
|
||||||
player_sprite: []u8 : #load("../assets/player.png")
|
pickup_sound := rl.LoadSound("assets/sounds/pickup_sound.ogg")
|
||||||
tomato_sprite: []u8 : #load("../assets/tomato.png")
|
|
||||||
|
player_spritesheet_sprite: []u8 : #load("../assets/player.png")
|
||||||
|
tomato_spritesheet_sprite: []u8 : #load("../assets/tomato.png")
|
||||||
|
tomato_item_sprite: []u8 : #load("../assets/tomato_item.png")
|
||||||
sell_button_sprite: []u8 : #load("../assets/sell_button.png")
|
sell_button_sprite: []u8 : #load("../assets/sell_button.png")
|
||||||
background_sprite: []u8 : #load("../assets/background.png")
|
background_sprite: []u8 : #load("../assets/background.png")
|
||||||
tile_dirt_dry_sprite: []u8 : #load("../assets/tile_dirt_dry.png")
|
tile_dirt_dry_sprite: []u8 : #load("../assets/tile_dirt_dry.png")
|
||||||
tile_dirt_wet_sprite: []u8 : #load("../assets/tile_dirt_wet.png")
|
tile_dirt_wet_sprite: []u8 : #load("../assets/tile_dirt_wet.png")
|
||||||
|
|
||||||
player_spritesheet_image := rl.LoadImageFromMemory(
|
load_texture_from_memory :: proc(data: []u8) -> (texture: rl.Texture2D) {
|
||||||
".png",
|
img := rl.LoadImageFromMemory(".png", raw_data(data), auto_cast len(data))
|
||||||
raw_data(player_sprite),
|
defer rl.UnloadImage(img)
|
||||||
auto_cast len(player_sprite),
|
texture = rl.LoadTextureFromImage(img)
|
||||||
)
|
return texture
|
||||||
tomato_spritesheet_image := rl.LoadImageFromMemory(
|
}
|
||||||
".png",
|
|
||||||
raw_data(tomato_sprite),
|
|
||||||
auto_cast len(tomato_sprite),
|
|
||||||
)
|
|
||||||
sell_button_image := rl.LoadImageFromMemory(
|
|
||||||
".png",
|
|
||||||
raw_data(sell_button_sprite),
|
|
||||||
auto_cast len(sell_button_sprite),
|
|
||||||
)
|
|
||||||
background_image := rl.LoadImageFromMemory(
|
|
||||||
".png",
|
|
||||||
raw_data(background_sprite),
|
|
||||||
auto_cast len(background_sprite),
|
|
||||||
)
|
|
||||||
sell_button_texture: rl.Texture2D
|
sell_button_texture: rl.Texture2D
|
||||||
background_texture: rl.Texture2D
|
background_texture: rl.Texture2D
|
||||||
|
|
||||||
tile_dirt_dry_image := rl.LoadImageFromMemory(
|
|
||||||
".png",
|
|
||||||
raw_data(tile_dirt_dry_sprite),
|
|
||||||
auto_cast len(tile_dirt_dry_sprite),
|
|
||||||
)
|
|
||||||
tile_dirt_wet_image := rl.LoadImageFromMemory(
|
|
||||||
".png",
|
|
||||||
raw_data(tile_dirt_wet_sprite),
|
|
||||||
auto_cast len(tile_dirt_wet_sprite),
|
|
||||||
)
|
|
||||||
tile_dirt_dry_texture: rl.Texture2D
|
tile_dirt_dry_texture: rl.Texture2D
|
||||||
tile_dirt_wet_texture: rl.Texture2D
|
tile_dirt_wet_texture: rl.Texture2D
|
||||||
|
tomato_texture: rl.Texture2D
|
||||||
|
|
||||||
player_animation: Animation
|
player_animation: Animation
|
||||||
tomato_animation: Animation
|
tomato_animation: Animation
|
||||||
|
|
@ -79,35 +60,44 @@ animations: [dynamic]^Animation
|
||||||
|
|
||||||
entities: [256]Entity
|
entities: [256]Entity
|
||||||
|
|
||||||
|
tween_ctx_f32: t.TweenContext(f32)
|
||||||
|
tween_ctx_vec2: t.TweenContext([2]f32)
|
||||||
|
|
||||||
main :: proc() {
|
main :: proc() {
|
||||||
rl.SetConfigFlags({.VSYNC_HINT, .WINDOW_RESIZABLE, .WINDOW_HIGHDPI, .MSAA_4X_HINT})
|
rl.SetConfigFlags({.VSYNC_HINT, .WINDOW_RESIZABLE, .WINDOW_HIGHDPI, .MSAA_4X_HINT})
|
||||||
rl.InitWindow(1280, 720, "Raylib Minimal")
|
rl.InitWindow(1280, 720, "Raylib Minimal")
|
||||||
rl.SetTargetFPS(rl.GetMonitorRefreshRate(rl.GetCurrentMonitor()))
|
rl.SetTargetFPS(rl.GetMonitorRefreshRate(rl.GetCurrentMonitor()))
|
||||||
|
|
||||||
|
rl.InitAudioDevice()
|
||||||
game_render_buffer = rl.LoadRenderTexture(
|
game_render_buffer = rl.LoadRenderTexture(
|
||||||
auto_cast GAME_SCREEN_WIDTH,
|
auto_cast GAME_SCREEN_WIDTH,
|
||||||
auto_cast GAME_SCREEN_HEIGHT,
|
auto_cast GAME_SCREEN_HEIGHT,
|
||||||
)
|
)
|
||||||
rl.SetTextureFilter(game_render_buffer.texture, rl.TextureFilter.POINT) // Texture scale filter to use
|
rl.SetTextureFilter(game_render_buffer.texture, rl.TextureFilter.POINT) // Texture scale filter to use
|
||||||
|
|
||||||
|
tween_ctx_f32 = t.context_init(f32)
|
||||||
|
tween_ctx_vec2 = t.context_init([2]f32)
|
||||||
|
defer t.context_destroy(tween_ctx_f32)
|
||||||
|
defer t.context_destroy(tween_ctx_vec2)
|
||||||
|
|
||||||
player_animation = Animation {
|
player_animation = Animation {
|
||||||
texture = rl.LoadTextureFromImage(player_spritesheet_image),
|
texture = load_texture_from_memory(player_spritesheet_sprite),
|
||||||
num_frames = 2,
|
num_frames = 2,
|
||||||
frame_length = 0.5,
|
frame_length = 0.5,
|
||||||
loop = true,
|
loop = true,
|
||||||
}
|
}
|
||||||
tomato_animation = Animation {
|
tomato_animation = Animation {
|
||||||
texture = rl.LoadTextureFromImage(tomato_spritesheet_image),
|
texture = load_texture_from_memory(tomato_spritesheet_sprite),
|
||||||
num_frames = 4,
|
num_frames = 4,
|
||||||
frame_length = 2,
|
frame_length = 0.1,
|
||||||
loop = false,
|
loop = false,
|
||||||
offset = {TILE_SIZE / 2, TILE_SIZE},
|
offset = {TILE_SIZE / 2, TILE_SIZE},
|
||||||
}
|
}
|
||||||
sell_button_texture = rl.LoadTextureFromImage(sell_button_image)
|
tomato_texture = load_texture_from_memory(tomato_item_sprite)
|
||||||
background_texture = rl.LoadTextureFromImage(background_image)
|
sell_button_texture = load_texture_from_memory(sell_button_sprite)
|
||||||
|
background_texture = load_texture_from_memory(background_sprite)
|
||||||
tile_dirt_dry_texture = rl.LoadTextureFromImage(tile_dirt_dry_image)
|
tile_dirt_dry_texture = load_texture_from_memory(tile_dirt_dry_sprite)
|
||||||
tile_dirt_wet_texture = rl.LoadTextureFromImage(tile_dirt_wet_image)
|
tile_dirt_wet_texture = load_texture_from_memory(tile_dirt_wet_sprite)
|
||||||
|
|
||||||
e := create_entity(
|
e := create_entity(
|
||||||
Entity {
|
Entity {
|
||||||
|
|
@ -118,17 +108,25 @@ main :: proc() {
|
||||||
)
|
)
|
||||||
player_handle = entity_to_handle(e^)
|
player_handle = entity_to_handle(e^)
|
||||||
|
|
||||||
|
rl.PlaySound(pickup_sound)
|
||||||
|
|
||||||
for !rl.WindowShouldClose() {
|
for !rl.WindowShouldClose() {
|
||||||
free_all(context.temp_allocator)
|
free_all(context.temp_allocator)
|
||||||
update()
|
update()
|
||||||
draw()
|
draw()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
rl.UnloadRenderTexture(game_render_buffer) // Unload render texture
|
rl.UnloadRenderTexture(game_render_buffer) // Unload render texture
|
||||||
|
rl.CloseAudioDevice()
|
||||||
rl.CloseWindow()
|
rl.CloseWindow()
|
||||||
}
|
}
|
||||||
|
|
||||||
update :: proc() {
|
update :: proc() {
|
||||||
|
// :tween update
|
||||||
|
t.tween_update(&tween_ctx_f32, auto_cast rl.GetFrameTime())
|
||||||
|
t.tween_update(&tween_ctx_vec2, auto_cast rl.GetFrameTime())
|
||||||
|
|
||||||
// :scaling update
|
// :scaling update
|
||||||
screen_scale = min(
|
screen_scale = min(
|
||||||
f32(rl.GetScreenWidth()) / GAME_SCREEN_WIDTH,
|
f32(rl.GetScreenWidth()) / GAME_SCREEN_WIDTH,
|
||||||
|
|
@ -155,16 +153,7 @@ update :: proc() {
|
||||||
}
|
}
|
||||||
|
|
||||||
// :animation update
|
// :animation update
|
||||||
for &e in entities {
|
update_entities()
|
||||||
if .Allocated not_in e.flags {continue}
|
|
||||||
|
|
||||||
#partial switch &data in e.data {
|
|
||||||
case TileData:
|
|
||||||
update_animation(&data.animation)
|
|
||||||
case PlayerData:
|
|
||||||
update_animation(&data.animation)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// :sell
|
// :sell
|
||||||
hover_sell_button := rl.CheckCollisionPointRec(
|
hover_sell_button := rl.CheckCollisionPointRec(
|
||||||
|
|
@ -179,7 +168,7 @@ update :: proc() {
|
||||||
// :player update
|
// :player update
|
||||||
player := handle_to_entity(player_handle)
|
player := handle_to_entity(player_handle)
|
||||||
if player != nil {
|
if player != nil {
|
||||||
pd, ok := player.data.(PlayerData);if ok {
|
pd, ok := &player.data.(PlayerData);if ok {
|
||||||
vel: rl.Vector2
|
vel: rl.Vector2
|
||||||
if rl.IsKeyDown(.D) do vel.x += 1
|
if rl.IsKeyDown(.D) do vel.x += 1
|
||||||
if rl.IsKeyDown(.A) do vel.x -= 1
|
if rl.IsKeyDown(.A) do vel.x -= 1
|
||||||
|
|
@ -202,21 +191,8 @@ draw :: proc() {
|
||||||
rl.BeginDrawing()
|
rl.BeginDrawing()
|
||||||
|
|
||||||
rl.DrawTextureV(background_texture, {}, rl.WHITE)
|
rl.DrawTextureV(background_texture, {}, rl.WHITE)
|
||||||
// :animation draw
|
|
||||||
//for t in tiles {
|
|
||||||
// rl.DrawRectangle(auto_cast t.pos.x, auto_cast t.pos.y, TILE_SIZE, TILE_SIZE, rl.BLACK)
|
|
||||||
// draw_animation(t.animation, t.pos)
|
|
||||||
//}
|
|
||||||
for e in entities {
|
|
||||||
if .Allocated not_in e.flags {continue}
|
|
||||||
|
|
||||||
#partial switch &data in e.data {
|
draw_entities()
|
||||||
case TileData:
|
|
||||||
draw_tile(e)
|
|
||||||
case PlayerData:
|
|
||||||
draw_player(e)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
rl.DrawTextureV(sell_button_texture, SELL_BUTTON_POS, rl.WHITE)
|
rl.DrawTextureV(sell_button_texture, SELL_BUTTON_POS, rl.WHITE)
|
||||||
rl.EndMode2D()
|
rl.EndMode2D()
|
||||||
|
|
@ -330,16 +306,17 @@ PlayerData :: struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
ItemData :: struct {
|
ItemData :: struct {
|
||||||
id: int,
|
id: int,
|
||||||
count: int,
|
count: int,
|
||||||
animation: Animation,
|
texture: rl.Texture2D,
|
||||||
}
|
}
|
||||||
|
|
||||||
TileData :: struct {
|
TileData :: struct {
|
||||||
has_plant: bool,
|
has_plant: bool,
|
||||||
is_watered: bool,
|
is_watered: bool,
|
||||||
plant_id: int,
|
plant_id: int,
|
||||||
animation: Animation,
|
animation: Animation,
|
||||||
|
plant_grown: bool,
|
||||||
}
|
}
|
||||||
|
|
||||||
EntityData :: union {
|
EntityData :: union {
|
||||||
|
|
@ -389,11 +366,76 @@ handle_to_entity :: proc(handle: EntityHandle) -> ^Entity {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
update_entities :: proc() {
|
||||||
|
for &e in entities {
|
||||||
|
if .Allocated not_in e.flags {continue}
|
||||||
|
|
||||||
|
#partial switch &data in e.data {
|
||||||
|
case TileData:
|
||||||
|
update_tile(e, &data)
|
||||||
|
case PlayerData:
|
||||||
|
update_animation(&data.animation)
|
||||||
|
case ItemData:
|
||||||
|
//rl.DrawTextureV(data.texture, e.pos + {0, t * 3}, rl.WHITE)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
update_tile :: proc(e: Entity, data: ^TileData) {
|
||||||
|
update_animation(&data.animation)
|
||||||
|
if data.has_plant && data.animation.done && data.plant_grown == false {
|
||||||
|
data.plant_grown = true
|
||||||
|
player := handle_to_entity(player_handle)
|
||||||
|
if player != nil {
|
||||||
|
item := create_entity(
|
||||||
|
Entity {
|
||||||
|
pos = e.pos,
|
||||||
|
kind = .Item,
|
||||||
|
data = ItemData{id = data.plant_id, count = 3, texture = tomato_texture},
|
||||||
|
},
|
||||||
|
)
|
||||||
|
tw := t.tween_to(&tween_ctx_vec2, &item.pos, player.pos)
|
||||||
|
tw.id = item.handle
|
||||||
|
tw.data = tw
|
||||||
|
tw.on_update = proc(ctx: ^t.TweenContext([2]f32), data: rawptr) {
|
||||||
|
player := handle_to_entity(player_handle)
|
||||||
|
if player != nil {
|
||||||
|
tween: ^t.Tween([2]f32) = transmute(^t.Tween([2]f32))data
|
||||||
|
tween.goal = player.pos - {TILE_SIZE, TILE_SIZE}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
tw.on_complete = proc(ctx: ^t.TweenContext([2]f32), data: rawptr) {
|
||||||
|
tween: ^t.Tween([2]f32) = transmute(^t.Tween([2]f32))data
|
||||||
|
fmt.println("Done")
|
||||||
|
rl.PlaySound(pickup_sound)
|
||||||
|
e := handle_to_entity(tween.id)
|
||||||
|
if e != nil && e.kind == .Item {
|
||||||
|
destroy_entity(e)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
draw_entities :: proc() {
|
||||||
|
for e in entities {
|
||||||
|
if .Allocated not_in e.flags {continue}
|
||||||
|
|
||||||
|
switch &data in e.data {
|
||||||
|
case TileData:
|
||||||
|
draw_tile(e)
|
||||||
|
case PlayerData:
|
||||||
|
draw_player(e)
|
||||||
|
case ItemData:
|
||||||
|
draw_item(e)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
entity_to_handle :: proc(e: Entity) -> EntityHandle {
|
entity_to_handle :: proc(e: Entity) -> EntityHandle {
|
||||||
return e.handle
|
return e.handle
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
draw_player :: proc(e: Entity) {
|
draw_player :: proc(e: Entity) {
|
||||||
data, ok := e.data.(PlayerData)
|
data, ok := e.data.(PlayerData)
|
||||||
draw_animation(data.animation, e.pos)
|
draw_animation(data.animation, e.pos)
|
||||||
|
|
@ -406,5 +448,7 @@ draw_tile :: proc(e: Entity) {
|
||||||
}
|
}
|
||||||
draw_item :: proc(e: Entity) {
|
draw_item :: proc(e: Entity) {
|
||||||
data, ok := e.data.(ItemData)
|
data, ok := e.data.(ItemData)
|
||||||
draw_animation(data.animation, e.pos)
|
t := f32(math.sin(rl.GetTime() * 5))
|
||||||
|
rl.DrawTextureV(data.texture, e.pos + {0, t * 3}, rl.WHITE)
|
||||||
|
//draw_animation(data.animation, e.pos + {0, t * 3})
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue