ippa-chingu 0.5.3 → 0.5.4

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,3 +1,25 @@
1
+ #--
2
+ #
3
+ # Chingu -- Game framework built on top of the opengl accelerated gamelib Gosu
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
+
1
23
  #
2
24
  # Effect-class
3
25
  #
@@ -1,3 +1,25 @@
1
+ #--
2
+ #
3
+ # Chingu -- Game framework built on top of the opengl accelerated gamelib Gosu
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
+
1
23
  module Chingu
2
24
  #
3
25
  # Calculates a fps and a tick-time for use in update-calls
@@ -1,10 +1,31 @@
1
+ #--
2
+ #
3
+ # Chingu -- Game framework built on top of the opengl accelerated gamelib Gosu
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
+
1
23
  module Chingu
2
24
  #
3
25
  # GameObject is our BasisGameObject (class with framespecific stuff)
4
26
  #
5
27
  # On top of that, it encapsulates GOSUs Image#draw_rot and all its parameters.
6
28
  #
7
-
8
29
  class GameObject < Chingu::BasicGameObject
9
30
  attr_accessor :image, :x, :y, :angle, :center_x, :center_y, :factor_x, :factor_y, :color, :mode, :zorder
10
31
  has_trait :input, :rotation_center
@@ -50,6 +71,8 @@ module Chingu
50
71
  # gameloop/framework logic (TODO: use or get rid of)
51
72
  @update = options[:update] || true
52
73
  @draw = options[:draw] || true
74
+
75
+ setup_trait(options) if respond_to?(:setup_trait)
53
76
  end
54
77
 
55
78
  # Quick way of setting both factor_x and factor_y
@@ -1,3 +1,25 @@
1
+ #--
2
+ #
3
+ # Chingu -- Game framework built on top of the opengl accelerated gamelib Gosu
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
+
1
23
  module Chingu
2
24
  #
3
25
  # Chingu incorporates a basic push/pop game state system (as discussed here: http://www.gamedev.net/community/forums/topic.asp?topic_id=477320).
@@ -1,3 +1,25 @@
1
+ #--
2
+ #
3
+ # Chingu -- Game framework built on top of the opengl accelerated gamelib Gosu
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
+
1
23
  module Chingu
2
24
  #
3
25
  # GameStateManger is responsible for keeping track of game states with a simple pop/push stack.
@@ -1,3 +1,25 @@
1
+ #--
2
+ #
3
+ # Chingu -- Game framework built on top of the opengl accelerated gamelib Gosu
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
+
1
23
  #
2
24
  # Debug game state (F1 is default key to start/exit debug win, 'p' to pause game)
3
25
  #
@@ -1,3 +1,25 @@
1
+ #--
2
+ #
3
+ # Chingu -- Game framework built on top of the opengl accelerated gamelib Gosu
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
+
1
23
  #
2
24
  # Premade game state for chingu - Fade between two game states
3
25
  # Fade from the current game state to a new one whenever with:
@@ -1,3 +1,26 @@
1
+ #--
2
+ #
3
+ # Chingu -- Game framework built on top of the opengl accelerated gamelib Gosu
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
+
23
+
1
24
  #
2
25
  # Premade game state for chingu - A simple pause state.
3
26
  # Pause whenever with:
@@ -1,3 +1,25 @@
1
+ #--
2
+ #
3
+ # Chingu -- Game framework built on top of the opengl accelerated gamelib Gosu
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
+
1
23
  module Chingu
2
24
  #
3
25
  # Various helper-methods to manipulate the screen.
@@ -1,3 +1,25 @@
1
+ #--
2
+ #
3
+ # Chingu -- Game framework built on top of the opengl accelerated gamelib Gosu
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
+
1
23
  module Chingu
2
24
 
3
25
  module InputClient
@@ -1,3 +1,25 @@
1
+ #--
2
+ #
3
+ # Chingu -- Game framework built on top of the opengl accelerated gamelib Gosu
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
+
1
23
  module Chingu
2
24
  module Inflector
3
25
  def Inflector.camelize(lower_case_and_underscored_word, first_letter_in_uppercase = true)
