zyps 0.7.5 → 0.7.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/lib/zyps/actions.rb CHANGED
@@ -175,18 +175,19 @@ class TagAction < Action
175
175
  end
176
176
 
177
177
 
178
- #Blend the target's color with another color.
179
- class BlendAction < Action
180
- #Color to apply to targets.
178
+ #Blend the actor's color with another color.
179
+ class BlendAction < TimedAction
180
+ #Color to apply to actor.
181
181
  attr_accessor :color
182
- def initialize(color)
183
- self.color = color
182
+ def initialize(rate, color)
183
+ super
184
+ @color = color
184
185
  end
185
- #Blend the targets' color with the assigned color.
186
+ #Blend the actor's color with the assigned color.
186
187
  def do(actor, targets)
187
- targets.each do |target|
188
- target.color += @color
189
- end
188
+ actor.color.red += (@color.red - actor.color.red) * delta
189
+ actor.color.green += (@color.green - actor.color.green) * delta
190
+ actor.color.blue += (@color.blue - actor.color.blue) * delta
190
191
  end
191
192
  end
192
193
 
@@ -269,4 +270,92 @@ class BreedAction < Action
269
270
  end
270
271
 
271
272
 
273
+ #Copies the given GameObject prototypes into the environment.
274
+ class SpawnAction < Action
275
+ #Environment to place children into.
276
+ attr_accessor :environment
277
+ #Array of GameObjects to copy into environment.
278
+ attr_accessor :prototypes
279
+ def initialize(environment, prototypes = [])
280
+ self.environment = environment
281
+ self.prototypes = prototypes
282
+ end
283
+ #Add children to environment.
284
+ def do(actor, targets)
285
+ prototypes.each do |prototype|
286
+ environment.objects << generate_child(actor, prototype)
287
+ end
288
+ end
289
+ #Copy prototype to actor's location.
290
+ def generate_child(actor, prototype)
291
+ #Copy prototype so it can be spawned repeatedly if need be.
292
+ child = prototype.copy
293
+ child.location = actor.location.copy
294
+ child
295
+ end
296
+ end
297
+
298
+
299
+ #Copies the given GameObject prototypes into the environment and destroys actor.
300
+ #Shrapnel's original vector will be added to actor's vector.
301
+ #Shrapnel's size will be actor's size divided by number of shrapnel pieces.
302
+ class ExplodeAction < SpawnAction
303
+ #Calls super method.
304
+ #Also removes actor from environment.
305
+ def do(actor, targets)
306
+ super
307
+ environment.objects.delete(actor)
308
+ end
309
+ #Calls super method.
310
+ #Also adds actor's vector to child's.
311
+ #Finally, reduces child's size to actor's size divided by number of shrapnel pieces.
312
+ def generate_child(actor, prototype)
313
+ child = super
314
+ child.vector += actor.vector
315
+ child.size = actor.size / prototypes.length
316
+ child
317
+ end
318
+ end
319
+
320
+
321
+
322
+ #Copies the given GameObject prototypes into the environment.
323
+ #Bullet's vector angle will be added to angle to target.
324
+ class ShootAction < SpawnAction
325
+ #Collection of GameObjects to copy into environment.
326
+ #First element will be copied on first call, subsequent elements on subsequent calls, wrapping back to start once end is reached.
327
+ #If an element is a collection, all its members will be copied in at once.
328
+ attr_accessor :prototypes
329
+ def initialize(*arguments)
330
+ super
331
+ @prototype_index = 0
332
+ @target_index = 0
333
+ end
334
+ #Copies next prototype into environment.
335
+ def do(actor, targets)
336
+ return if targets.empty?
337
+ #If next item is a collection of prototypes, copy them all in at once.
338
+ if prototypes[@prototype_index].respond_to?(:each)
339
+ prototypes[@prototype_index].each do |prototype|
340
+ environment.objects << generate_child(actor, prototype, targets[@target_index])
341
+ end
342
+ #Otherwise copy the single prototype.
343
+ else
344
+ environment.objects << generate_child(actor, prototypes[@prototype_index], targets[@target_index])
345
+ end
346
+ #Move to next target and prototype group, wrapping to start of array if need be.
347
+ @target_index = (@target_index + 1) % targets.length
348
+ @prototype_index = (@prototype_index + 1) % prototypes.length
349
+ end
350
+ #Calls super method.
351
+ #Also adds angle to target to child's vector angle.
352
+ def generate_child(actor, prototype, target)
353
+ child = super(actor, prototype)
354
+ child.vector.pitch = Utility.find_angle(actor.location, target.location) + child.vector.pitch
355
+ child
356
+ end
357
+ end
358
+
359
+
360
+
272
361
  end #module Zyps
