public
Last active

macruby core animation Fireworks, have a nice macruby new year

  • Download Gist
gistfile1.rb
Ruby
1 2 3 4 5 6 7 8 9 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 145 146 147 148 149 150 151 152 153 154 155 156
#!/usr/local/bin/macruby
framework 'Cocoa'
framework 'QuartzCore'
framework 'CoreGraphics' # Mountain Lion Update
 
 
class FireworkDelegate
attr_accessor :window
def initWithURL(url)
case url
when String
@firework_photo_url = NSURL.URLWithString(url)
when NSURL
@firework_photo_url = url
else
raise "The AppDelegate class was initiated with an unknown type object"
end
self
end
def applicationDidFinishLaunching(aNotification)
window.delegate = self
view = window.contentView
view.wantsLayer = true
@root_layer = CALayer.layer #create the root layer
#configure the layer attributes
@root_layer.bounds = view.frame
color = CGColorCreateGenericRGB(0.0, 0.0, 0.0, 1)
@root_layer.backgroundColor = color
CGColorRelease(color)
# Load the spark image for the particle
data_provider = CGDataProviderCreateWithURL(@firework_photo_url)
image = CGImageCreateWithPNGDataProvider(data_provider, nil, false, KCGRenderingIntentDefault)
@mortor = CAEmitterLayer.layer
@mortor.emitterPosition = CGPointMake(320, 0)
@mortor.renderMode = KCAEmitterLayerAdditive
# Invisible particle representing the rocket before the explosion
rocket = CAEmitterCell.emitterCell
rocket.emissionLongitude = Math::PI / 2
rocket.emissionLatitude = 0
rocket.lifetime = 1.6
rocket.birthRate = 1
rocket.velocity = 400
rocket.velocityRange = 100
rocket.yAcceleration = -250
rocket.emissionRange = Math::PI / 4
color = CGColorCreateGenericRGB(0.5, 0.5, 0.5, 0.5)
rocket.color = color
CGColorRelease(color);
rocket.redRange = 0.5
rocket.greenRange = 0.5
rocket.blueRange = 0.5
# Name the cell so that it can be animated later using keypath
rocket.name = "rocket"
# Flare particles emitted from the rocket as it flys
flare = CAEmitterCell.emitterCell
flare.contents = image
flare.emissionLongitude = (4 * Math::PI) / 2
flare.scale = 0.4
flare.velocity = 100
flare.birthRate = 45
flare.lifetime = 1.5
flare.yAcceleration = -350
flare.emissionRange = Math::PI / 7
flare.alphaSpeed = -0.7
flare.scaleSpeed = -0.1
flare.scaleRange = 0.1
flare.beginTime = 0.01
flare.duration = 0.7
# The particles that make up the explosion
firework = CAEmitterCell.emitterCell
firework.contents = image
firework.birthRate = 9999
firework.scale = 0.6
firework.velocity = 130
firework.lifetime = 2
firework.alphaSpeed = -0.2
firework.yAcceleration = -80
firework.beginTime = 1.5
firework.duration = 0.1
firework.emissionRange = 2 * Math::PI
firework.scaleSpeed = -0.1
firework.spin = 2
# Name the cell so that it can be animated later using keypath
firework.name = "firework"
# preSpark is an invisible particle used to later emit the spark
preSpark = CAEmitterCell.emitterCell
preSpark.birthRate = 80
preSpark.velocity = firework.velocity * 0.70
preSpark.lifetime = 1.7
preSpark.yAcceleration = firework.yAcceleration * 0.85
preSpark.beginTime = firework.beginTime - 0.2
preSpark.emissionRange = firework.emissionRange
preSpark.greenSpeed = 100
preSpark.blueSpeed = 100
preSpark.redSpeed = 100
# Name the cell so that it can be animated later using keypath
preSpark.name = "preSpark"
# The 'sparkle' at the end of a firework
spark = CAEmitterCell.emitterCell
spark.contents = image
spark.lifetime = 0.05
spark.yAcceleration = -250
spark.beginTime = 0.8
spark.scale = 0.4
spark.birthRate = 10
preSpark.emitterCells = [spark]
rocket.emitterCells = [flare, firework, preSpark]
@mortor.emitterCells = [rocket]
@root_layer.addSublayer @mortor
view.layer = @root_layer
view.needsDisplay = true # Force the view to update
window.center
window.orderFrontRegardless
end
def applicationShouldTerminateAfterLastWindowClosed the_application
true
end
def applicationShouldTerminate sender
NSLog("Finished")
NSTerminateNow
end
end
 
application = NSApplication.sharedApplication
NSApplication.sharedApplication.activationPolicy = NSApplicationActivationPolicyRegular
application.delegate = FireworkDelegate.alloc.initWithURL(ARGV.shift || "http://dl.dropbox.com/u/930741/tspark.png")
# create the Application Window
frame = [0.0, 0.0, 660, 500]
window = NSWindow.alloc.initWithContentRect frame,
styleMask: NSTitledWindowMask | NSClosableWindowMask,
backing: NSBackingStoreBuffered,
defer: false
 
application.delegate.window = window
window.orderOut(nil)
window.display
puts "Starting the app..."
application.run

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.