@@ -0,0 +1,133 @@
1
+ #--
2
+ # Copyright (C)2009 Tony Arcieri
3
+ # You can redistribute this under the terms of the MIT license
4
+ # See file LICENSE for details
5
+ #++
6
+
7
+ module RequireAll
8
+ # A wonderfully simple way to load your code.
9
+ #
10
+ # The easiest way to use require_all is to just point it at a directory
11
+ # containing a bunch of .rb files. These files can be nested under
12
+ # subdirectories as well:
13
+ #
14
+ # require_all 'lib'
15
+ #
16
+ # This will find all the .rb files under the lib directory and load them.
17
+ # The proper order to load them in will be determined automatically.
18
+ #
19
+ # If the dependencies between the matched files are unresolvable, it will
20
+ # throw the first unresolvable NameError.
21
+ #
22
+ # You can also give it a glob, which will enumerate all the matching files:
23
+ #
24
+ # require_all 'lib/**/*.rb'
25
+ #
26
+ # It will also accept an array of files:
27
+ #
28
+ # require_all Dir.glob("blah/**/*.rb").reject { |f| stupid_file(f) }
29
+ #
30
+ # Or if you want, just list the files directly as arguments:
31
+ #
32
+ # require_all 'lib/a.rb', 'lib/b.rb', 'lib/c.rb', 'lib/d.rb'
33
+ #
34
+ def require_all(*args)
35
+ # Handle passing an array as an argument
36
+ args.flatten!
37
+
38
+ if args.size > 1
39
+ # If we got a list, those be are files!
40
+ files = args
41
+ else
42
+ arg = args.first
43
+ begin
44
+ # Try assuming we're doing plain ol' require compat
45
+ stat = File.stat(arg)
46
+
47
+ if stat.file?
48
+ files = [arg]
49
+ elsif stat.directory?
50
+ files = Dir.glob File.join(arg, '**', '*.rb')
51
+ else
52
+ raise ArgumentError, "#{arg} isn't a file or directory"
53
+ end
54
+ rescue Errno::ENOENT
55
+ # If the stat failed, maybe we have a glob!
56
+ files = Dir.glob arg
57
+
58
+ # Maybe it's an .rb file and the .rb was omitted
59
+ if File.file?(arg + '.rb')
60
+ require(arg + '.rb')
61
+ return true
62
+ end
63
+
64
+ # If we ain't got no files, the glob failed
65
+ raise LoadError, "no such file to load -- #{arg}" if files.empty?
66
+ end
67
+ end
68
+
69
+ # If there's nothing to load, you're doing it wrong!
70
+ raise LoadError, "no files to load" if files.empty?
71
+
72
+ files.map! { |file| File.expand_path file }
73
+
74
+ begin
75
+ failed = []
76
+ first_name_error = nil
77
+
78
+ # Attempt to load each file, rescuing which ones raise NameError for
79
+ # undefined constants. Keep trying to successively reload files that
80
+ # previously caused NameErrors until they've all been loaded or no new
81
+ # files can be loaded, indicating unresolvable dependencies.
82
+ files.each do |file|
83
+ begin
84
+ require file
85
+ rescue NameError => ex
86
+ failed << file
87
+ first_name_error ||= ex
88
+ rescue ArgumentError => ex
89
+ # Work around ActiveSuport freaking out... *sigh*
90
+ #
91
+ # ActiveSupport sometimes throws these exceptions and I really
92
+ # have no idea why. Code loading will work successfully if these
93
+ # exceptions are swallowed, although I've run into strange
94
+ # nondeterministic behaviors with constants mysteriously vanishing.
95
+ # I've gone spelunking through dependencies.rb looking for what
96
+ # exactly is going on, but all I ended up doing was making my eyes
97
+ # bleed.
98
+ #
99
+ # FIXME: If you can understand ActiveSupport's dependencies.rb
100
+ # better than I do I would *love* to find a better solution
101
+ raise unless ex.message["is not missing constant"]
102
+
103
+ STDERR.puts "Warning: require_all swallowed ActiveSupport 'is not missing constant' error"
104
+ STDERR.puts ex.backtrace[0..9]
105
+ end
106
+ end
107
+
108
+ # If this pass didn't resolve any NameErrors, we've hit an unresolvable
109
+ # dependency, so raise one of the exceptions we encountered.
110
+ if failed.size == files.size
111
+ raise first_name_error
112
+ else
113
+ files = failed
114
+ end
115
+ end until failed.empty?
116
+
117
+ true
118
+ end
119
+
120
+ # Works like require_all, but paths are relative to the caller rather than
121
+ # the current working directory
122
+ def require_rel(*paths)
123
+ # Handle passing an array as an argument
124
+ paths.flatten!
125
+
126
+ source_directory = File.dirname caller.first.sub(/:\d+$/, '')
127
+ paths.each do |path|
128
+ require_all File.join(source_directory, path)
129
+ end
130
+ end
131
+ end
132
+
133
+ include RequireAll
@@ -1,27 +1,120 @@
1
+ #--
2
+ #
3
+ # Chingu -- Game framework built on top of the opengl accelerated gamelib Gosu
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
+
1
23
  module Chingu
