summaryrefslogtreecommitdiff
path: root/template/states/game.lua
blob: 89f91fb1ae4b1b61492ee73c9915be82dbeb0c9a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
--[[
Game State - Main gameplay state
Demonstrates:
- GameState usage
- Animation system
- Object-oriented player
- Basic game loop
]]

local Object = require("lib.classic")
local Animation = require("lib.animation")
local GameState = Object:extend()

-- Player class
local Player = Object:extend()

function Player:new(x, y)
  self.x = x
  self.y = y
  self.speed = 200
  self.animation = nil
end

function Player:update(dt)
  local moved = false
  
  -- Movement
  if RL.IsKeyDown(RL.KEY_LEFT) or RL.IsKeyDown(RL.KEY_A) then
    self.x = self.x - self.speed * dt
    moved = true
  end
  if RL.IsKeyDown(RL.KEY_RIGHT) or RL.IsKeyDown(RL.KEY_D) then
    self.x = self.x + self.speed * dt
    moved = true
  end
  if RL.IsKeyDown(RL.KEY_UP) or RL.IsKeyDown(RL.KEY_W) then
    self.y = self.y - self.speed * dt
    moved = true
  end
  if RL.IsKeyDown(RL.KEY_DOWN) or RL.IsKeyDown(RL.KEY_S) then
    self.y = self.y + self.speed * dt
    moved = true
  end
  
  -- Update animation
  if self.animation then
    if moved then
      self.animation:play("walk")
    else
      self.animation:play("idle")
    end
    self.animation:update(dt)
  end
end

function Player:draw()
  if self.animation then
    self.animation:drawSimple(self.x, self.y)
  else
    -- Fallback: draw a simple rectangle
    RL.DrawRectangle({self.x, self.y, 32, 32}, RL.BLUE)
  end
end

-- Game State
function GameState:new()
  self.player = nil
  self.paused = false
end

function GameState:enter(previous)
  print("Entered game state")
  
  local screenSize = RL.GetScreenSize()
  
  -- Create player
  self.player = Player(screenSize[1] / 2 - 16, screenSize[2] / 2 - 16)
  
  -- TODO: Load player sprite sheet and create animation
  -- Example:
  -- local playerTexture = RL.LoadTexture("assets/player.png")
  -- self.player.animation = Animation.new(playerTexture, 32, 32, {
  --   idle = {frames = {1, 2, 3, 4}, fps = 8, loop = true},
  --   walk = {frames = {5, 6, 7, 8}, fps = 12, loop = true}
  -- })
  -- self.player.animation:play("idle")
end

function GameState:update(dt)
  -- Pause/unpause
  if RL.IsKeyPressed(RL.KEY_ESCAPE) or RL.IsKeyPressed(RL.KEY_P) then
    self.paused = not self.paused
  end
  
  if self.paused then
    return
  end
  
  -- Update game objects
  if self.player then
    self.player:update(dt)
  end
end

function GameState:draw()
  RL.ClearBackground({50, 50, 50, 255})
  
  -- Draw game objects
  if self.player then
    self.player:draw()
  end
  
  -- Draw pause overlay
  if self.paused then
    local screenSize = RL.GetScreenSize()
    local centerX = screenSize[1] / 2
    local centerY = screenSize[2] / 2
    
    -- Semi-transparent overlay
    RL.DrawRectangle({0, 0, screenSize[1], screenSize[2]}, {0, 0, 0, 128})
    
    -- Pause text
    local text = "PAUSED"
    local size = 40
    local width = RL.MeasureText(text, size)
    RL.DrawText(text, {centerX - width / 2, centerY - 20}, size, RL.WHITE)
    
    local hint = "Press ESC or P to resume"
    local hintSize = 20
    local hintWidth = RL.MeasureText(hint, hintSize)
    RL.DrawText(hint, {centerX - hintWidth / 2, centerY + 30}, hintSize, RL.GRAY)
  end
  
  -- Draw controls hint
  local hint = "WASD/ARROWS: Move  |  ESC: Pause"
  local hintSize = 16
  local screenSize = RL.GetScreenSize()
  RL.DrawText(hint, {10, screenSize[2] - 30}, hintSize, RL.LIGHTGRAY)
end

function GameState:leave()
  print("Left game state")
  -- Cleanup game assets here
end

return GameState