Skip to content

Commit

Permalink
goodbuy automatic_assets-trait, hello animation-trait (automatic load…
Browse files Browse the repository at this point in the history
…ing of an image is such an easy task anyway, def setup; @image = Image[#{self.filename}.png]; end). Updated README and examples.
  • Loading branch information
ippa committed May 31, 2010
1 parent c262f84 commit 0825f85
Show file tree
Hide file tree
Showing 4 changed files with 117 additions and 133 deletions.
22 changes: 10 additions & 12 deletions README.rdoc
Expand Up @@ -451,7 +451,7 @@ Chingus inputhandler will detect that Menu is a GameState-class, create a new in

== The setup-method
If a setup() is available in a instance of Chingu::GameObject, Chingu::Window and Chingu::GameState it will automatically be called.
This is the perfect spot to include various setup/init-tasks like setting colors or loading animations (if you're not using the automatic_assets-trait).
This is the perfect spot to include various setup/init-tasks like setting colors or loading animations (if you're not using the animation-trait).
You could also override initialize() for this purpose but it's been proven prone to errors again and again.
Compare the 2 snippets below:

Expand Down Expand Up @@ -596,21 +596,19 @@ You can also scale the calculated radius with has_trait-options:
# :debug => true shows the actual circle in red on the screen
has_trait :bounding_circle, :debug => true

==== Trait "automatic_assets"
Automatically load images and animations depending on the class-name.
Useful when having a lot of simple classes thats mainpurpose is displaying an image or animation.
==== Trait "animation"
Automatically load animations depending on the class-name.
Useful when having a lot of simple classes thats mainpurpose is displaying an animation.
Assuming the below code is included in a class FireBall.

# fire_ball.png / fire_ball.png will be loaded into @image
has_trait :automatic_assets

# If a fire_ball_10x10.png/bmp exists, it will be loaded as a tileanimation (and fire_ball.png image will be ignored)
#
# If a fire_ball_10x10.png/bmp exists, it will be loaded as a tileanimation.
# 10x10 would indicate the width and height of each tile so Chingu knows hows to cut it up into single frames.
# The animation will then be available in animations[:default] as a Animation-instance.
# The animation will then be available in animations[:default] as an Animation-instance.
#
# Several animations can be loaded at the same time, for example if these tile files exist:
# fire_ball_10x10_fly.png # Will be available in animations[:fly] as a Animation-instance
# fire_ball_10x10_explode.png # Will be available in animations[:explode] as a Animation-instance
# If more then 1 animation exist, they'll will be loaded at the same time, for example:
# fire_ball_10x10_fly.png # Will be available in animations[:fly] as an Animation-instance
# fire_ball_10x10_explode.png # Will be available in animations[:explode] as an Animation-instance
#
# The below example will set the 200ms delay between each frame on all animations loaded.
#
Expand Down
Expand Up @@ -5,7 +5,7 @@
include Chingu

#
# automatic_asset-trait example
# animation-trait example
#
class Game < Chingu::Window
def initialize
Expand All @@ -28,43 +28,56 @@ def setup

def update
super
game_objects.select { |game_object| game_object.outside_window? }.each(&:destroy)
$window.caption = "game_objects: #{game_objects.size}"
end
end

class Actor < GameObject
has_trait :automatic_assets, :delay => 100
has_trait :velocity

def update
destroy if outside_window?
@image = @animation.next if @animation
def setup
@image = Image["#{self.filename}.png"]
end

end

class Spaceship < Actor; end # spaceship.png will be loaded
class Plane < Actor; end # plane.png will be loaded
class FireBullet < Actor; end # fire_bullet.png will be loaded
class Droid < Actor; end # droid_11x16.png will be loaded and animated with :delay parameter, each frame beeing 11 x 16 pixels

#
# droid_11x16.png will be loaded and animated with :delay parameter, each frame beeing 11 x 16 pixels
#
class Droid < Actor
has_trait :animation, :delay => 200

def update
@image = self.animation.next
end
end

#
# star_25x25_default.png and star_25x25_explode.png will be loaded.
# Access the 2 animation-"states" with animation[:default] and animation[:explode]
# You could also change the default playing animation with switch_animation(state)
# Access the 2 animation-"states" with self.animations[:default] and self.animations[:explode]
# self.animation will point to self.animations[:default]
#
class Star < Actor
has_trait :animation, :delay => 100

def setup
@animation = self.animations[:default]
self.animations[:explode].loop = false
end

def update

if @x < $window.width/2 || @y < $window.height/2
@animation = self.animations[:explode]
@animation.loop = false
@image = self.animations[:explode].next
else
@image = self.animations[:default].next
end
super
end

end

Game.new.show
82 changes: 82 additions & 0 deletions lib/chingu/traits/animation.rb
@@ -0,0 +1,82 @@
#--
#
# Chingu -- OpenGL accelerated 2D game framework for Ruby
# Copyright (C) 2009 ippa / ippa@rubylicio.us
#
# This library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License as published by the Free Software Foundation; either
# version 2.1 of the License, or (at your option) any later version.
#
# This library is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public
# License along with this library; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
#
#++

module Chingu
module Traits
#
# A chingu trait providing automatic loading of tile-animations
#
# For example:
# class FireBall; has_traits :animation; end;
#
# Will automatically load:
# - fire_ball.png into self.animations[:default]
# - fire_ball_exploding.png into self.animations[:exploding]
#
# Adds accessors animations -> Hash with all animations, hash-key is the name of the animation
#
module Animation

module ClassMethods

def initialize_trait(options = {})
trait_options[:animation] = {:directory => "media", :play => true, :delay => 100}.merge(options)
end

end

def setup_trait(options)
@animation_options = {:debug => false}.merge(options)
@animations = load_animations
super
end

#
# Try loading animation from class-name
#
def load_animations
animations = {}
glob = "#{trait_options[:animation][:directory]}/#{self.filename}_*"
puts "Animations? #{glob}" if trait_options[:animation][:debug]
Dir[glob].each do |tile_file|
if tile_file =~ /[a-zA-Z\_+]_(\d+)x(\d+)_*([a-zA-Z]*)\.(bmp|png)/
state = $3.length > 0 ? $3 : "default"
animations[state.to_sym] = Chingu::Animation.new(:file => tile_file, :delay => trait_options[:animation][:delay])
# puts "ANIM: #{tile_file}, width: #{width}, height: #{height}, state: #{state}" if trait_options[:animation][:debug]
end
end
return animations
end

def animation
@animations[:default]
end

#
# Returns all animations, then access invidual states with animations[:explode] etc.
#
def animations
@animations
end

end
end
end
109 changes: 0 additions & 109 deletions lib/chingu/traits/automatic_assets.rb

This file was deleted.

0 comments on commit 0825f85

Please sign in to comment.