@@ -105,4 +105,26 @@ class ClassCondition < Condition
105
105
  end
106
106
 
107
107
 
108
+ #True if the given interval has elapsed.
109
+ class ElapsedTimeCondition < Condition
110
+ #The number of seconds that must elapse before the condition is true.
111
+ attr_accessor :interval
112
+ def initialize(interval = 1.0)
113
+ self.interval = interval
114
+ @clock = Clock.new
115
+ @elapsed_time = 0
116
+ end
117
+ #Returns the array of targets if the interval has elapsed.
118
+ def select(actor, targets)
119
+ @elapsed_time += @clock.elapsed_time
120
+ if @elapsed_time >= interval
121
+ @elapsed_time = 0
122
+ return targets
123
+ else
124
+ return []
125
+ end
126
+ end
127
+ end
128
+
129
+
108
130
  end #module Zyps
data/test/test_zyps.rb CHANGED
@@ -595,5 +595,56 @@ class TestBehavior < Test::Unit::TestCase
595
595
  assert_equal(1, action.stop_count, "stop() should NOT have been called.")
596
596
  end
597
597
 
598
+ end
599
+
600
+
601
+ class TestAdditions < Test::Unit::TestCase
602
+
603
+ #Add a new behavior to a creature with the given action.
604
+ def add_action(action, creature)
605
+ behavior = Behavior.new
606
+ behavior.actions << action
607
+ creature.behaviors << behavior
608
+ end
609
+
610
+ def setup
611
+ @environment = Environment.new
612
+ @actor = Creature.new(:name => 'target1', :location => Location.new(1, 1))
613
+ @env_fact = gravity = Gravity.new(200)
614
+ end
615
+
616
+ def test_environment_double_arrow_objects
617
+ assert_equal(0, @environment.objects.size)
618
+ @environment << @actor
619
+ assert_equal(1, @environment.objects.size)
620
+ assert_equal(0, @environment.environmental_factors.size)
621
+ end
622
+
623
+ def test_environment_double_arrow_factors
624
+ assert_equal(0, @environment.environmental_factors.size)
625
+ @environment << @env_fact
626
+ assert_equal(1, @environment.environmental_factors.size)
627
+ assert_equal(0, @environment.objects.size)
628
+ end
629
+
630
+ def test_game_object_double_arrow
631
+ #test color
632
+ @actor << Color.new(1,1,1)
633
+ assert_equal(Color.new(1,1,1), @actor.color)
634
+ #test vector
635
+ vect =Vector.new(23,13)
636
+ @actor << vect
637
+ assert_equal(vect, @actor.vector)
638
+ #test location
639
+ @actor << Location.new(13,13)
640
+ assert_equal(13, @actor.location.x)
641
+ assert_equal(13, @actor.location.y)
642
+ #test behavior
643
+ behavior = Behavior.new
644
+ behavior.actions << TagAction.new("1")
645
+ @actor << behavior
646
+ assert_equal(1, @actor.behaviors.size)
647
+ assert_equal("1", @actor.behaviors.first.actions.first.tag)
648
+ end
598
649
 
599
650
  end
@@ -154,32 +154,32 @@ class TestActions < Test::Unit::TestCase
154
154
  end
155
155
 
156
156
 
157
- #A BlendAction shifts the target's color toward the given color.
157
+ #A BlendAction shifts the actor's color toward the given color.
158
158
  def test_blend_action_black
