summaryrefslogtreecommitdiff
path: root/src/gnu_and_bola_brawlers/main.lua
blob: af3952c6cd652e4663408ac2f31c1a02a522c182 (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
function love.load()
  local load = require 'load/default'
  load.initialize()
end

function love.keypressed(key, scancode)
  local keypressed = require 'keypressed/default'
  keypressed.trigger(character.bola, images.bola, metaSprite.bola, controller.player1, key, scancode)
end

function love.keyreleased(key, scancode)
  local keyreleased = require 'keyreleased/default'
  keyreleased.trigger(character.bola, key, controller.player1, scancode)
end

function love.update(dt)
  nextTime = nextTime + (1 / fps)

  animation = function(metaSprite, images, dt)
    --Increase actual elapsed time with new dt
    metaSprite.elapsedTime = metaSprite.elapsedTime + dt --AnimationStart

    --In order to know if it is necessary to change the frame
    if metaSprite.elapsedTime >= (1 / metaSprite.fps) then
      metaSprite.elapsedTime = metaSprite.elapsedTime - (1 / metaSprite.fps)
      if metaSprite.currentFrame == # metaSprite then --If current frame is equal to long metaSprite list
        metaSprite.currentFrame = 1 -- Return to first frame
      else
        metaSprite.currentFrame = metaSprite.currentFrame + 1 --Next frame
      end
    end

    images.quad = metaSprite[metaSprite.currentFrame] --Update with new fame
  end
  animation(metaSprite.bola, images.bola, dt)

  motion = function(character, images, metaSprite, dt)
    if character.jump.higher > 0 and character.actionA == true then
      if character.jump.limitButtonJump == false then
        character.jump.higher   = character.jump.higher - dt
        character.jump.velocity = character.jump.velocity + character.jump.height * (dt / character.jump.higherMax)
      end
    end

    if character.actionLeft == true and character.actionRight == false then
      metaSprite = require 'walk'
      character.position.x = character.position.x - (character.velocity * dt)
      character.scale.x = -1
    end

    if character.actionRight == true and character.actionLeft == false then
      metaSprite = require 'walk'
      character.position.x = character.position.x + (character.velocity * dt)
      character.scale.x =  1
    end

    if character.actionUp == true and character.actionDown == false then
      metaSprite = require 'walk'
      character.position.y = character.position.y - (character.velocity * dt)
      character.jump.ground = character.position.y
    end

    if character.actionDown == true and character.actionUp == false then
      metaSprite = require 'walk'
      character.position.y = character.position.y + (character.velocity * dt)
      character.jump.ground = character.position.y
    end

    if character.actionUp == true and character.actionDown == true then
      metaSprite = require 'stand'
    elseif character.actionLeft == true and character.actionRight == true then
      metaSprite = require 'stand'
    elseif character.actionLeft == false and character.actionRight == false and character.actionUp == false and character.actionDown == false then
      metaSprite = require 'stand'
    end

    return metaSprite
  end
  metaSprite.bola = motion(character.bola, images.bola, metaSprite.bola, dt)

  gravity = function(character, dt)
    if character.jump.velocity ~= 0 then
      character.jump.isJumping = true
      character.position.y = character.position.y + (character.jump.velocity * dt)
      character.jump.velocity = character.jump.velocity - (character.gravity * dt)
    end

    if character.position.y > character.jump.ground then
      character.jump.velocity = 0
      character.position.y = character.jump.ground
      character.jump.higher = character.jump.higherMax

      character.jump.limitButtonJump = false
      character.jump.isJumping = false
      character.actionA = false

      if love.keyboard.isScancodeDown(controller.player1.up) then
        character.actionUp = true
      elseif love.keyboard.isScancodeDown(controller.player1.down) then
        character.actionDown = true
      end
    end
  end
  gravity(character.bola, dt)

  limit = function(character, dt)
    if character.position.x <= character.origin.x then
      character.position.x = character.origin.x
    elseif character.position.x >= windowProfile.mode.width - character.origin.x then
      character.position.x = windowProfile.mode.width - character.origin.x
    end

    if character.position.y <= character.origin.y then
      character.position.y = character.origin.y
    elseif character.position.y >= windowProfile.mode.height - character.origin.y then
      character.position.y = windowProfile.mode.height - character.origin.y
    end
  end
  limit(character.bola, dt)
end

function love.draw()
  local draw = require 'draw/default'
  draw.refresh()
  draw.object(images.bola, character.bola)
end