2
24
  module Traits
3
25
  #
4
- # TODO: everything. convert to class?
5
- #
26
+ #
6
27
  # Use QuadTrees: http://lab.polygonal.de/2007/09/09/quadtree-demonstration/
7
28
  #
29
+ # Makes use of 3 attributes
30
+ # @bounding_box - a Rect-instance, uses in bounding_box collisions
31
+ # @radius -
32
+ # @detect_collisions - [true|false], should object be checked for collisions with Object.each_collision
33
+ #
8
34
  module CollisionDetection
9
-
35
+ attr_accessor :bounding_box, :radius, :detect_collisions
36
+
10
37
  def self.included(base)
11
38
  base.extend(ClassMethods)
12
39
  end
13
40
 
41
+ #
42
+ # Automaticly try to set a bounding_box and radius. Don't overwrite if they already exists.
43
+ #
44
+ def setup_trait(options)
45
+ if @x and @y and @image
46
+ @bounding_box ||= Rect.new(@x, @y, @image.width, @image.height)
47
+ end
48
+
49
+ if @image
50
+ @radius ||= @image.width / 2
51
+ end
52
+
53
+ @detect_collisions = true
54
+ super
55
+ end
56
+
57
+ #
58
+ # The standard method called when self needs to be checked for a collision with another object
59
+ # By default it calls bounding_box_collision? which will check for intersectons between the
60
+ # two objects "bounding_box" attributs (a Chingu::Rect instance)
61
+ #
14
62
  def collision?(object2)
15
- self.rect.collide_rect?(object2.rect)
63
+ #bounding_box_collision?(object2)
64
+ radius_collision?(object2)
65
+ end
66
+
67
+ #
68
+ # Collide self with a given game object by checking both objects bounding_box'es
69
+ # Returns true if colliding.
70
+ #
71
+ def bounding_box_collision?(object2)
72
+ self.bounding_box.collide_rect?(object2.bounding_box)
73
+ end
74
+
75
+ #
76
+ # Collide self using distance between 2 objects and their radius.
77
+ # Returns true if colliding.
78
+ #
79
+ def radius_collision?(object2)
80
+ distance(self.x, self.y, object2.x, object2.y) < self.radius + object2.radius
81
+ end
82
+
83
+ #
84
+ # Have bounding box follow game objects x/y
85
+ #
86
+ def update
87
+ if defined?(@bounding_box) && @bounding_box.is_a?(Rect)
88
+ @bounding_box.x = self.x
89
+ @bounding_box.y = self.y
90
+ end
91
+
92
+ super
16
93
  end
94
+
17
95
 
18
96
  module ClassMethods
97
+ #
98
+ # Class method that will check for collisions between all instances of two classes
99
+ # and yield the 2 colliding game object instances.
100
+ #
101
+ # It will not collide objects with themselves.
102
+ #
103
+ # example:
104
+ #
105
+ # Enemy.each_collision(Bullet).each do |enemy, bullet| enemy.die!; end
106
+ #
107
+ #
19
108
  def each_collision(klasses = [])
20
109
  # Make sure klasses is always an array.
21
110
  Array(klasses).each do |klass|
22
111
  self.all.each do |object1|
23
112
  klass.all.each do |object2|
24
- yield object1, object2 if object1.collision?(object2)
113
+ next if object1 == object2 # Don't collide objects with themselves
114
+
115
+ if object1.detect_collisions && object2.detect_collisions
116
+ yield object1, object2 if object1.collision?(object2)
117
+ end
25
118
  end
26
119
  end
27
120
  end