Class: GamesAndRpgParadise::Duck

Inherits:
Object
  • Object
show all
Defined in:
lib/games_and_rpg_paradise/gui/gosu/duck_hunt_calculator/duck.rb

Overview

GamesAndRpgParadise::Duck

Constant Summary collapse

DUCK =
#

DUCK

#
Gosu::Image.load_tiles(::GamesAndRpgParadise.base_directory?+'images/duck_hunt_calculator/duck.png', 48, 48)
DUCK_SIGN =
#

DUCK_SIGN

#
Gosu::Image.new(::GamesAndRpgParadise.base_directory?+'images/duck_hunt_calculator/duck_sign.png')
USE_THIS_FONT =
#

USE_THIS_FONT

#
Gosu::Font.new(10)
FALL_SOUND =
#

FALL_SOUND

#
Gosu::Sample.new(::GamesAndRpgParadise.base_directory?+'gui/gosu/duck_hunt_calculator/fall.mp3')
DUCK_SOUND =
#

DUCK_SOUND

#
Gosu::Sample.new(::GamesAndRpgParadise.base_directory?+'gui/gosu/duck_hunt_calculator/duck.ogg')

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(number) ⇒ Duck

#

initialize

#


41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/games_and_rpg_paradise/gui/gosu/duck_hunt_calculator/duck.rb', line 41

def initialize(number)
  @position_x = (Gosu.random(0, 2) > 1) ? 0 : 240 # left or right
  @position_y = Gosu.random(0, 140)

  @vx = 1  if @position_x == 0
  @vx = -1 if @position_x == 240

  if @position_y > 90
    @vy = -1  
  elsif @position_y < 90
    @vy = 1
  else
    @vy = Gosu.random(-1, 2)
  end   
  @number = number
  play_the_duck_sound
  reset
end

Instance Attribute Details

#to_removeObject (readonly)

Returns the value of attribute to_remove.



15
16
17
# File 'lib/games_and_rpg_paradise/gui/gosu/duck_hunt_calculator/duck.rb', line 15

def to_remove
  @to_remove
end

Instance Method Details

#drawObject

#

draw

#


138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
# File 'lib/games_and_rpg_paradise/gui/gosu/duck_hunt_calculator/duck.rb', line 138

def draw
  frame = @frame.floor
  frame += 3 if @vx == 1
  frame += 6 if @vx == -1
  frame += 9 if @dead
  DUCK[frame].draw(@position_x, @position_y, 30)
  unless @dead
    # the spritesheet is not symetrical so we have to adjust position regarding x velocity
    if @vx > 0
      DUCK_SIGN.draw(@position_x + 10, @position_y + 35, 29)
      USE_THIS_FONT.draw_text(@number, @position_x + 13, @position_y + 38, 29)
    else
      DUCK_SIGN.draw(@position_x + 24, @position_y + 35, 29)
      USE_THIS_FONT.draw_text(@number, @position_x + 26, @position_y + 38, 29)
    end
  end
end

#play_the_duck_soundObject

#

play_the_duck_sound

#


73
74
75
# File 'lib/games_and_rpg_paradise/gui/gosu/duck_hunt_calculator/duck.rb', line 73

def play_the_duck_sound
  DUCK_SOUND.play
end

#resetObject

#

reset

#


63
64
65
66
67
68
# File 'lib/games_and_rpg_paradise/gui/gosu/duck_hunt_calculator/duck.rb', line 63

def reset
  @boring_time = 0
  @frame = 0
  @dead = false
  @spawned = false
end

#shoot(x, y) ⇒ Object

#

shoot

#


119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
# File 'lib/games_and_rpg_paradise/gui/gosu/duck_hunt_calculator/duck.rb', line 119

def shoot(x, y)
  unless @dead
    if x >= @position_x + 14 and x <= @position_x + 48 - 14
      if y >= @position_y + 14 and y <= @position_y + 48 - 14
        @dead = true
        @vx = 0
        @vy = 4
        @frame = 0
        FALL_SOUND.play
        return @number
      end
    end
  end
  return nil
end

#updateObject

#

update

#


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
# File 'lib/games_and_rpg_paradise/gui/gosu/duck_hunt_calculator/duck.rb', line 80

def update
  speed = 1.5
  @frame += 0.1

  if @frame > 2
    @frame = 0
    @frame = 1 if @dead
  end

  @position_x += @vx * speed
  @position_y += @vy * (speed * 0.2)

  # we check boundaries when the duck has entered the scene
  @spawned = true if @position_x >= 0 and @position_x <= 240 - 48 and @position_y >= 0 and @position_y <= 140
  if @spawned and !@dead
    @vx = -@vx if @position_x <= 0 or @position_x > 240 - 48
    @vy = -@vy if @position_y <= 0 or @position_y > 140
  end

  # we want to remove a duck that is dead and bottom of screen
  if @dead and @position_y > 180
    @to_remove = true
  end

  # we'll set to remove a duck that got bored of flying
  unless @dead
    @boring_time += 1
    if @boring_time > 400
      @vy = -1
      if @position_y < -48
        @to_remove = true
      end
    end
  end
end