Class: Actor
- Inherits:
-
Object
- Object
- Actor
- Includes:
- Gravitier, Jumper, MoveByCursor, MoveByDirection, Mover, UserInputs
- Defined in:
- lib/fantasy/actor.rb
Overview
Represents one active entity in the game. An actor can be the Player sprite. Or one of the enemies. Or the bullet. An actor can also be a tree, or a wall
Instance Attribute Summary collapse
-
#collision_with ⇒ Array, String
Array of strings (or "all").
-
#direction ⇒ Coordinates
Controls the direction in which this Actor will move in the next frame.
-
#gravity ⇒ Float
Controls constant force this Actor will receive on each frame.
-
#jump_force ⇒ Float
Controls impulse this Actor will receive when the (see #jump) is triggered.
-
#layer ⇒ Integer
In which layer the image of the Actor is rendered.
-
#name ⇒ String
The value to internal name of this Actor.
-
#position ⇒ Coordinates
Coordinates object where x and y represent the position of the Actor in the World (no necessarily in the Screen).
-
#scale ⇒ Float
The value to scale the image of the Actor when drawn.
-
#solid ⇒ Boolean
When
true
the Actor will cause and respond to collisions. -
#speed ⇒ Float
Controls the pixels per second which this Actor will move in the next frame.
Instance Method Summary collapse
-
#destroy ⇒ Object
Destroy this Actor is not longer, moved, or rendered, or cause any collision.
-
#height ⇒ Fixnum
The Actor height in pixels.
-
#image=(image_name) ⇒ Object
Set a new image to the Actor.
-
#initialize(image_name) ⇒ Actor
constructor
Generate an Actor with all the default attribute values.
-
#on_after_move(&block) ⇒ Object
The block to be executed after each frame.
-
#on_after_move_do ⇒ Object
This method is triggered after each frame.
-
#on_collision(&block) ⇒ Object
The block to be executed when Actor collides with another Actor.
-
#on_collision_do(other) ⇒ Object
This method is triggered when Actor collides with another Actor.
-
#on_destroy(&block) ⇒ Object
The block to be executed before the Actor is destroyed.
-
#on_destroy_do ⇒ Object
This method is triggered before the Actor is destroyed.
-
#on_floor(&block) ⇒ Object
The block to be executed when the Actor touches floor.
-
#on_floor_do ⇒ Object
This method is triggered when the Actor touches floor.
-
#on_jumping(&block) ⇒ Object
The block to be executed when the Actor starts jumping.
-
#on_jumping_do ⇒ Object
This method is triggered when the Actor starts jumping.
-
#solid? ⇒ Boolean
The value of
@solid
. -
#width ⇒ Fixnum
The Actor width in pixels.
Methods included from UserInputs
#on_click, #on_click_do, #on_cursor_down, #on_cursor_down_do, #on_cursor_left, #on_cursor_left_do, #on_cursor_right, #on_cursor_right_do, #on_cursor_up, #on_cursor_up_do, #on_mouse_button_left, #on_mouse_button_left_do, #on_space_bar, #on_space_bar_do
Methods included from Jumper
Methods included from Gravitier
Methods included from Mover
#add_force, #apply_forces, #impulse
Methods included from MoveByDirection
Methods included from MoveByCursor
Constructor Details
#initialize(image_name) ⇒ Actor
Generate an Actor with all the default attribute values
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 |
# File 'lib/fantasy/actor.rb', line 236 def initialize(image_name) @image_name = image_name @image = Image.new(image_name) @name = image_name @position = Coordinates.zero @direction = Coordinates.zero @speed = 0 @scale = 1 @solid = true @draggable_on_debug = true @dragging = false @dragging_offset = nil @layer = 0 @gravity = 0 @jump_force = 0 @collision_with = "all" @on_floor = false @on_after_move_callback = nil @on_collision_callback = nil @on_destroy_callback = nil @on_jumping_callback = nil @on_floor_callback = nil Global.actors << self end |
Instance Attribute Details
#collision_with ⇒ Array, String
Array of strings (or "all"). Represents with which other solid Actors this Actor collide.
Default "all"
.
218 219 220 |
# File 'lib/fantasy/actor.rb', line 218 def collision_with @collision_with end |
#direction ⇒ Coordinates
The the pixels per second is represented by the @speed
attribute
Controls the direction in which this Actor will move in the next frame.
Default Coordinates.zero
.
96 97 98 |
# File 'lib/fantasy/actor.rb', line 96 def direction @direction end |
#gravity ⇒ Float
The the direction will be Coordinates.down
Controls constant force this Actor will receive on each frame.
Default 0
.
135 136 137 |
# File 'lib/fantasy/actor.rb', line 135 def gravity @gravity end |
#jump_force ⇒ Float
The the direction will be Coordinates.up
Controls impulse this Actor will receive when the (see #jump) is triggered.
Default 0
.
122 123 124 |
# File 'lib/fantasy/actor.rb', line 122 def jump_force @jump_force end |
#layer ⇒ Integer
In which layer the image of the Actor is rendered. Smaller numbers are rendered behind higher numbers.
Default 0
.
202 203 204 |
# File 'lib/fantasy/actor.rb', line 202 def layer @layer end |
#name ⇒ String
The value to internal name of this Actor.
It is useful for collision management for example.
Default (same as image_name
).
190 191 192 |
# File 'lib/fantasy/actor.rb', line 190 def name @name end |
#position ⇒ Coordinates
Coordinates object where x and y represent the position of the Actor in the World (no necessarily in the Screen).
Default Coordinates.zero
.
83 84 85 |
# File 'lib/fantasy/actor.rb', line 83 def position @position end |
#scale ⇒ Float
this value affects the attributes width
and height
The value to scale the image of the Actor when drawn.
If the value is 2
the image will rendered at double of size.
If the value is 0.5
the image will rendered at half of size.
Default 1
.
165 166 167 |
# File 'lib/fantasy/actor.rb', line 165 def scale @scale end |
#solid ⇒ Boolean
When true
the Actor will cause and respond to collisions.
When false
the Actor won't cause neither respond to collisions.
Default true
.
150 151 152 |
# File 'lib/fantasy/actor.rb', line 150 def solid @solid end |
#speed ⇒ Float
The the direction is represented by the @direction
attribute
Controls the pixels per second which this Actor will move in the next frame.
Default 0
.
109 110 111 |
# File 'lib/fantasy/actor.rb', line 109 def speed @speed end |
Instance Method Details
#destroy ⇒ Object
Destroy this Actor is not longer, moved, or rendered, or cause any collision.
369 370 371 372 |
# File 'lib/fantasy/actor.rb', line 369 def destroy on_destroy_do Global.actors.delete(self) end |
#height ⇒ Fixnum
Returns the Actor height in pixels.
282 283 284 |
# File 'lib/fantasy/actor.rb', line 282 def height @image.height * @scale end |
#image=(image_name) ⇒ Object
Set a new image to the Actor.
272 273 274 |
# File 'lib/fantasy/actor.rb', line 272 def image=(image_name) @image = Image.new(image_name) end |
#on_after_move(&block) ⇒ Object
The block to be executed after each frame
424 425 426 |
# File 'lib/fantasy/actor.rb', line 424 def on_after_move(&block) @on_after_move_callback = block end |
#on_after_move_do ⇒ Object
This method is triggered after each frame
487 488 489 |
# File 'lib/fantasy/actor.rb', line 487 def on_after_move_do instance_exec(&@on_after_move_callback) unless @on_after_move_callback.nil? end |
#on_collision(&block) ⇒ Object
The block to be executed when Actor collides with another Actor
437 438 439 |
# File 'lib/fantasy/actor.rb', line 437 def on_collision(&block) @on_collision_callback = block end |
#on_collision_do(other) ⇒ Object
This method is triggered when Actor collides with another Actor
501 502 503 |
# File 'lib/fantasy/actor.rb', line 501 def on_collision_do(other) instance_exec(other, &@on_collision_callback) unless @on_collision_callback.nil? end |
#on_destroy(&block) ⇒ Object
The block to be executed before the Actor is destroyed
448 449 450 |
# File 'lib/fantasy/actor.rb', line 448 def on_destroy(&block) @on_destroy_callback = block end |
#on_destroy_do ⇒ Object
This method is triggered before the Actor is destroyed
513 514 515 |
# File 'lib/fantasy/actor.rb', line 513 def on_destroy_do instance_exec(&@on_destroy_callback) unless @on_destroy_callback.nil? end |
#on_floor(&block) ⇒ Object
The block to be executed when the Actor touches floor
471 472 473 |
# File 'lib/fantasy/actor.rb', line 471 def on_floor(&block) @on_floor_callback = block end |
#on_floor_do ⇒ Object
This method is triggered when the Actor touches floor
538 539 540 |
# File 'lib/fantasy/actor.rb', line 538 def on_floor_do instance_exec(&@on_floor_callback) unless @on_floor_callback.nil? end |
#on_jumping(&block) ⇒ Object
The block to be executed when the Actor starts jumping
459 460 461 |
# File 'lib/fantasy/actor.rb', line 459 def on_jumping(&block) @on_jumping_callback = block end |
#on_jumping_do ⇒ Object
This method is triggered when the Actor starts jumping
525 526 527 |
# File 'lib/fantasy/actor.rb', line 525 def on_jumping_do instance_exec(&@on_jumping_callback) unless @on_jumping_callback.nil? end |
#solid? ⇒ Boolean
Returns the value of @solid
.
287 288 289 |
# File 'lib/fantasy/actor.rb', line 287 def solid? @solid end |
#width ⇒ Fixnum
Returns the Actor width in pixels.
277 278 279 |
# File 'lib/fantasy/actor.rb', line 277 def width @image.width * @scale end |