159
159
  #Create a BlendAction that blends to black.
160
- add_action(BlendAction.new(Color.new(0, 0, 0)), @actor)
161
- #Set the target's color.
162
- @target1.color = Color.new(0.5, 0.5, 0.5)
163
- #Act.
160
+ add_action(BlendAction.new(1, Color.new(0, 0, 0)), @actor)
161
+ #Set the actor's color.
162
+ @actor.color = Color.new(0.5, 0.5, 0.5)
163
+ #Act (time difference is 0.1 seconds).
164
164
  @environment.interact
165
- #Verify the target's new color.
166
- assert_in_delta(0.25, @target1.color.red, REQUIRED_ACCURACY)
167
- assert_in_delta(0.25, @target1.color.green, REQUIRED_ACCURACY)
168
- assert_in_delta(0.25, @target1.color.blue, REQUIRED_ACCURACY)
165
+ #Verify the actor's new color.
166
+ assert_in_delta(0.45, @actor.color.red, REQUIRED_ACCURACY)
167
+ assert_in_delta(0.45, @actor.color.green, REQUIRED_ACCURACY)
168
+ assert_in_delta(0.45, @actor.color.blue, REQUIRED_ACCURACY)
169
169
  end
170
170
 
171
171
  #Test shifting colors toward white.
172
172
  def test_blend_action_white
173
173
  #Create a BlendAction that blends to white.
174
- add_action(BlendAction.new(Color.new(1, 1, 1)), @actor)
175
- #Set the target's color.
176
- @target1.color = Color.new(0.5, 0.5, 0.5)
177
- #Act.
174
+ add_action(BlendAction.new(1, Color.new(1, 1, 1)), @actor)
175
+ #Set the actor's color.
176
+ @actor.color = Color.new(0.5, 0.5, 0.5)
177
+ #Act (time difference is 0.1 seconds).
178
178
  @environment.interact
179
- #Verify the target's new color.
180
- assert_in_delta(0.75, @target1.color.red, REQUIRED_ACCURACY)
181
- assert_in_delta(0.75, @target1.color.green, REQUIRED_ACCURACY)
182
- assert_in_delta(0.75, @target1.color.blue, REQUIRED_ACCURACY)
179
+ #Verify the actor's new color.
180
+ assert_in_delta(0.55, @actor.color.red, REQUIRED_ACCURACY)
181
+ assert_in_delta(0.55, @actor.color.green, REQUIRED_ACCURACY)
182
+ assert_in_delta(0.55, @actor.color.blue, REQUIRED_ACCURACY)
183
183
  end
184
184
 
185
185
 
@@ -231,4 +231,65 @@ class TestActions < Test::Unit::TestCase
231
231
  assert_equal(@actor.location.y, child.location.y)
232
232
  end
233
233
 
