Class: Zyps::ShootAction

Inherits:
SpawnAction show all
Defined in:
lib/zyps/actions.rb

Overview

Copies the given GameObject prototypes into the environment. Bullet’s vector angle will be added to angle to target.

Instance Attribute Summary collapse

Attributes inherited from SpawnAction

#environment

Instance Method Summary collapse

Constructor Details

#initialize(*arguments) ⇒ ShootAction

Returns a new instance of ShootAction.



329
330
331
332
333
# File 'lib/zyps/actions.rb', line 329

def initialize(*arguments)
	super
	@prototype_index = 0
	@target_index = 0
end

Instance Attribute Details

#prototypesObject

Collection of GameObjects to copy into environment. First element will be copied on first call, subsequent elements on subsequent calls, wrapping back to start once end is reached. If an element is a collection, all its members will be copied in at once.



328
329
330
# File 'lib/zyps/actions.rb', line 328

def prototypes
  @prototypes
end

Instance Method Details

#do(actor, targets) ⇒ Object

Copies next prototype into environment.



335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
# File 'lib/zyps/actions.rb', line 335

def do(actor, targets)
	return if targets.empty?
	#If next item is a collection of prototypes, copy them all in at once.
	if prototypes[@prototype_index].respond_to?(:each)
		prototypes[@prototype_index].each do |prototype|
			environment.objects << generate_child(actor, prototype, targets[@target_index])
		end
	#Otherwise copy the single prototype.
	else
		environment.objects << generate_child(actor, prototypes[@prototype_index], targets[@target_index])
	end
	#Move to next target and prototype group, wrapping to start of array if need be.
	@target_index = (@target_index + 1) % targets.length
	@prototype_index = (@prototype_index + 1) % prototypes.length
end

#generate_child(actor, prototype, target) ⇒ Object

Calls super method. Also adds angle to target to child’s vector angle.



352
353
354
355
356
# File 'lib/zyps/actions.rb', line 352

def generate_child(actor, prototype, target)
	child = super(actor, prototype)
	child.vector.pitch = Utility.find_angle(actor.location, target.location) + child.vector.pitch
	child
end