chingu 0.6.9 → 0.7.0

Sign up to get free protection for your applications and to get access to all the features.
data/Rakefile CHANGED
@@ -14,7 +14,7 @@ begin
14
14
  gemspec.rubyforge_project = "chingu"
15
15
  gemspec.version = Chingu::VERSION
16
16
 
17
- gemspec.add_dependency 'gosu', '>= 0.7.17'
17
+ gemspec.add_dependency 'gosu', '>= 0.7.18'
18
18
  end
19
19
  Jeweler::GemcutterTasks.new
20
20
  rescue LoadError
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{chingu}
8
- s.version = "0.6.9"
8
+ s.version = "0.7.0"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["ippa"]
12
- s.date = %q{2010-01-05}
12
+ s.date = %q{2010-01-14}
13
13
  s.description = %q{OpenGL accelerated 2D game framework for Ruby. Builds on Gosu (Ruby/C++) which provides all the core functionality. Chingu adds simple yet powerful game states, prettier input handling, deployment safe asset-handling, a basic re-usable game object and stackable game logic.}
14
14
  s.email = %q{ippa@rubylicio.us}
15
15
  s.extra_rdoc_files = [
@@ -39,6 +39,7 @@ Gem::Specification.new do |s|
39
39
  "examples/example15_trait_timer2.rb",
40
40
  "examples/example16_online_high_scores.rb",
41
41
  "examples/example17_gosu_tutorial.rb",
42
+ "examples/example18_viewport.rb",
42
43
  "examples/example1_basics.rb",
43
44
  "examples/example2_gamestate_basics.rb",
44
45
  "examples/example3_parallax.rb",
@@ -121,6 +122,8 @@ Gem::Specification.new do |s|
121
122
  "lib/chingu/traits/retrofy.rb",
122
123
  "lib/chingu/traits/timer.rb",
123
124
  "lib/chingu/traits/velocity.rb",
125
+ "lib/chingu/traits/viewport.rb",
126
+ "lib/chingu/viewport.rb",
124
127
  "lib/chingu/window.rb"
125
128
  ]