234
+ def test_spawn_action
235
+ #Set up prototypes.
236
+ prototypes = [Creature.new, GameObject.new]
237
+ prototypes[0].vector = Vector.new(1, 45)
238
+ #Add prototypes to new SpawnAction.
239
+ add_action(SpawnAction.new(@environment, prototypes), @actor)
240
+ #Interact.
241
+ @environment.interact
242
+ #All children should be spawned.
243
+ assert_equal(5, @environment.objects.length)
244
+ #Childrens' starting location should match actor's.
245
+ assert_equal(@environment.objects[-1].location, @actor.location)
246
+ #Spawned objects should be copy of originals.
247
+ assert_not_same(prototypes[0], @environment.objects[-2])
248
+ #Spawned objects' vectors should be same as originals'.
249
+ assert_equal(prototypes[0].vector, @environment.objects[-2].vector)
250
+ end
251
+
252
+ def test_explode_action
253
+ #Set up prototypes.
254
+ prototypes = [Creature.new, GameObject.new]
255
+ prototypes[0].vector = Vector.new(1, 45)
256
+ #Add prototypes to new ExplodeAction.
257
+ add_action(ExplodeAction.new(@environment, prototypes), @actor)
258
+ #Interact.
259
+ @environment.interact
260
+ #Actor should be removed from environment.
261
+ assert(! @environment.objects.include?(@actor))
262
+ #All children should be spawned.
263
+ assert_equal(4, @environment.objects.length)
264
+ children = @environment.objects[-2, 2]
265
+ #Spawned objects' vectors should be sum of originals' plus actor's.
266
+ assert_equal(prototypes[0].vector + @actor.vector, children[0].vector)
267
+ assert_equal(prototypes[1].vector + @actor.vector, children[1].vector)
268
+ end
269
+
270
+ def test_shoot_action
271
+ #Set up prototypes.
272
+ prototypes = [[Creature.new, GameObject.new], Creature.new(:name => '2')]
273
+ prototypes[0][0].vector.pitch = 5
274
+ #Add prototypes to new ShootAction.
275
+ add_action(ShootAction.new(@environment, prototypes), @actor)
276
+ #Interact with both targets.
277
+ @environment.interact
278
+ #Both objects in first group should have been spawned.
279
+ assert_equal(5, @environment.objects.length)
280
+ #First spawned object's vector should match angle to first target plus prototype's vector angle.
281
+ children = @environment.objects[-2, 2]
282
+ assert_equal(45 + 5, children[0].vector.pitch)
283
+ #Second spawned object's vector should match angle to first target.
284
+ assert_equal(45, children[1].vector.pitch)
285
+ #Fire second set of bullets.
286
+ @environment.interact
287
+ #Only second set should have been spawned.
288
+ assert_equal(6, @environment.objects.length)
289
+ children = @environment.objects[-1, 1]
290
+ assert_equal('Copy of 2', children[0].name)
291
+ #Spawned object should be aimed at second target.
292
+ assert_equal(225, children[0].vector.pitch)
293
+ end
294
+
234
295
  end
@@ -96,4 +96,16 @@ class TestConditions < Test::Unit::TestCase
96
96
  end
97
97
 
98
98
 
99
+ def test_elapsed_time_condition
100
+ condition = ElapsedTimeCondition.new
101
+ condition.interval = 0.2
102
+ #Test for falsehood.
103
+ #On first pass, its clock will only be at 0.1 seconds.
104
+ assert(! condition.select(@actor, [@target]).include?(@target))
105
+ #Test for truth.
106
+ #On first pass, its clock will be at 0.2 seconds, the assigned interval.
107
+ assert(condition.select(@actor, [@target]).include?(@target))
108
+ end
109
+
110
+
99
111
  end
metadata CHANGED
@@ -1,93 +1,87 @@
1
1
  --- !ruby/object:Gem::Specification
2
+ rubygems_version: 0.9.2
3
+ specification_version: 1
2
4
  name: zyps
3
5
  version: !ruby/object:Gem::Version
4
- version: 0.7.5
6
+ version: 0.7.6
7
+ date: 2008-02-29 00:00:00 -07:00
8
+ summary: A game library for Ruby
9
+ require_paths:
10
+ - lib
11
+ email: jay@mcgavren.com
12
+ homepage: http://jay.mcgavren.com/zyps/
13
+ rubyforge_project: zyps
14
+ description:
15
+ autorequire: zyps
16
+ default_executable:
17
+ bindir: bin
18
+ has_rdoc: true
19
+ required_ruby_version: !ruby/object:Gem::Version::Requirement
20
+ requirements:
21
+ - - ">"
22
+ - !ruby/object:Gem::Version
23
+ version: 0.0.0
24
+ version:
5
25
  platform: ruby
26
+ signing_key:
27
+ cert_chain:
28
+ post_install_message:
6
29
  authors:
7
30
  - Jay McGavren
