Class: Actor

Inherits:
Object
  • Object
show all
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

Examples:

Actor can be used directly

player = Actor.new("warrior") # ./images/warrior.png
player.position = Coordinates.new(100, 100)
player.solid = true
player.speed = 200
player.layer = 1
player.move_with_cursors
player.collision_with = ["enemy", "bullets"] # default "all"

player.on_collision do |other|
  if other.name == "enemy"
    player.destroy
  end
end

player.on_after_move do
  if player.position.x > Global.screen_width
    player.position.x = Global.screen_width
  end

  if player.position.x < 0
    player.position.x = 0
  end
end

Or in a subclass:

class Player < Actor
  def initialize
    super("warrior") # ./images/warrior.png
    @position = Coordinates.new(100, 100)
    @solid = true
    @speed = 200
    @layer = 1
    @direction = Coordinates.zero
    @collision_with = ["enemy", "bullets"] # default "all"
    move_with_cursors
  end

  on_collision do |other|
    if other.name == "enemy"
      destroy
    end
  end

  on_after_move do
    if @position.x > Global.screen_width
      @position.x = Global.screen_width
    end

    if @position.x < 0
      @position.x = 0
    end
  end
end

Instance Attribute Summary collapse

Instance Method Summary collapse

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

#jump

Methods included from Gravitier

#add_force_by_gravity

Methods included from Mover

#add_force, #apply_forces, #impulse

Methods included from MoveByDirection

#move_by_direction

Methods included from MoveByCursor

#move_with_cursors

Constructor Details

#initialize(image_name) ⇒ Actor

Generate an Actor with all the default attribute values

Examples:

Generate an Actor

actor = Actor.new("image")
actor.position # => Coordinates.zero
actor.direction # => Coordinates.zero
actor.speed # => 0
actor.scale # => 1
actor.solid # => true
actor.draggable_on_debug # => true
actor.layer # => 0
actor.gravity # => 0
actor.jump_force # => 0
actor.collision_with # => "all"

