summaryrefslogtreecommitdiffhomepage
path: root/samples/99_genre_mario/02_jumping_and_collisions/app/main.rb
blob: 52d9135ad65c53128252de2680862eec602f69e7 (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
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
class Game
  attr_gtk

  def tick
    defaults
    render
    input
    calc
  end

  def defaults
    return if state.tick_count != 0

    player.x                     = 64
    player.y                     = 800
    player.size                  = 50
    player.dx                    = 0
    player.dy                    = 0
    player.action                = :falling

    player.max_speed             = 20
    player.jump_power            = 15
    player.jump_air_time         = 15
    player.jump_increase_power   = 1

    state.gravity                = -1
    state.drag                   = 0.001
    state.tile_size              = 64
    state.tiles                ||= [
      { ordinal_x:  0, ordinal_y: 0 },
      { ordinal_x:  1, ordinal_y: 0 },
      { ordinal_x:  2, ordinal_y: 0 },
      { ordinal_x:  3, ordinal_y: 0 },
      { ordinal_x:  4, ordinal_y: 0 },
      { ordinal_x:  5, ordinal_y: 0 },
      { ordinal_x:  6, ordinal_y: 0 },
      { ordinal_x:  7, ordinal_y: 0 },
      { ordinal_x:  8, ordinal_y: 0 },
      { ordinal_x:  9, ordinal_y: 0 },
      { ordinal_x: 10, ordinal_y: 0 },
      { ordinal_x: 11, ordinal_y: 0 },
      { ordinal_x: 12, ordinal_y: 0 },

      { ordinal_x:  9, ordinal_y: 3 },
      { ordinal_x: 10, ordinal_y: 3 },
      { ordinal_x: 11, ordinal_y: 3 },
    ]

    tiles.each do |t|
      t.rect = { x: t.ordinal_x * 64,
                 y: t.ordinal_y * 64,
                 w: 64,
                 h: 64 }
    end
  end

  def render
    render_player
    render_tiles
    # render_grid
  end

  def input
    input_jump
    input_move
  end

  def calc
    calc_player_rect
    calc_left
    calc_right
    calc_below
    calc_above
    calc_player_dy
    calc_player_dx
    calc_game_over
  end

  def render_player
    outputs.sprites << {
      x: player.x,
      y: player.y,
      w: player.size,
      h: player.size,
      path: 'sprites/square/red.png'
    }
  end

  def render_tiles
    outputs.sprites << state.tiles.map do |t|
      t.merge path: 'sprites/square/white.png',
              x: t.ordinal_x * 64,
              y: t.ordinal_y * 64,
              w: 64,
              h: 64
    end
  end

  def render_grid
    if state.tick_count == 0
      outputs[:grid].background_color = [0, 0, 0, 0]
      outputs[:grid].borders << available_brick_locations
      outputs[:grid].labels  << available_brick_locations.map do |b|
        [
          b.merge(text: "#{b.ordinal_x},#{b.ordinal_y}",
                  x: b.x + 2,
                  y: b.y + 2,
                  size_enum: -3,
                  vertical_alignment_enum: 0,
                  blendmode_enum: 0),
          b.merge(text: "#{b.x},#{b.y}",
                  x: b.x + 2,
                  y: b.y + 2 + 20,
                  size_enum: -3,
                  vertical_alignment_enum: 0,
                  blendmode_enum: 0)
        ]
      end
    end

    outputs.sprites << { x: 0, y: 0, w: 1280, h: 720, path: :grid }
  end

  def input_jump
    if inputs.keyboard.key_down.space
      player_jump
    end

    if inputs.keyboard.key_held.space
      player_jump_increase_air_time
    end
  end

  def input_move
    if player.dx.abs < 20
      if inputs.keyboard.left
        player.dx -= 2
      elsif inputs.keyboard.right
        player.dx += 2
      end
    end
  end

  def calc_game_over
    if player.y < -64
      player.x = 64
      player.y = 800
      player.dx = 0
      player.dy = 0
    end
  end

  def calc_player_rect
    player.rect      = player_current_rect
    player.next_rect = player_next_rect
    player.prev_rect = player_prev_rect
  end

  def calc_player_dx
    player.dx  = player_next_dx
    player.x  += player.dx
  end

  def calc_player_dy
    player.y  += player.dy
    player.dy  = player_next_dy
  end

  def calc_below
    return unless player.dy < 0
    tiles_below = tiles_find { |t| t.rect.top <= player.prev_rect.y }
    collision = tiles_find_colliding tiles_below, (player.rect.merge y: player.next_rect.y)
    if collision
      player.y  = collision.rect.y + state.tile_size
      player.dy = 0
      player.action = :standing
    else
      player.action = :falling
    end
  end

  def calc_left
    return unless player.dx < 0 && player_next_dx < 0
    tiles_left = tiles_find { |t| t.rect.right <= player.prev_rect.left }
    collision = tiles_find_colliding tiles_left, (player.rect.merge x: player.next_rect.x)
    return unless collision
    player.x  = collision.rect.right
    player.dx = 0
  end

  def calc_right
    return unless player.dx > 0 && player_next_dx > 0
    tiles_right = tiles_find { |t| t.rect.left >= player.prev_rect.right }
    collision = tiles_find_colliding tiles_right, (player.rect.merge x: player.next_rect.x)
    return unless collision
    player.x  = collision.rect.left - player.rect.w
    player.dx = 0
  end

  def calc_above
    return unless player.dy > 0
    tiles_above = tiles_find { |t| t.rect.y >= player.prev_rect.y }
    collision = tiles_find_colliding tiles_above, (player.rect.merge y: player.next_rect.y)
    return unless collision
    player.dy = 0
    player.y  = collision.rect.bottom - player.rect.h
  end

  def player_current_rect
    { x: player.x, y: player.y, w: player.size, h: player.size }
  end

  def available_brick_locations
    (0..19).to_a
      .product(0..11)
      .map do |(ordinal_x, ordinal_y)|
      { ordinal_x: ordinal_x,
        ordinal_y: ordinal_y,
        x: ordinal_x * 64,
        y: ordinal_y * 64,
        w: 64,
        h: 64 }
    end
  end

  def player
    state.player ||= args.state.new_entity :player
  end

  def player_next_dy
    player.dy + state.gravity + state.drag ** 2 * -1
  end

  def player_next_dx
    player.dx * 0.8
  end

  def player_next_rect
    player.rect.merge x: player.x + player_next_dx,
                      y: player.y + player_next_dy
  end

  def player_prev_rect
    player.rect.merge x: player.x - player.dx,
                      y: player.y - player.dy
  end

  def player_jump
    return if player.action != :standing
    player.action = :jumping
    player.dy = state.player.jump_power
    current_frame = state.tick_count
    player.action_at = current_frame
  end

  def player_jump_increase_air_time
    return if player.action != :jumping
    return if player.action_at.elapsed_time >= player.jump_air_time
    player.dy += player.jump_increase_power
  end

  def tiles
    state.tiles
  end

  def tiles_find_colliding tiles, target
    tiles.find { |t| t.rect.intersect_rect? target }
  end

  def tiles_find &block
    tiles.find_all(&block)
  end
end

def tick args
  $game ||= Game.new
  $game.args = args
  $game.tick
end

$gtk.reset