Class: GamesAndRpgParadise::ParticleSimulator::Entity

Inherits:
Object
  • Object
show all
Defined in:
lib/games_and_rpg_paradise/gui/gosu/particle_simulator/entity.rb

Direct Known Subclasses

Hunter, Prey

Constant Summary collapse

CAP =
#

CAP

#
0.99
DAMPEN =
#

DAMPEN

#
0.9

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(game) ⇒ Entity

#

initialize

#


31
32
33
34
35
# File 'lib/games_and_rpg_paradise/gui/gosu/particle_simulator/entity.rb', line 31

def initialize(game)
  @game = game
  @x, @y = rand(WIDTH), rand(HEIGHT)
  @vx = @vy = 0.0
end

Instance Attribute Details

#vxObject

Returns the value of attribute vx.



26
27
28
# File 'lib/games_and_rpg_paradise/gui/gosu/particle_simulator/entity.rb', line 26

def vx
  @vx
end

#vyObject

Returns the value of attribute vy.



26
27
28
# File 'lib/games_and_rpg_paradise/gui/gosu/particle_simulator/entity.rb', line 26

def vy
  @vy
end

#xObject (readonly)

set to 1.0 if the walls are trampolines



25
26
27
# File 'lib/games_and_rpg_paradise/gui/gosu/particle_simulator/entity.rb', line 25

def x
  @x
end

Instance Method Details

#accelerate(dir) ⇒ Object

#

accelerate

#


97
98
99
100
# File 'lib/games_and_rpg_paradise/gui/gosu/particle_simulator/entity.rb', line 97

def accelerate(dir)
  @vx += Gosu.offset_x(dir, @accel)
  @vy += Gosu.offset_y(dir, @accel)
end

#drawObject

#

draw

#


75
76
# File 'lib/games_and_rpg_paradise/gui/gosu/particle_simulator/entity.rb', line 75

def draw
end

#find_closest(targets) ⇒ Object

#

find_closest

#


81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/games_and_rpg_paradise/gui/gosu/particle_simulator/entity.rb', line 81

def find_closest(targets)
  closest = targets.first
  dist = Gosu.distance(closest.x, closest.y, @x, @y)
  targets.each { |tar|
    new_dist = Gosu.distance(tar.x, tar.y, @x, @y)
    if new_dist < dist
      closest = tar
      dist = new_dist
    end
  }
  return closest
end

#moveObject

#

move

#


40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/games_and_rpg_paradise/gui/gosu/particle_simulator/entity.rb', line 40

def move
  # ======================================================================= #
  # Degrade the velocity so that it will be capped.
  # ======================================================================= #
  @vx = @vx * CAP
  @vy = @vy * CAP
  
  # ======================================================================= #
  # bounce off the walls
  # ======================================================================= #
  @vx = (@x < 0 or @x > WIDTH) ? -(@vx) * DAMPEN : @vx
  @vy = (@y < 0 or @y > HEIGHT) ? -(@vy) * DAMPEN : @vy
  
  @x = WIDTH.to_f if @x > WIDTH
  @x = 0.0 if @x < 0
  @y = HEIGHT.to_f if @y > HEIGHT
  @y = 0.0 if @y < 0
  
  # ======================================================================= #
  # put it together
  # ======================================================================= #
  @x += @vx
  @y += @vy
end

#y?Boolean Also known as: y

#

y?

#

Returns:

  • (Boolean)


68
69
70
# File 'lib/games_and_rpg_paradise/gui/gosu/particle_simulator/entity.rb', line 68

def y?
  @y
end