Class: GamesAndRpgParadise::ParticleSimulator::Prey

Inherits:
Entity
  • Object
show all
Defined in:
lib/games_and_rpg_paradise/gui/gosu/particle_simulator/smart_prey.rb,
lib/games_and_rpg_paradise/gui/gosu/particle_simulator/prey.rb

Overview

#

here’s my quickly put together “smart prey” class as an example.

It improved the avg lifetime about 30sec by turning the velocity as the hunter approached. Simple.

#

Constant Summary

Constants inherited from Entity

Entity::CAP, Entity::DAMPEN

Instance Attribute Summary collapse

Attributes inherited from Entity

#vx, #vy, #x

Instance Method Summary collapse

Methods inherited from Entity

#accelerate, #find_closest, #move, #y?

Constructor Details

#initialize(game) ⇒ Prey

#

initialize

#


18
19
20
21
22
# File 'lib/games_and_rpg_paradise/gui/gosu/particle_simulator/prey.rb', line 18

def initialize(game)
  super(game)
  @accel = P_ACCEL
  reset
end

Instance Attribute Details

#eatenObject

Returns the value of attribute eaten.



13
14
15
# File 'lib/games_and_rpg_paradise/gui/gosu/particle_simulator/prey.rb', line 13

def eaten
  @eaten
end

Instance Method Details

#drawObject

#

draw

#


55
56
57
# File 'lib/games_and_rpg_paradise/gui/gosu/particle_simulator/prey.rb', line 55

def draw
  @game.draw_square(@x, @y, 3, 0xff00ff00)
end

#resetObject

#

reset

#


27
28
29
30
31
32
# File 'lib/games_and_rpg_paradise/gui/gosu/particle_simulator/prey.rb', line 27

def reset
  # ======================================================================= #
  # === @eaten
  # ======================================================================= #
  @eaten = false
end

#update(hunters) ⇒ Object

#

update

#


37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/games_and_rpg_paradise/gui/gosu/particle_simulator/prey.rb', line 37

def update(hunters)
  # ======================================================================= #
  # Find the closest hunter and accelerate away from them.
  # ======================================================================= #
  closest = find_closest(hunters)
  # ======================================================================= #
  # take the angle from the hunter to the prey and run along that path
  # ======================================================================= #
  accelerate(
    Gosu.angle(closest.x, closest.y, @x, @y)
  )
  move
  !@eaten # return to verify existence
end