Parameters:

  • image_name (string)

    the name of the image file from ./images/*



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_withArray, String

Array of strings (or "all"). Represents with which other solid Actors this Actor collide.

Default "all".

Examples:

Set with which other Actors this Actor is colliding:

actor = Actor.new("image")
actor.collision_with = ["enemy", "bullet"]

Set this Actors collides with all other Actors

actor = Actor.new("image")
actor.collision_with = "all"

Returns:

  • (Array, String)

    the actual list of names of Actors to collide with



218
219
220
# File 'lib/fantasy/actor.rb', line 218

def collision_with
  @collision_with
end

#directionCoordinates

Note:

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.

Examples:

Set direction

actor = Actor.new("image")
actor.direction = Coordinates.right

Returns:



96
97
98
# File 'lib/fantasy/actor.rb', line 96

def direction
  @direction
end

#gravityFloat

Note:

The the direction will be Coordinates.down

Controls constant force this Actor will receive on each frame.

Default 0.

Examples:

Set gravity

actor = Actor.new("image")
actor.gravity = 10

Returns:

  • (Float)

    the actual gravity



135
136
137
# File 'lib/fantasy/actor.rb', line 135

def gravity
  @gravity
end

#jump_forceFloat

Note:

The the direction will be Coordinates.up

Controls impulse this Actor will receive when the (see #jump) is triggered.

Default 0.

Examples:

Set jump_force

actor = Actor.new("image")
actor.jump_force = 100

Returns:

  • (Float)

    the actual jump_force



122
123
124
# File 'lib/fantasy/actor.rb', line 122

def jump_force
  @jump_force
end

#layerInteger

In which layer the image of the Actor is rendered. Smaller numbers are rendered behind higher numbers.

Default 0.

Examples:

Set layer

actor = Actor.new("image")
actor.layer = -1

Returns:

  • (Integer)

    the actual layer



202
203
204
# File 'lib/fantasy/actor.rb', line 202

def layer
  @layer
end

#nameString

The value to internal name of this Actor.

It is useful for collision management for example.

Default (same as image_name).

Examples:

Set name

actor = Actor.new("image")
actor.name = "spaceship"

Used in collision trigger

actor = Actor.new("image")
actor.on_collision do |other|
  if other.name == "enemy"
    # damage
  end
end

Used with collision_with

actor = Actor.new("image")
actor.collision_with = ["enemy", "bullet"]

Returns:

  • (String)

    the actual name



190
191
192
# File 'lib/fantasy/actor.rb', line 190

def name
  @name
end

#positionCoordinates

Coordinates object where x and y represent the position of the Actor in the World (no necessarily in the Screen).

Default Coordinates.zero.

Examples:

Setting position

actor = Actor.new("image")
actor.position = Coordinates.new(10, 20)
actor.position.x # => 10

Modify position

actor.position.x +=  1
actor.position.x # => 11

Returns:



83
84
85
# File 'lib/fantasy/actor.rb', line 83

def position
  @position
end

#scaleFloat

Note:

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.

Examples:

Set scale

actor = Actor.new("image")
actor.scale = 6

Returns:

  • (Float)

    the actual scale



165
166
167
# File 'lib/fantasy/actor.rb', line 165

def scale
  @scale
end

#solidBoolean

When true the Actor will cause and respond to collisions.

When false the Actor won't cause neither respond to collisions.

Default true.

Examples:

Set solid

actor = Actor.new("image")
actor.solid = false

Parameters:

  • solid (true, false)

    only true or false

Returns:

  • (Boolean)

    the actual solid value



150
151
152
# File 'lib/fantasy/actor.rb', line 150

def solid
  @solid
end

#speedFloat

Note:

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.

Examples:

Set speed

actor = Actor.new("image")
actor.speed = 10

Returns:

  • (Float)

    the actual speed



109
110
111
# File 'lib/fantasy/actor.rb', line 109

def speed
  @speed
end

Instance Method Details

#destroyObject

Destroy this Actor is not longer, moved, or rendered, or cause any collision.

Examples:

Destroy an Actor

actor = Actor.new("image")
actor.destroy


369
370
371
372
# File 'lib/fantasy/actor.rb', line 369

def destroy
  on_destroy_do
  Global.actors.delete(self)
end

#heightFixnum

Returns the Actor height in pixels.

Returns:

  • (Fixnum)

    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.

Examples:

Set a new image

actor = Actor.new("player_walk")
actor.image_name = "player_jump"

Parameters:

  • image_name (String)

    The image file from ./images/*



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

Examples:

Limit Actor movement horizontally

actor = Actor.new("image")
actor.on_after_move do
  if actor.position.x > 100
    actor.position.x = 100
  end
end


424
425
426
# File 'lib/fantasy/actor.rb', line 424

def on_after_move(&block)
  @on_after_move_callback = block
end

#on_after_move_doObject

This method is triggered after each frame

Examples:

Limit Actor movement horizontally

class Player < Actor
  def on_after_move_do
    if @position.x > 100
      @position.x = 100
    end
  end
end


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

Examples:

Collision detected with "bullet"

actor = Actor.new("image")
actor.on_collision do |other|
  if other.name == "bullet"
    actor.destroy
  end
end


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

Examples:

Limit Actor movement horizontally

class Player < Actor
  def on_collision_do(other)
    if other.name == "bullet"
      destroy
    end
  end
end


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

Examples:

Executes when destroyed

actor = Actor.new("image")
actor.on_destroy do
  Sound.play("explosion")
end


448
449
450
# File 'lib/fantasy/actor.rb', line 448

def on_destroy(&block)
  @on_destroy_callback = block
end

#on_destroy_doObject

This method is triggered before the Actor is destroyed

Examples:

Executes when destroyed

class Player < Actor
  def on_destroy_do
    Sound.play("explosion")
  end
end


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

Examples:

Change image when jumping

actor = Actor.new("walk")
actor.on_floor do
  actor.image_name = "land"
  Clock.new { actor.image_name = "walk" }.run_on(seconds: 0.8)
end


471
472
473
# File 'lib/fantasy/actor.rb', line 471

def on_floor(&block)
  @on_floor_callback = block
end

#on_floor_doObject

This method is triggered when the Actor touches floor

Examples:

Change image when jumping

class Player < Actor
  def.on_floor_do
    self.image_name = "land"
    Clock.new { self.image_name = "walk" }.run_on(seconds: 0.8)
  end
end


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

Examples:

Change image when jumping

actor = Actor.new("walk")
actor.on_jumping do
  actor.image_name = "jump"
end


459
460
461
# File 'lib/fantasy/actor.rb', line 459

def on_jumping(&block)
  @on_jumping_callback = block
end

#on_jumping_doObject

This method is triggered when the Actor starts jumping

Examples:

Change image when jumping

class Player < Actor
  def on_jumping_do
    self.image_name = "jump"
  end
end


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.

Returns:

  • (Boolean)

    the value of @solid



287
288
289
# File 'lib/fantasy/actor.rb', line 287

def solid?
  @solid
end

#widthFixnum

Returns the Actor width in pixels.

Returns:

  • (Fixnum)

    the Actor width in pixels



277
278
279
# File 'lib/fantasy/actor.rb', line 277

def width
  @image.width * @scale
end