Class: ExampleApp::Avatar

Inherits:
Zif::Sprite show all
Defined in:
app/avatar.rb

Overview

The Avatar is the player controllable sprite.

Constant Summary collapse

DIRECTIONS =

rubocop:disable Layout/ExtraSpacing

{
  [ 0,  1] => :n,
  [ 0, -1] => :s,
  [ 1,  0] => :e,
  [-1,  0] => :w,

  [ 1,  1] => :e,
  [ 1, -1] => :e,
  [-1,  1] => :w,
  [-1, -1] => :w
}.freeze
WALK_SPEED =

rubocop:enable Layout/ExtraSpacing

8

Constants inherited from Zif::Sprite

Zif::Sprite::BLENDMODE

Instance Attribute Summary collapse

Attributes inherited from Zif::Sprite

#a, #angle, #b, #g, #h, #logical_x, #logical_y, #name, #path, #r, #render_target, #source_h, #source_w, #source_x, #source_y, #w, #x, #y, #z_index

Attributes included from Zif::Clickable

#on_mouse_changed, #on_mouse_down, #on_mouse_up

Attributes included from Zif::Actions::Animatable

#animation_sequences, #cur_animation

Attributes included from Zif::Actions::Actionable

#actions, #dirty

Instance Method Summary collapse

Methods inherited from Zif::Sprite

#blend, #blend=, #center, #center_x, #center_y, #clicked?, #color, #color=, #dup_and_assign, #exclude_from_serialize, #hide, #rect, rect_array_to_hash, rect_array_to_source_hash, #rect_hash, rect_hash_to_source_hash, #show, #source_as_rect_hash, #source_center, #source_is_set?, #source_rect, #source_rect_hash, #source_wh, #source_xy, #to_h, #view_actual_size!, #wh, #xy, #zoom_factor

Methods included from Zif::Clickable

#absorb_click?, #clicked?

Methods included from Zif::Actions::Animatable

#new_basic_animation, #new_tiled_animation, #register_animation_sequence, #run_animation_sequence, #stop_animating

Methods included from Zif::Actions::Actionable

#bounce_forever_around, #delayed_action, #fade_in, #fade_out, #fade_out_and_in_forever, #new_action, #perform_actions, #run_action, #running_actions?, #stop_action

Methods included from Zif::Serializable

#exclude_from_serialize, #inspect, #serialize, #to_s

Methods included from Zif::Assignable

#assign

Constructor Details

#initialize(proto, x = 0, y = 0, max_x = 1280, max_y = 720) ⇒ Avatar

Returns a new instance of Avatar.



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'app/avatar.rb', line 23

def initialize(proto, x=0, y=0, max_x=1280, max_y=720)
  super()
  assign(proto.to_h)
  stop_walking

  # Initial position
  @x = x
  @y = y
  @max_x = max_x
  @max_y = max_y

  @map_bounds = [[0, 0], Zif.sub_positions([@max_x, @max_y], wh)]

  new_basic_animation(
    named:               :fly,
    paths_and_durations: [1, 2, 3, 4, 3, 2].map { |i| ["dragon_#{i}", 4] }
  )
end

Instance Attribute Details

#map_boundsObject

Returns the value of attribute map_bounds.



4
5
6
# File 'app/avatar.rb', line 4

def map_bounds
  @map_bounds
end

#max_xObject

Returns the value of attribute max_x.



4
5
6
# File 'app/avatar.rb', line 4

def max_x
  @max_x
end

#max_yObject

Returns the value of attribute max_y.



4
5
6
# File 'app/avatar.rb', line 4

def max_y
  @max_y
end

#movement_actionObject

Returns the value of attribute movement_action.



5
6
7
# File 'app/avatar.rb', line 5

def movement_action
  @movement_action
end

#moving_toObject

Returns the value of attribute moving_to.



5
6
7
# File 'app/avatar.rb', line 5

def moving_to
  @moving_to
end

#walkingObject

Returns the value of attribute walking.



5
6
7
# File 'app/avatar.rb', line 5

def walking
  @walking
end

Instance Method Details

#cap_movement(to) ⇒ Object



48
49
50
51
52
53
# File 'app/avatar.rb', line 48

def cap_movement(to)
  [
    [[@x + to[0], @map_bounds[0][0]].max, @map_bounds[1][0]].min.floor,
    [[@y + to[1], @map_bounds[0][1]].max, @map_bounds[1][1]].min.floor
  ]
end

#moved_this_tick?Boolean

Returns:

  • (Boolean)


42
43
44
# File 'app/avatar.rb', line 42

def moved_this_tick?
  @dirty
end

#perform_tickObject



46
# File 'app/avatar.rb', line 46

def perform_tick; end

#start_walking(to = nil) ⇒ Object



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'app/avatar.rb', line 55

def start_walking(to=nil)
  @walking = true
  return unless to

  relative_to = Zif.sub_positions(to, xy)
  offset_to = Zif.sub_positions(relative_to, Zif.position_math(:idiv, wh, [2, 2]))

  @moving_to = cap_movement(offset_to)

  distance = Zif.distance(*xy, *@moving_to)
  duration = distance.fdiv(WALK_SPEED).ceil
  stop_action(@movement_action) if @movement_action

  @movement_action = new_action(
    {
      x: @moving_to[0],
      y: @moving_to[1]
    },
    duration: duration,
    easing:   :smooth_stop
  ) { stop_walking }

  run_action(@movement_action)
end

#stop_walkingObject



80
81
82
83
# File 'app/avatar.rb', line 80

def stop_walking
  @walking = false
  @moving_to = nil
end