Class: GamesAndRpgParadise::GardenHero::Player

Inherits:
Object
  • Object
show all
Defined in:
lib/games_and_rpg_paradise/gui/gosu/garden_hero/core/player/player.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(window) ⇒ Player

#

initialize

#


18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/games_and_rpg_paradise/gui/gosu/garden_hero/core/player/player.rb', line 18

def initialize(window)
  @window, @x, @y = window, 600, 456
  @face, @score, @stars, @lives = 'left', 0, 0, 3
  @stamina = 100
  @left = Gosu::Image.new "images/player/player-left.png", tileable: false
  @right = Gosu::Image.new window, "images/player/player-right.png", false
  @up = Gosu::Image.new window, "images/player/player-up.png", false
  @down = Gosu::Image.new window, "images/player/player-down.png", false
  @ui_star = Gosu::Image.new window, "images/player/star.png", true
  @ui_lives = Gosu::Image.new window, "images/player/heart.png", true
  @collect = Gosu::Song.new(window, "sounds/player/collect.ogg")
  @collect_apples = Gosu::Song.new(window, "sounds/player/collect_apples.ogg")
  @win_song = Gosu::Song.new(window, "sounds/level/win.ogg")
  @game_over_song = Gosu::Song.new(window, "sounds/level/game_over.ogg")
  @die_song = Gosu::Song.new(window, "sounds/player/die.ogg")
  @weapon = ApplePlayer.new window, self
  @ui = Gosu::Font.new(window, 'Monospace', 20)
end

Instance Attribute Details

#faceObject (readonly)

Returns the value of attribute face.



13
14
15
# File 'lib/games_and_rpg_paradise/gui/gosu/garden_hero/core/player/player.rb', line 13

def face
  @face
end

#scoreObject (readonly)

Returns the value of attribute score.



13
14
15
# File 'lib/games_and_rpg_paradise/gui/gosu/garden_hero/core/player/player.rb', line 13

def score
  @score
end

#windowObject (readonly)

Returns the value of attribute window.



13
14
15
# File 'lib/games_and_rpg_paradise/gui/gosu/garden_hero/core/player/player.rb', line 13

def window
  @window
end

#xObject (readonly)

Returns the value of attribute x.



13
14
15
# File 'lib/games_and_rpg_paradise/gui/gosu/garden_hero/core/player/player.rb', line 13

def x
  @x
end

#yObject (readonly)

Returns the value of attribute y.



13
14
15
# File 'lib/games_and_rpg_paradise/gui/gosu/garden_hero/core/player/player.rb', line 13

def y
  @y
end

Instance Method Details

#add_apples_scoreObject

add score when collect apples by player



439
440
441
442
# File 'lib/games_and_rpg_paradise/gui/gosu/garden_hero/core/player/player.rb', line 439

def add_apples_score
  @score += 50
  @lives += 1 if @lives < 3
end

#add_enemies_scoreObject

add_enemies_score

add score when kill enemies



447
448
449
# File 'lib/games_and_rpg_paradise/gui/gosu/garden_hero/core/player/player.rb', line 447

def add_enemies_score
  @score += 75
end

#add_injuryObject

add injury when enemies attack



471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
# File 'lib/games_and_rpg_paradise/gui/gosu/garden_hero/core/player/player.rb', line 471

def add_injury
  case face
  when 'left'
    @x += 4
  when 'right'
    @x -= 4
  when 'up'
    @y += 4
  when 'down'
    @y -= 4
  end
  @stamina -= 5
  if @stamina <= 0
    @die_song.play(looping = false)
    @stamina = 0
    reboot
  end
end

#add_injury_to_enemiesObject

add injury to enemies



491
492
493
494
495
496
497
498
499
500
501
502
503
504
# File 'lib/games_and_rpg_paradise/gui/gosu/garden_hero/core/player/player.rb', line 491

def add_injury_to_enemies
  window.level.enemies.each do |e|
    if Gosu::distance(e.x, e.y, @weapon.x, @weapon.y) <= 8
      @weapon.drawing = false
      add_enemies_score
    end
    if Gosu::distance(e.x, e.y, x, y) <= 8
      add_injury
    end
  end
  window.level.enemies.reject! { |e|
    Gosu::distance(e.x, e.y, @weapon.x, @weapon.y) <= 8
  }
end

#add_stars_scoreObject