126
129
  s.homepage = %q{http://github.com/ippa/chingu}
@@ -138,6 +141,7 @@ Gem::Specification.new do |s|
138
141
  "examples/example15_trait_timer2.rb",
139
142
  "examples/example16_online_high_scores.rb",
140
143
  "examples/example17_gosu_tutorial.rb",
144
+ "examples/example18_viewport.rb",
141
145
  "examples/example1_basics.rb",
142
146
  "examples/example2_gamestate_basics.rb",
143
147
  "examples/example3_parallax.rb",
@@ -155,12 +159,12 @@ Gem::Specification.new do |s|
155
159
  s.specification_version = 3
156
160
 
157
161
  if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
158
- s.add_runtime_dependency(%q<gosu>, [">= 0.7.17"])
162
+ s.add_runtime_dependency(%q<gosu>, [">= 0.7.18"])
159
163
  else
160
- s.add_dependency(%q<gosu>, [">= 0.7.17"])
164
+ s.add_dependency(%q<gosu>, [">= 0.7.18"])
161
165
  end
162
166
  else
163
- s.add_dependency(%q<gosu>, [">= 0.7.17"])
167
+ s.add_dependency(%q<gosu>, [">= 0.7.18"])
164
168
  end
165
169
  end
166
170
 
@@ -0,0 +1,173 @@
1
+ #!/usr/bin/env ruby
2
+ require 'rubygems'
3
+ require 'opengl'
4
+ require File.join(File.dirname($0), "..", "lib", "chingu")
5
+ include Gosu
6
+ include Chingu
7
+
8
+ #
9
+ # viewport-trait example
10
+ #
11
+ # Gotcha: collision_detection-trait debug mode currently borks up with viewports (only the visuals)
12
+ #
13
+ class Game < Chingu::Window
14
+ attr_reader :factor
15
+
16
+ def initialize
17
+ super
18
+ @factor = 6
19
+ self.input = { :escape => :exit }
20
+ switch_game_state(Level)
21
+ end
22
+ end
23
+
24
+ class Level < GameState
25
+ #
26
+ # This adds accessor 'viewport' to class and overrides draw() to use it.
27
+ #
28
+ has_trait :viewport
29
+
30
+ #
31
+ # We create our 3 different game objects in this order:
32
+ # 1) map 2) stars 3) player
33
+ # Since we don't give any zorders Chingu automatically increments zorder between each object created, putting player on top
34
+ #
35
+ def initialize(options = {})
36
+ super
37
+ @map = GameObject.create(:image => "background1.png", :factor => $window.factor, :rotation_center => :top_left, :retrofy => true)
38
+ self.viewport.x_min = 0
39
+ self.viewport.y_min = 0
40
+ self.viewport.x_max = @map.image.width * $window.factor - $window.width
41
+ self.viewport.y_max = @map.image.height * $window.factor - $window.height
42
+
43
+ # Create 40 stars scattered around the map
44
+ 40.times { |nr| Star.create(:x => rand * self.viewport.x_max, :y => rand * self.viewport.y_max) }
45
+
46
+ @droid = Droid.create(:x => 100, :y => 100)
47
+ end
48
+
49
+ def update
50
+ super
51
+
52
+ @droid.each_collision(Star) do |droid, star|
53
+ star.destroy
54
+ Sound["laser.wav"].play(0.5)
55
+ end
56
+
57
+ #
58
+ # Align viewport with the droid in the middle.
59
+ # This will make droid will be in the center of the screen all the time...
60
+ # ...except when hitting outer borders and viewport x/y _ min/max kicks in.
61
+ #
62
+ self.viewport.x = @droid.x - $window.width / 2
63
+ self.viewport.y = @droid.y - $window.height / 2
64
+
65
+ $window.caption = "viewport-trait example. Move with arrows! x/y: #{@droid.x}/#{@droid.y} - viewport x/y: #{self.viewport.x}/#{self.viewport.y} - FPS: #{$window.fps}"
66
+ end
67
+ end
68
+
69
+ class Droid < Chingu::GameObject
70
+ has_trait :bounding_box
71
+ has_traits :timer, :collision_detection
72
+
73
+ attr_accessor :last_x, :last_y
74
+
75
+ def initialize(options = {})
76
+ super
77
+
78
+ #
79
+ # This shows up the shorten versio of input-maps, where each key calls a method of the very same name.
80
+ # Use this by giving an array of symbols to self.input
81
+ #
82
+ self.input = [:holding_left, :holding_right, :holding_up, :holding_down]
83
+
84
+ # Load the full animation from tile-file media/droid.bmp
85
+ @full_animation = Chingu::Animation.new(:file => "droid.bmp", :size => [11,16]).retrofy
86
+
87
+ # Create new animations from specific frames and stuff them into easy to access hash
88
+ @animations = {}
89
+ @animations[:scan] = @full_animation[0..5]
90
+ @animations[:up] = @full_animation[6..7]
91
+ @animations[:down] = @full_animation[8..9]
92
+ @animations[:left] = @full_animation[10..11]
93
+ @animations[:right] = @full_animation[12..13]
94
+
95
+ # Start out by animation frames 0-5 (contained by @animations[:scan])
96
+ @animation = @animations[:scan]
97
+
98
+ self.factor = $window.factor
99
+ @last_x, @last_y = @x, @y
100
+ @speed = 3
101
+
102
+ update
103
+ end
104
+
105
+ def holding_left
106
+ @x -= @speed
107
+ @animation = @animations[:left]
108
+ end
109
+
110
+ def holding_right
111
+ @x += @speed
112
+ @animation = @animations[:right]
113
+ end
114
+
115
+ def holding_up
116
+ @y -= @speed
117
+ @animation = @animations[:up]
118
+ end
119
+
120
+ def holding_down
121
+ @y += @speed
122
+ @animation = @animations[:down]
123
+ end
124
+
125
+ # We don't need to call super() in update().
126
+ # By default GameObject#update is empty since it doesn't contain any gamelogic to speak of.
127
+ def update
128
+
129
+ # Move the animation forward by fetching the next frame and putting it into @image
130
+ # @image is drawn by default by GameObject#draw
131
+ @image = @animation.next
132
+
133
+ #
134
+ # If droid stands still, use the scanning animation
135
+ #
136
+ if @x == @last_x && @y == @last_y
137
+ @animation = @animations[:scan]
138
+ end
139
+
140
+ @x, @y = @last_x, @last_y if self.parent.outside_viewport?(self)
141
+ @last_x, @last_y = @x, @y
142
+ end
143
+ end
144
+
145
+ class Star < GameObject
146
+ has_trait :bounding_circle
147
+ has_trait :collision_detection
148
+
149
+ def initialize(options={})
150
+ super
151
+
152
+ @animation = Chingu::Animation.new(:file => media_path("Star.png"), :size => 25)
153
+ @image = @animation.next
154
+ self.color = Gosu::Color.new(0xff000000)
155
+ self.color.red = rand(255 - 40) + 40
156
+ self.color.green = rand(255 - 40) + 40
157
+ self.color.blue = rand(255 - 40) + 40
158
+
159
+ #
160
+ # A cached bounding circle will not adapt to changes in size, but it will follow objects X / Y
161
+ # Same is true for "cache_bounding_box"
162
+ #
163
+ cache_bounding_circle
164
+ end
165
+
166
+ def update
167
+ # Move the animation forward by fetching the next frame and putting it into @image
168
+ # @image is drawn by default by GameObject#draw
169
+ @image = @animation.next
170
+ end
171
+ end
172
+
173
+ Game.new.show
@@ -33,7 +33,7 @@ require_all "#{CHINGU_ROOT}/chingu/traits"
33
33
  require_all "#{CHINGU_ROOT}/chingu"
34
34
 
35
35
  module Chingu
36
- VERSION = "0.6.9"
36
+ VERSION = "0.7.0"
37
37
 
38
38
  DEBUG_COLOR = Gosu::Color.new(0xFFFF0000)
39
39
  DEBUG_ZORDER = 9999
@@ -118,8 +118,19 @@ module Chingu
118
118
  distance(self.x, self.y, object.x, object.y)
119
119
  end
120
120
 
121
+ #
122
+ # Our encapsulation of GOSU's image.draw_rot, uses the objects variables to draw it on screen if @visible is true
123
+ #
121
124
  def draw
122
125
  @image.draw_rot(@x, @y, @zorder, @angle, @center_x, @center_y, @factor_x, @factor_y, @color, @mode) if @visible
123
126
  end
127
+
128
+ #
129
+ # Works as #draw() but takes offsets for all draw_rot()-arguments. Used among others by by viewport-trait
130
+ #
131
+ def draw_relative(x=0, y=0, zorder=0, angle=0, center_x=0, center_y=0, factor_x=0, factor_y=0)
132
+ @image.draw_rot(@x+x, @y+y, @zorder+zorder, @angle+angle, @center_x+center_x, @center_y+center_y, @factor_x+factor_x, @factor_y+factor_y, @color, @mode) if @visible
133
+ end
134
+
124
135
  end
125
136
  end
@@ -27,8 +27,8 @@ module Chingu
27
27
  #
28
28
  class GameObjectList
29
29
 
30
- def initialize
31
- @game_objects = Array.new
30
+ def initialize(options = {})
31
+ @game_objects = options[:game_objects] || []
32
32
  #@game_objects_by_class = Hash.new
33
33
  end
34
34
 
@@ -73,7 +73,14 @@ module Chingu
73
73
  object.draw
74
74
  end
75
75
  end
76
-
76
+
77
+ def draw_relative(x=0, y=0, zorder=0, angle=0, center_x=0, center_y=0, factor_x=0, factor_y=0)
78
+ @game_objects.each{ |object| object.visible }.each do |object|
79
+ object.draw_trait
80
+ object.draw_relative(x, y, zorder, angle, center_x, center_y, factor_x, factor_y)
81
+ end
82
+ end
83
+
77
84
  def update
78
85
  @game_objects.select{ |object| not object.paused }.each do |object|
79
86
  object.update_trait
@@ -0,0 +1,75 @@
1
+ #--
2
+ #
3
+ # Chingu -- OpenGL accelerated 2D game framework for Ruby
4
+ # Copyright (C) 2009 ippa / ippa@rubylicio.us
5
+ #
6
+ # This library is free software; you can redistribute it and/or
7
+ # modify it under the terms of the GNU Lesser General Public
8
+ # License as published by the Free Software Foundation; either
9
+ # version 2.1 of the License, or (at your option) any later version.
10
+ #
11
+ # This library is distributed in the hope that it will be useful,
12
+ # but WITHOUT ANY WARRANTY; without even the implied warranty of
13
+ # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14
+ # Lesser General Public License for more details.
15
+ #
16
+ # You should have received a copy of the GNU Lesser General Public
17
+ # License along with this library; if not, write to the Free Software
18
+ # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19
+ #
20
+ #++
21
+
22
+ module Chingu
23
+ module Traits
24
+ #
25
+ # A chingu trait providing velocity and acceleration logic.
26
+ # Adds parameters: velocity_x/y, acceleration_x/y and modifies self.x / self.y
27
+ # Also keeps previous_x and previous_y which is the x, y before modification.
28
+ # Can be useful for example collision detection
29
+ #
30
+ module Viewport
31
+ attr_accessor :viewport
32
+
33
+ module ClassMethods
34
+ def initialize_trait(options = {})
35
+ trait_options[:viewport] = {:apply => true}.merge(options)
36
+ end
37
+ end
38
+
39
+ def setup_trait(options)
40
+ @viewport_options = {:debug => false}.merge(options)
41
+
42
+ @viewport = Chingu::Viewport.new()
43
+ @viewport.x = options[:viewport_x] || 0
44
+ @viewport.y = options[:viewport_y] || 0
45
+
46
+ super
47
+ end
48
+
49
+ #
50
+ # Returns true if object is inside view port, false if outside
51
+ # TODO: add view port height and width! (and use clip_to when painting?)
52
+ #
53
+ # This is a very flawed implementation, it Should take inte account objects
54
+ # height,width,factor_x,factor_y,center_x,center_y as well...
55
+ #
56
+ def inside_viewport?(object)
57
+ object.x >= @viewport.x && object.x <= (@viewport.x + $window.width) &&
58
+ object.y >= @viewport.y && object.y <= (@viewport.y + $window.height)
59
+ end
60
+
61
+ # Returns true object is outside the view port
62
+ def outside_viewport?(object)
63
+ not inside_viewport?(object)
64
+ end
65
+
66
+ #
67
+ # Override game states default draw that draws objects relative to the viewport.
68
+ # It only draws game objects inside the viewport. (GOSU does no such optimizations)
69
+ #
70
+ def draw
71
+ self.game_objects.draw_relative(-@viewport.x, -@viewport.y)
72
+ end
73
+ end
74
+ end
75
+ end
@@ -0,0 +1,68 @@
1
+ #--
2
+ #
3
+ # Chingu -- OpenGL accelerated 2D game framework for Ruby
4
+ # Copyright (C) 2009 ippa / ippa@rubylicio.us
5
+ #
6
+ # This library is free software; you can redistribute it and/or
7
+ # modify it under the terms of the GNU Lesser General Public
8
+ # License as published by the Free Software Foundation; either
9
+ # version 2.1 of the License, or (at your option) any later version.
10
+ #
11
+ # This library is distributed in the hope that it will be useful,
12
+ # but WITHOUT ANY WARRANTY; without even the implied warranty of
13
+ # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14
+ # Lesser General Public License for more details.
15
+ #
16
+ # You should have received a copy of the GNU Lesser General Public
17
+ # License along with this library; if not, write to the Free Software
18
+ # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19
+ #
20
+ #++
21
+
22
+ module Chingu
23
+ #
24
+ # A basic Viewport class
25
+ #
26
+ # TODO:
27
+ # Implement use of viewports angle, center_x, center_y, factor_x, factor_y
28
+ #
29
+ class Viewport
30
+ attr_accessor :x, :y, :x_min, :x_max, :y_min, :y_max
31
+ #attr_accessor :angle, :center_x, :center_y, :factor_x, :factor_y
32
+
33
+ def initialize(options = {})
34
+ @x = options[:x] || 0
35
+ @y = options[:y] || 0
36
+ @angle = options[:angle] || 0
37
+
38
+ #self.factor = options[:factor] || options[:scale] || 1.0
39
+ #@factor_x = options[:factor_x] if options[:factor_x]
40
+ #@factor_y = options[:factor_y] if options[:factor_y]
41
+
42
+ #self.center = options[:center] || 0.5
43
+ #@rotation_center = options[:rotation_center]
44
+ #self.rotation_center(options[:rotation_center]) if options[:rotation_center]
45
+
46
+ #@center_x = options[:center_x] if options[:center_x]
47
+ #@center_y = options[:center_y] if options[:center_y]
48
+
49
+ @x_min = nil
50
+ @x_max = nil
51
+ @y_min = nil
52
+ @y_max = nil
53
+ end
54
+
55
+ def x=(x)
56
+ @x = x
57
+ @x = @x_min if @x_min && @x < @x_min
58
+ @x = @x_max if @x_max && @x > @x_max
59
+ end
60
+
61
+ def y=(y)
62
+ @y = y
63
+ @y = @y_min if @y_min && @y < @y_min
64
+ @y = @y_max if @y_max && @y > @y_max
65
+ end
66
+
67
+ end
68
+ end
@@ -37,7 +37,7 @@ module Chingu
37
37
  include Chingu::Helpers::GameState # Easy access to the global game state-queue
38
38
  include Chingu::Helpers::GameObject # Adds game_objects_of_class etc ...
39
39
  include Chingu::Helpers::InputDispatcher # Input dispatch-helpers
40
- include Chingu::Helpers::InputClient # WIndow has its own inputmap
40
+ include Chingu::Helpers::InputClient # Window has its own inputmap
41
41
 
42
42
  attr_reader :root, :game_state_manager, :game_objects, :milliseconds_since_last_tick
43
43
 
@@ -56,7 +56,7 @@ module Chingu
56
56
 
57
57
  @fps_counter = FPSCounter.new
58
58
  @game_state_manager = GameStateManager.new
59
- @milliseconds_since_last_tick = 0
59
+ @milliseconds_since_last_tick = 0
60
60
  end
61
61
 
62
62
  #
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: chingu
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.6.9
4
+ version: 0.7.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - ippa
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2010-01-05 00:00:00 +01:00
12
+ date: 2010-01-14 00:00:00 +01:00
13
13
  default_executable:
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
@@ -20,7 +20,7 @@ dependencies:
20
20
  requirements:
21
21
  - - ">="
22
22
  - !ruby/object:Gem::Version
23
- version: 0.7.17
23
+ version: 0.7.18
24
24
  version:
25
25
  description: OpenGL accelerated 2D game framework for Ruby. Builds on Gosu (Ruby/C++) which provides all the core functionality. Chingu adds simple yet powerful game states, prettier input handling, deployment safe asset-handling, a basic re-usable game object and stackable game logic.
26
26
  email: ippa@rubylicio.us
@@ -54,6 +54,7 @@ files:
54
54
  - examples/example15_trait_timer2.rb
55
55
  - examples/example16_online_high_scores.rb
56
56
  - examples/example17_gosu_tutorial.rb
57
+ - examples/example18_viewport.rb
57
58
  - examples/example1_basics.rb
58
59
  - examples/example2_gamestate_basics.rb
59
60
  - examples/example3_parallax.rb
@@ -136,6 +137,8 @@ files:
136
137
  - lib/chingu/traits/retrofy.rb
137
138
  - lib/chingu/traits/timer.rb
138
139
  - lib/chingu/traits/velocity.rb
140
+ - lib/chingu/traits/viewport.rb
141
+ - lib/chingu/viewport.rb
139
142
  - lib/chingu/window.rb
140
143
  has_rdoc: true
141
144
  homepage: http://github.com/ippa/chingu
@@ -174,6 +177,7 @@ test_files:
174
177
  - examples/example15_trait_timer2.rb
175
178
  - examples/example16_online_high_scores.rb
176
179
  - examples/example17_gosu_tutorial.rb
180
+ - examples/example18_viewport.rb
177
181
  - examples/example1_basics.rb
178
182
  - examples/example2_gamestate_basics.rb
179
183
  - examples/example3_parallax.rb