Class: ExampleApp::CompoundSpriteTest

Inherits:
ZifExampleScene show all
Includes:
Zif::Traceable
Defined in:
app/scenes/compound_sprite_test.rb

Overview

Demonstrating usage of a CompoundSprite with actions

Instance Attribute Summary collapse

Attributes included from Zif::Traceable

#tracer_service_name

Attributes inherited from ZifExampleScene

#next_scene, #scene_timer, #tracer_service_name

Instance Method Summary collapse

Methods included from Zif::Traceable

#mark, #mark_and_print, #mark_prefix, #tracer

Methods inherited from ZifExampleScene

#display_context_labels, #display_timer_bar

Methods inherited from Zif::Scene

#unload_scene

Methods included from Zif::Serializable

#exclude_from_serialize, #inspect, #serialize, #to_s

Constructor Details

#initializeCompoundSpriteTest

rubocop:disable Metrics/AbcSize



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
# File 'app/scenes/compound_sprite_test.rb', line 10

def initialize
  super
  @tracer_service_name = :tracer
  @next_scene = :ui_sample

  @compound_sprite = Zif::CompoundSprite.new.tap do |s|
    s.name = 'Test Sprite'
    s.x = 100
    s.y = 100
    s.w = 800
    s.h = 400
    s.source_x = 0
    s.source_y = 0
    s.source_w = 800
    s.source_h = 400
  end

  @outline = $game.services[:sprite_registry].construct(:white_1).tap do |s|
    s.x = 100
    s.y = 100
    s.w = 800
    s.h = 400
    s.source_x = 0
    s.source_y = 0
    s.source_w = 800
    s.source_h = 400
    s.a = 30
  end

  @dragons = [[0, 50], [100, 150], [0, 250]].map do |pos|
    dragon = $game.services[:sprite_registry].construct('dragon_1').tap do |s|
      s.name = "dragon_#{pos.inspect}"
      s.x = pos[0]
      s.y = pos[1]
    end

    finish_pos = 1000 - pos[0]

    dragon.run_action(
      Zif::Actions::Sequence.new(
        [
          dragon.new_action(
            {x: finish_pos}, duration: 10.seconds, easing: :smooth_start
          ) { dragon.flip_horizontally = true },
          dragon.new_action(
            {x: pos[0]}, duration: 10.seconds, easing: :smooth_stop
          ) { dragon.flip_horizontally = false }
        ],
        repeat: :forever
      )
    )

    dragon.run_action(
      Zif::Actions::Sequence.new(
        [
          dragon.new_action({y: pos[1] + 300}, duration: 5.seconds, easing: :smooth_start),
          dragon.new_action({y: pos[1] - 300}, duration: 5.seconds, easing: :smooth_stop)
        ],
        repeat: :forever
      )
    )

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

    dragon.run_animation_sequence(:fly)

    dragon
  end

  @compound_sprite.sprites += @dragons

  50.times do |i|
    _wait, layer = i.divmod(10)
    pixie = Pixie.new.tap do |s|
      s.x = @dragons[1].x
      s.y = @dragons[1].y
      s.a = 0
    end

    # Start spraying
    pixie.run_action(
      pixie.delayed_action(rand.seconds) do
        pixie_spray(pixie, layer)
      end
    )

    @compound_sprite.sprites << pixie
  end

  dragon_label = Zif::UI::Label.new('A Thunder of Dragons', size: 0, alignment: :center).tap do |label|
    label.x = 50
    label.y = 0
    label.r = 255
    label.g = 255
    label.b = 255
    label.a = 255
  end

  dragon_label.run_action(
    Zif::Actions::Sequence.new(
      [
        dragon_label.new_action({x: 1000}, duration: 10.seconds, easing: :smooth_start),
        dragon_label.new_action({x: 0   }, duration: 10.seconds, easing: :smooth_stop) # rubocop:disable Layout/ExtraSpacing
      ],
      repeat: :forever
    )
  )

  @compound_sprite.labels << dragon_label

  [@outline, @compound_sprite].each do |sprite|
    sprite.run_action(
      Zif::Actions::Sequence.new(
        [
          sprite.new_action({x: 300}, duration: 6.seconds, easing: :linear),
          sprite.new_action({x: 100}, duration: 6.seconds, easing: :linear)
        ],
        repeat: :forever
      )
    )

    sprite.run_action(
      Zif::Actions::Sequence.new(
        [
          sprite.new_action({y: 300}, duration: 3.seconds, easing: :linear),
          sprite.new_action({y: 100}, duration: 3.seconds, easing: :linear)
        ],
        repeat: :forever
      )
    )
  end
end

Instance Attribute Details

#compound_spriteObject

Returns the value of attribute compound_sprite.



7
8
9
# File 'app/scenes/compound_sprite_test.rb', line 7

def compound_sprite
  @compound_sprite
end

Instance Method Details

#perform_tickObject



164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
# File 'app/scenes/compound_sprite_test.rb', line 164

def perform_tick
  mark('#perform_tick: Start')
  $gtk.args.outputs.background_color = [0, 0, 0, 0]

  @compound_sprite.source_h += 100 if $gtk.args.inputs.keyboard.key_up.up
  @compound_sprite.source_h -= 100 if $gtk.args.inputs.keyboard.key_up.down
  @compound_sprite.source_w += 100 if $gtk.args.inputs.keyboard.key_up.right
  @compound_sprite.source_w -= 100 if $gtk.args.inputs.keyboard.key_up.left

  # rubocop:disable Layout/LineLength
  $gtk.args.outputs.labels << { x: 4, y: 120, text: 'Up/Down: Modify source_h.  Right/Left: Modify source_w.', r: 255, g: 255, b: 255, a: 255}
  $gtk.args.outputs.labels << { x: 4, y: 100, text: "Compund Sprite: #{@compound_sprite.rect} -> #{@compound_sprite.source_rect}.  Zoom #{@compound_sprite.zoom_factor}", r: 255, g: 255, b: 255, a: 255}
  # rubocop:enable Layout/LineLength

  mark('#perform_tick: Finished')
  super
end

#pixie_spray(pix, layer) ⇒ Object



182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
# File 'app/scenes/compound_sprite_test.rb', line 182

def pixie_spray(pix, layer)
  idx = rand(3)
  dir = @dragons[idx].flip_horizontally ? 1 : -1

  pix.x = @dragons[idx].center_x + (5 * layer) * dir
  pix.y = @dragons[idx].center_y - 10
  pix.a = 50
  pix.run_action(pix.new_action(
                   {
                     x:     pix.x + ((rand(100) + 20) * dir),
                     angle: rand(360 * 5)
                   },
                   duration: 2.seconds,
                   easing:   :smooth_stop5
                 ))
  pix.run_action(pix.new_action({y: pix.y + Zif.relative_rand(50)}, duration: 2.seconds, easing: :smooth_stop5))
  pix.run_action(
    pix.fade_out(rand.seconds) do
      pixie_spray(pix, layer)
    end
  )
end

#prepare_sceneObject

rubocop:enable Metrics/AbcSize



147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
# File 'app/scenes/compound_sprite_test.rb', line 147

def prepare_scene
  super

  @compound_sprite.sprites.each do |dragon| # and pixies
    $game.services[:action_service].register_actionable(dragon)
  end
  @compound_sprite.labels.each do |dragon_label|
    $game.services[:action_service].register_actionable(dragon_label)
  end

  $game.services[:action_service].register_actionable(@compound_sprite)
  $game.services[:action_service].register_actionable(@outline)

  $gtk.args.outputs.static_sprites << @outline
  $gtk.args.outputs.static_sprites << @compound_sprite
end