add_stars_score

add score when collect stars by player



433
434
435
436
# File 'lib/games_and_rpg_paradise/gui/gosu/garden_hero/core/player/player.rb', line 433

def add_stars_score
  @score += 10
  @stars += 1
end

#attackObject

attack

attack enemies



454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
# File 'lib/games_and_rpg_paradise/gui/gosu/garden_hero/core/player/player.rb', line 454

def attack
  if @weapon.drawing == false
    case face
    when 'left'
      @weapon.x, @weapon.y = x - 4, y + 4
    when 'right'
      @weapon.x, @weapon.y = x + 12, y + 4
    when 'up'
      @weapon.x, @weapon.y = x + 4, y - 8
    when 'down'
      @weapon.x, @weapon.y = x + 4, y + 16
    end
  end
  @weapon.drawing = true
end

#collect_applesObject

collect_apples

collect apples by player



418
419
420
421
422
423
424
425
426
427
428
# File 'lib/games_and_rpg_paradise/gui/gosu/garden_hero/core/player/player.rb', line 418

def collect_apples
  window.level.apples.each { |a|
    if (x - a.x).abs <= 8 && (y - a.y).abs <= 8
      add_apples_score
      @collect_apples.play(looping = false)
    end
  }
  window.level.apples.reject! do |a|
    (x - a.x).abs <= 8 && (y - a.y).abs <= 8
  end
end

#collect_starsObject

collect stars by player



403
404
405
406
407
408
409
410
411
412
413
# File 'lib/games_and_rpg_paradise/gui/gosu/garden_hero/core/player/player.rb', line 403

def collect_stars
  window.level.stars.each do |s|
    if (x - s.x).abs <= 8 && (y - s.y).abs <= 8
      add_stars_score
      @collect.play(looping = false)
    end
  end
  window.level.stars.reject! do |s|
    (x - s.x).abs <= 8 && (y - s.y).abs <= 8
  end
end

#drawObject

#

draw

#


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
# File 'lib/games_and_rpg_paradise/gui/gosu/garden_hero/core/player/player.rb', line 49

def draw
  case face
  # === left
  when 'left'
    @left.draw(@x, @y, 1)
  # === right
  when 'right'
    @right.draw(@x, @y, 1)
  # === up
  when 'up'
    @up.draw(@x, @y, 1)
  # === down
  when 'down'
    @down.draw(@x, @y, 1)
  end
  @ui_star.draw(8, 484, 1)
  @weapon.draw
  size = 16
  @lives.times {
    @ui_lives.draw(632 - size, 484, 1)
    size += 16
  }
  @ui.draw_text("#{@stars}", 24, 484, 1)
  @ui.draw_text("#{@score}", 572 - (Math.log10(@score + 1).to_i) * 8, 483.5, 1)
end

#moveObject

#

move

Player movement logic.

#


80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/games_and_rpg_paradise/gui/gosu/garden_hero/core/player/player.rb', line 80

def move
  if @lives > 0
    walk_on_horizontal_roads
    walk_on_vertical_roads
    collect_stars
    collect_apples
    attack if window.button_down? Gosu::KbSpace
    case @stars
    when 36
      @window.win_game = true
      @win_song.play
      @window.total_score += score
      @stars = 0
    end
  else
    window.level.game_over = true
    @game_over_song.play
  end
  @weapon.update
  add_injury_to_enemies
end

#move_downObject

move_down



396
397
398
399
400
# File 'lib/games_and_rpg_paradise/gui/gosu/garden_hero/core/player/player.rb', line 396

def move_down
  @face = 'down'
  @weapon.last_direction = 'down' if @weapon.drawing == false
  @y += 2
end

#move_leftObject

move_left



376
377
378
379
380
# File 'lib/games_and_rpg_paradise/gui/gosu/garden_hero/core/player/player.rb', line 376

def move_left
  @face = 'left'
  @weapon.last_direction = 'left' if @weapon.drawing == false
  @x -= 2
end

#move_rightObject

move_right



383
384
385
386
387
# File 'lib/games_and_rpg_paradise/gui/gosu/garden_hero/core/player/player.rb', line 383

def move_right
  @face = 'right'
  @weapon.last_direction = 'right' if @weapon.drawing == false
  @x += 2
end

#move_upObject

move_up