8
- autorequire: zyps
9
- bindir: bin
10
- cert_chain: []
11
-
12
- date: 2008-02-16 00:00:00 -07:00
13
- default_executable:
14
- dependencies:
15
- - !ruby/object:Gem::Dependency
16
- name: wxruby
17
- version_requirement:
18
- version_requirements: !ruby/object:Gem::Requirement
19
- requirements:
20
- - - ">="
21
- - !ruby/object:Gem::Version
22
- version: 1.9.2
23
- version:
24
- description:
25
- email: jay@mcgavren.com
26
- executables:
27
- - zyps
28
- extensions: []
29
-
30
- extra_rdoc_files:
31
- - README.txt
32
- - COPYING.LESSER.txt
33
- - COPYING.txt
34
31
  files:
35
32
  - COPYING.LESSER.txt
36
33
  - COPYING.txt
37
- - README_windows.txt
38
34
  - README.txt
39
- - bin/zyps_demo
35
+ - README_windows.txt
40
36
  - bin/zyps
37
+ - bin/zyps-175
38
+ - bin/zyps_demo
41
39
  - lib/zyps
40
+ - lib/zyps/actions.rb
41
+ - lib/zyps/conditions.rb
42
+ - lib/zyps/environmental_factors.rb
43
+ - lib/zyps/remote.rb
42
44
  - lib/zyps/views
43
45
  - lib/zyps/views/canvas
44
46
  - lib/zyps/views/canvas/wx.rb
45
47
  - lib/zyps/views/trails.rb
46
- - lib/zyps/conditions.rb
47
- - lib/zyps/remote.rb
48
- - lib/zyps/actions.rb
49
- - lib/zyps/environmental_factors.rb
50
48
  - lib/zyps.rb
49
+ - test/test_zyps.rb
51
50
  - test/zyps
52
- - test/zyps/test_behaviors.rb
53
51
  - test/zyps/test_actions.rb
52
+ - test/zyps/test_behaviors.rb
54
53
  - test/zyps/test_conditions.rb
55
- - test/zyps/test_remote.rb
56
54
  - test/zyps/test_environmental_factors.rb
55
+ - test/zyps/test_remote.rb
56
+ test_files:
57
57
  - test/test_zyps.rb
58
- has_rdoc: true
59
- homepage: http://jay.mcgavren.com/zyps/
60
- post_install_message:
58
+ - test/zyps/test_actions.rb
59
+ - test/zyps/test_behaviors.rb
60
+ - test/zyps/test_conditions.rb
61
+ - test/zyps/test_environmental_factors.rb
62
+ - test/zyps/test_remote.rb
61
63
  rdoc_options:
62
64
  - --title
63
65
  - Zyps - A game library for Ruby
64
66
  - --main
65
67
  - README.txt
66
- require_paths:
67
- - lib
68
- required_ruby_version: !ruby/object:Gem::Requirement
69
- requirements:
70
- - - ">="
71
- - !ruby/object:Gem::Version
72
- version: "0"
73
- version:
74
- required_rubygems_version: !ruby/object:Gem::Requirement
75
- requirements:
76
- - - ">="
77
- - !ruby/object:Gem::Version
78
- version: "0"
79
- version:
68
+ extra_rdoc_files:
69
+ - README.txt
70
+ - COPYING.LESSER.txt
71
+ - COPYING.txt
72
+ executables:
73
+ - zyps
74
+ extensions: []
75
+
80
76
  requirements: []
81
77
 
82
- rubyforge_project: zyps
83
- rubygems_version: 1.0.1
84
- signing_key:
85
- specification_version: 2
86
- summary: A game library for Ruby
87
- test_files:
88
- - test/zyps/test_behaviors.rb
89
- - test/zyps/test_actions.rb
90
- - test/zyps/test_conditions.rb
91
- - test/zyps/test_remote.rb
92
- - test/zyps/test_environmental_factors.rb
93
- - test/test_zyps.rb
78
+ dependencies:
79
+ - !ruby/object:Gem::Dependency
80
+ name: wxruby
81
+ version_requirement:
82
+ version_requirements: !ruby/object:Gem::Version::Requirement
83
+ requirements:
84
+ - - ">="
85
+ - !ruby/object:Gem::Version
86
+ version: 1.9.2
87
+ version: