summaryrefslogtreecommitdiff
path: root/player.lua
blob: 14f0885ee65af17770ad2f0fb8d137f632c70ae3 (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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
local Entity = require("entity")
local Animation = require("animation")

local Player = {}
Player.__index = Player
setmetatable(Player, { __index = Entity })

local MOVE_SPEED = 65
local SWIM_SPEED = 100
local JUMP_FORCE = -200
local GROUND_LAYER = "ground"

function Player.new(world, spawnX, spawnY)
  local w, h = 16, 16
  local pw, ph = 16, 16  -- physics body size
  local self = setmetatable(Entity:new(spawnX or 0, spawnY or 0, w, h, pw, ph), Player)

  self.directionState = { 'side', 'up', 'down', 'side_up', 'side_down' }
  self.directionIndex = 1
  self.direction = self.directionState[self.directionIndex]

  self.availableJumps = 0
  self:enablePhysics(world, "dynamic")

  self.fixture:setFriction(0)
  self.doubleJump = true
  self.jumpsUsed = 0
  self.animations = {
    idle = Animation.new("assets/player/idle.png", 16, 16, 0.6),
    running = Animation.new("assets/player/running.png", 16, 16, 0.4),
    going_up = Animation.new("assets/player/going_up.png", 16, 16, 0.8),
    going_down = Animation.new("assets/player/going_down.png", 16, 16, 0.5),
    stop_running = Animation.new("assets/player/stop_running.png", 16, 16, 0.5),
    ground_hit = Animation.new("assets/player/ground_hit.png", 16, 16, 0.5),
    swimming = Animation.new("assets/player/swimming.png", 16, 16, 0.5)
  }
  if love and love.filesystem and love.filesystem.getDirectoryItems then
    local ok, items = pcall(love.filesystem.getDirectoryItems, "assets/player")
    if ok and items then
      for _, filename in ipairs(items) do
        if filename:match("%.png$") then
          local name = filename:gsub("%.png$", "")
          if not self.animations[name] then
            self.animations[name] = Animation.new("assets/player/" .. filename, 16, 16, 0.3)
          end
        end
      end
    end
  end
  
  self.animations.ground_hit.looping = false
  self.animations.stop_running.looping = false

  self.currentAnim = self.animations.idle
  
  self.lastFacing = 1
  
  self.state = "idle"
  self.wasOnFloor = false
  
  self.vx = 0
  self.vy = 0
  
  self.grounded = false
  self.isInLiquid = false
  self.wasInLiquid = false
  self.waterSurfaceContact = false
  return self
end

function Player:trackDirectionByKeyPressed()
    local left  = love.keyboard.isDown("left", "a")
    local right = love.keyboard.isDown("right", "d")
    local up    = love.keyboard.isDown("up", "w")
    local down  = love.keyboard.isDown("down", "s")

    if up and left then
        self.directionIndex = 4 
    elseif up and right then
        self.directionIndex = 4
    elseif down and left then
        self.directionIndex = 5
    elseif down and right then
        self.directionIndex = 5

    elseif up then
        self.directionIndex = 2
    elseif down then
        self.directionIndex = 3
    elseif left then
        self.directionIndex = 1
    elseif right then
        self.directionIndex = 1
    end

    self.direction = self.directionState[self.directionIndex]
end

function Player:update(dt)
  self:syncFromPhysicsBody()
  self:trackDirectionByKeyPressed()
  
  local vx, vy = self.body:getLinearVelocity()
  self.vx = vx
  self.vy = vy
  
  local c = self.contact or { floor = 0, wall = 0, ceiling = 0 }
  local onFloor = c.floor == 1
  local onWaterSurface = self.waterSurfaceContact
  local prevOnFloor = self.wasOnFloor
  local prevState = self.state

  local move = 0
  if love.keyboard.isDown("left", "a") then move = move - 1 end
  if love.keyboard.isDown("right", "d") then move = move + 1 end

  if self.isInLiquid then
    local vx, vy = self.body:getLinearVelocity()
    local mass = self.body:getMass()
    local gx, gy = self.body:getWorld():getGravity()

    if not self.wasInLiquid then
      vx = vx * 0.4
      vy = math.min(vy, 60)
      self.body:setLinearVelocity(vx, vy)
    end

    self.body:setLinearDamping(6)

    local buoyancy = 0.9
    self.body:applyForce(0, -gy * mass * buoyancy)

    local moveY = 0
    if love.keyboard.isDown("up", "w") then moveY = moveY - 1 end
    if love.keyboard.isDown("down", "s") then moveY = moveY + 1 end

    local inputLen = math.sqrt(move * move + moveY * moveY)
    if inputLen > 1 then
      move = move / inputLen
      moveY = moveY / inputLen
    end

    if move ~= 0 then self.lastFacing = move end
    self.body:applyForce(move * SWIM_SPEED, moveY * SWIM_SPEED)
  elseif onFloor or onWaterSurface then
    self.body:setLinearDamping(0)
    self.jumpsUsed = 0
    if move ~= 0 then
      self.lastFacing = move
      self.body:setLinearVelocity(move * MOVE_SPEED, vy)
    else
      self.body:setLinearVelocity(0, vy)
    end
  else
    self.body:setLinearDamping(0)
    if move ~= 0 then self.lastFacing = move end
    self.body:setLinearVelocity(move * MOVE_SPEED * 0.7, vy)
  end

  local desiredState
  if self.isInLiquid then
    desiredState = "swimming"
  elseif onFloor then
    desiredState = (move ~= 0) and "running" or "idle"
  else
    desiredState = (vy < 0) and "jumping" or "falling"
  end

  if self.isInLiquid then
    self.state = desiredState
  elseif self.state == "ground_hit" or self.state == "stop_running" then
    if not onFloor or self.currentAnim:isFinished() then
      self.state = desiredState
    end
  else
    local wasAirborne = prevState == "jumping" or prevState == "falling"
    if onFloor and not prevOnFloor and wasAirborne then
      self.state = "ground_hit"
      self.animations.ground_hit:reset()
    elseif onFloor and prevState == "running" and move == 0 then
      self.state = "stop_running"
      self.animations.stop_running:reset()
    else
      self.state = desiredState
    end
  end

  self.wasOnFloor = onFloor
  self.wasInLiquid = self.isInLiquid

  local animMap = {
    idle         = self.animations.idle,
    running      = self.animations.running,
    jumping      = self.animations.going_up,
    falling      = self.animations.going_down,
    swimming     = self.animations.swimming,
    stop_running = self.animations.stop_running,
    ground_hit   = self.animations.ground_hit,
  }
  self.currentAnim = animMap[self.state] or self.animations.idle
  self.currentAnim:update(dt)
end

function Player:jump()
  local c = self.contact or { floor = 0, wall = 0, ceiling = 0 }
  local onFloor = c.floor == 1
  local onWall = c.wall == 1
  local onWaterSurface = self.waterSurfaceContact

  if onWall and not onFloor then
    return false
  end

  local maxJumps = self.doubleJump and 1 or 0

  if self.jumpsUsed >= maxJumps then
    return false
  end

  if self.isInLiquid and not onWaterSurface then
    return false
  end

  self.body:setLinearVelocity(self.body:getLinearVelocity(), JUMP_FORCE)

  self.state = "jumping"
  self.animations.going_up:reset()

  self.jumpsUsed = self.jumpsUsed + 1

  return true
end

function Player:draw()
  local flip = (self.lastFacing < 0)
  self.currentAnim:draw(self.x, self.y, flip)
end

function Player:getBody()
  return self.body
end

return Player