390
391
392
393
394
# File 'lib/games_and_rpg_paradise/gui/gosu/garden_hero/core/player/player.rb', line 390

def move_up
  @face = 'up'
  @weapon.last_direction = 'up' if @weapon.drawing == false
  @y -= 2
end

#rebootObject

#

Reboot player.

#


509
510
511
512
513
514
515
516
517
518
# File 'lib/games_and_rpg_paradise/gui/gosu/garden_hero/core/player/player.rb', line 509

def reboot
  case window.level.num
  when 1
    start_point 600, 456
  when 2
    start_point 620, 168
  end
  @lives -= 1
  @stamina = 100
end

#start_point(x, y) ⇒ Object

#

start_point

set player coordinates in new level

#


42
43
44
# File 'lib/games_and_rpg_paradise/gui/gosu/garden_hero/core/player/player.rb', line 42

def start_point(x, y)
  @x, @y = x, y
end

#walk_on_horizontal_roadsObject

#

player walking on horizontal roads

#


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
# File 'lib/games_and_rpg_paradise/gui/gosu/garden_hero/core/player/player.rb', line 105

def walk_on_horizontal_roads
  case window.level.num
  when 1
    if (x >= 82 && x <= 604 && y >= 448 && y <= 460)
      if window.button_down? Gosu::KbLeft
        move_left if @x >= 90
      end
      if window.button_down? Gosu::KbRight
        move_right if @x <= 600
      end
      if window.button_down? Gosu::KbUp
        move_up if @y >= 453
      end
      if window.button_down? Gosu::KbDown
        move_down if @y <= 454
      end
    end

    if (x >= 4 && x <= 604 && y >= 306 && y <= 314)
      if window.button_down? Gosu::KbLeft
        move_left if @x >= 8
      end
      if window.button_down? Gosu::KbRight
        move_right if @x <= 600
      end
      if window.button_down? Gosu::KbUp
        move_up if @y >= 308
      end
      if window.button_down? Gosu::KbDown
        move_down if @y <= 310
      end
    end

    if (x >= 4 && x <= 462 && y >= 44 && y <= 56)
      if window.button_down? Gosu::KbLeft
        move_left if @x >= 8
      end
      if window.button_down? Gosu::KbRight
        move_right if @x <= 458
      end
      if window.button_down? Gosu::KbUp
        move_up if @y >= 52
      end
      if window.button_down? Gosu::KbDown
        move_down if @y <= 53
      end
    end
  when 2
    if (x >= 4 && x <= 632 && y >= 160 && y <= 168)
      if window.button_down? Gosu::KbLeft
        move_left if @x >= 8
      end
      if window.button_down? Gosu::KbRight
        move_right if @x <= 628
      end
      if window.button_down? Gosu::KbUp
        move_up if @y >= 164
      end
      if window.button_down? Gosu::KbDown
        move_down if @y <= 165
      end
    end

    if (x >= 82 && x <= 536 && y >= 436 && y <= 440)
      if window.button_down? Gosu::KbLeft
        move_left if @x >= 88
      end
      if window.button_down? Gosu::KbRight
        move_right if @x <= 532
      end
      if window.button_down? Gosu::KbUp
        move_up if @y >= 438
      end
      if window.button_down? Gosu::KbDown
        move_down if @y <= 438
      end
    end
  end
end

#walk_on_vertical_roadsObject

#

walk_on_vertical_roads

Player walking on vertical roads.

#


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
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
# File 'lib/games_and_rpg_paradise/gui/gosu/garden_hero/core/player/player.rb', line 190

