Class: Enemy

Inherits:
Object
  • Object
show all
Defined in:
lib/games_paradise/gui/gosu/garden_hero/core/level/enemy.rb

Overview

#

Enemy - main enemy class

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(window, x, y, face = 'right') ⇒ Enemy

Returns a new instance of Enemy.



8
9
10
11
12
13
14
15
# File 'lib/games_paradise/gui/gosu/garden_hero/core/level/enemy.rb', line 8

def initialize window, x, y, face = 'right'
  @window, @x, @y, @face = window, x, y, face
  @left = Gosu::Image.new window, "images/enemy/enemy-left.png", false
  @right = Gosu::Image.new window, "images/enemy/enemy-right.png", false
  @up = Gosu::Image.new window, "images/enemy/enemy-up.png", false
  @down = Gosu::Image.new window, "images/enemy/enemy-down.png", false
  @right_x, @left_x, @down_y, @up_y = @x + 50, @x - 50, @y + 50, @y - 50
end

Instance Attribute Details

#down_yObject (readonly)

Returns the value of attribute down_y.



17
18
19
# File 'lib/games_paradise/gui/gosu/garden_hero/core/level/enemy.rb', line 17

def down_y
  @down_y
end

#faceObject (readonly)

Returns the value of attribute face.



17
18
19
# File 'lib/games_paradise/gui/gosu/garden_hero/core/level/enemy.rb', line 17

def face
  @face
end

#left_xObject (readonly)

Returns the value of attribute left_x.



17
18
19
# File 'lib/games_paradise/gui/gosu/garden_hero/core/level/enemy.rb', line 17

def left_x
  @left_x
end

#right_xObject (readonly)

Returns the value of attribute right_x.



17
18
19
# File 'lib/games_paradise/gui/gosu/garden_hero/core/level/enemy.rb', line 17

def right_x
  @right_x
end

#up_yObject (readonly)

Returns the value of attribute up_y.



17
18
19
# File 'lib/games_paradise/gui/gosu/garden_hero/core/level/enemy.rb', line 17

def up_y
  @up_y
end

#windowObject (readonly)

Returns the value of attribute window.



17
18
19
# File 'lib/games_paradise/gui/gosu/garden_hero/core/level/enemy.rb', line 17

def window
  @window
end

#xObject (readonly)

Returns the value of attribute x.



17
18
19
# File 'lib/games_paradise/gui/gosu/garden_hero/core/level/enemy.rb', line 17

def x
  @x
end

#yObject (readonly)

Returns the value of attribute y.



17
18
19
# File 'lib/games_paradise/gui/gosu/garden_hero/core/level/enemy.rb', line 17

def y
  @y
end

Instance Method Details

#drawObject

draw



20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/games_paradise/gui/gosu/garden_hero/core/level/enemy.rb', line 20

def draw
  case face
  when 'left'
    @left.draw(@x, @y, 1)
  when 'right'
    @right.draw(@x, @y, 1)
  when 'up'
    @up.draw(@x, @y, 1)
  when 'down'
    @down.draw(@x, @y, 1)
  end
end

#moveObject

enemy’s movement logic



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/games_paradise/gui/gosu/garden_hero/core/level/enemy.rb', line 34

def move
  case face
  when 'right'
    @x += 0.5 if @x <= right_x
    @face = 'left' if @x == right_x
  when 'left'
    @x -= 0.5 if @x >= left_x
    @face = 'right' if @x == left_x
  when 'down'
    @y += 0.5 if @y <= down_y
    @face = 'up' if @y == down_y
  when 'up'
    @y -= 0.5 if @y >= up_y
    @face = 'down' if @y == up_y
  end
end