def walk_on_vertical_roads
  case window.level.num
  when 1
    if (x >= 590 && x <= 602 && y >= 312 && y <= 460)
      if window.button_down? Gosu::KbLeft
        move_left if @x >= 598
      end
      if window.button_down? Gosu::KbRight
        move_right if @x <= 600
      end
      if window.button_down? Gosu::KbUp
        move_up if @y >= 316
      end
      if window.button_down? Gosu::KbDown
        move_down if @y <= 454
      end
    end

    if (x >= 546 && x <= 560 && y >= 220 && y <= 314)
      if window.button_down? Gosu::KbLeft
        move_left if @x >= 550
      end
      if window.button_down? Gosu::KbRight
        move_right if @x <= 554
      end
      if window.button_down? Gosu::KbUp
        move_up if @y >= 222
      end
      if window.button_down? Gosu::KbDown
        move_down if @y <= 310
      end
    end

    if (x >= 448 && x <= 460 && y >= 50 && y <= 314)
      if window.button_down? Gosu::KbLeft
        move_left if @x >= 454
      end
      if window.button_down? Gosu::KbRight
        move_right if @x <= 458
      end
      if window.button_down? Gosu::KbUp
        move_up if @y >= 56
      end
      if window.button_down? Gosu::KbDown
        move_down if @y <= 310
      end
    end

    if (x >= 368 && x <= 380 && y >= 160 && y <= 314)
      if window.button_down? Gosu::KbLeft
        move_left if @x >= 374
      end
      if window.button_down? Gosu::KbRight
        move_right if @x <= 376
      end
      if window.button_down? Gosu::KbUp
        move_up if @y >= 164
      end
      if window.button_down? Gosu::KbDown
        move_down if @y <= 310
      end
    end

    if (x >= 80 && x <= 96 && y >= 160 && y <= 314)
      if window.button_down? Gosu::KbLeft
        move_left if @x >= 86
      end
      if window.button_down? Gosu::KbRight
        move_right if @x <= 88
      end
      if window.button_down? Gosu::KbUp
        move_up if @y >= 164
      end
      if window.button_down? Gosu::KbDown
        move_down if @y <= 310
      end
    end

    if (x >= 4 && x <= 16 && y >= 4 && y <= 314)
      if window.button_down? Gosu::KbLeft
        move_left if @x >= 8
      end
      if window.button_down? Gosu::KbRight
        move_right if @x <= 9
      end
      if window.button_down? Gosu::KbUp
        move_up if @y >= 6
      end
      if window.button_down? Gosu::KbDown
        move_down if @y <= 310
      end
    end

    if (x >= 82 && x <= 92 && y >= 428 && y <= 460)
      if window.button_down? Gosu::KbLeft
        move_left if @x >= 88
      end
      if window.button_down? Gosu::KbRight
        move_right if @x <= 90
      end
      if window.button_down? Gosu::KbUp
        move_up if @y >= 430
      end
      if window.button_down? Gosu::KbDown
        move_down if @y <= 454
      end
    end
  when 2
    if (x >= 550 && x <= 556 && y >= 94 && y <= 168)
      if window.button_down? Gosu::KbLeft
        move_left if @x >= 552
      end
      if window.button_down? Gosu::KbRight
        move_right if @x <= 553
      end
      if window.button_down? Gosu::KbUp
        move_up if @y >= 98
      end
      if window.button_down? Gosu::KbDown
        move_down if @y <= 164
      end
    end

    if (x >= 294 && x <= 298 && y >= 94 && y <= 440)
      if window.button_down? Gosu::KbLeft
        move_left if @x >= 296
      end
      if window.button_down? Gosu::KbRight
        move_right if @x <= 297
      end
      if window.button_down? Gosu::KbUp
        move_up if @y >= 98
      end
      if window.button_down? Gosu::KbDown
        move_down if @y <= 436
      end
    end

    if (x >= 70 && x <= 74 && y >= 94 && y <= 168)
      if window.button_down? Gosu::KbLeft
        move_left if @x >= 72
      end
      if window.button_down? Gosu::KbRight
        move_right if @x <= 73
      end
      if window.button_down? Gosu::KbUp
        move_up if @y >= 98
      end
      if window.button_down? Gosu::KbDown
        move_down if @y <= 164
      end
    end

    if (x >= 84 && x <= 90 && y >= 376 && y <= 444)
      if window.button_down? Gosu::KbLeft
        move_left if @x >= 88
      end
      if window.button_down? Gosu::KbRight
        move_right if @x <= 89
      end
      if window.button_down? Gosu::KbUp
        move_up if @y >= 382
      end
      if window.button_down? Gosu::KbDown
        move_down if @y <= 438
      end
    end

    if (x >= 532 && x <= 538 && y >= 376 && y <= 444)
      if window.button_down? Gosu::KbLeft
        move_left if @x >= 535
      end
      if window.button_down? Gosu::KbRight
        move_right if @x <= 536
      end
      if window.button_down? Gosu::KbUp
        move_up if @y >= 382
      end
      if window.button_down? Gosu::KbDown
        move_down if @y <= 438
      end
    end
  end
end