gosu 0.8.7.2 → 0.9.0.pre1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/COPYING +29 -0
- data/Gosu/Audio.hpp +1 -0
- data/Gosu/Font.hpp +8 -4
- data/Gosu/Graphics.hpp +28 -18
- data/Gosu/GraphicsBase.hpp +17 -13
- data/Gosu/Image.hpp +54 -45
- data/Gosu/Input.hpp +5 -5
- data/Gosu/Version.hpp +3 -3
- data/Gosu/Window.hpp +1 -8
- data/README.txt +25 -0
- data/ext/gosu/gosu_wrap.cxx +1495 -1275
- data/ext/gosu/gosu_wrap.h +2 -2
- data/lib/gosu/patches.rb +71 -35
- data/lib/gosu/preview.rb +9 -142
- data/lib/gosu/swig_patches.rb +20 -10
- data/rdoc/gosu.rb +1185 -0
- data/src/Bitmap/BitmapUtils.cpp +9 -9
- data/src/Graphics/Common.hpp +3 -1
- data/src/Graphics/Graphics.cpp +100 -38
- data/src/Graphics/Image.cpp +57 -15
- data/src/Graphics/LargeImageData.cpp +9 -10
- data/src/Graphics/LargeImageData.hpp +1 -1
- data/src/Graphics/TexChunk.cpp +5 -6
- data/src/Graphics/TexChunk.hpp +1 -4
- data/src/Graphics/Texture.cpp +10 -3
- data/src/Graphics/Texture.hpp +1 -2
- data/src/Iconv.hpp +2 -2
- data/src/Input/Input.cpp +25 -9
- data/src/Input/InputTouch.mm +5 -3
- data/src/Text/Font.cpp +13 -8
- data/src/Window.cpp +66 -37
- data/src/WindowTouch.mm +3 -3
- metadata +79 -92
- data/examples/ChipmunkIntegration.rb +0 -275
- data/examples/CptnRuby.rb +0 -223
- data/examples/GosuZen.rb +0 -68
- data/examples/MoreChipmunkAndRMagick.rb +0 -155
- data/examples/OpenGLIntegration.rb +0 -226
- data/examples/RMagickIntegration.rb +0 -417
- data/examples/TextInput.rb +0 -154
- data/examples/Tutorial.rb +0 -131
- data/examples/media/Beep.wav +0 -0
- data/examples/media/CptnRuby Gem.png +0 -0
- data/examples/media/CptnRuby Map.txt +0 -25
- data/examples/media/CptnRuby Tileset.png +0 -0
- data/examples/media/CptnRuby.png +0 -0
- data/examples/media/Cursor.png +0 -0
- data/examples/media/Earth.png +0 -0
- data/examples/media/Explosion.wav +0 -0
- data/examples/media/Landscape.svg +0 -10
- data/examples/media/LargeStar.png +0 -0
- data/examples/media/Smoke.png +0 -0
- data/examples/media/Soldier.png +0 -0
- data/examples/media/Space.png +0 -0
- data/examples/media/Star.png +0 -0
- data/examples/media/Starfighter.bmp +0 -0
data/src/WindowTouch.mm
CHANGED
@@ -3,7 +3,7 @@
|
|
3
3
|
#include <Gosu/Audio.hpp>
|
4
4
|
#include <Gosu/Input.hpp>
|
5
5
|
#include "MacUtility.hpp"
|
6
|
-
#include "
|
6
|
+
#include "GosuView.hpp"
|
7
7
|
|
8
8
|
#import <CoreGraphics/CoreGraphics.h>
|
9
9
|
#import <UIKit/UIKit.h>
|
@@ -19,7 +19,7 @@ namespace Gosu
|
|
19
19
|
{
|
20
20
|
static CGRect &screenRect()
|
21
21
|
{
|
22
|
-
static CGRect screenRect = [[UIScreen mainScreen]
|
22
|
+
static CGRect screenRect = [[UIScreen mainScreen] nativeBounds];
|
23
23
|
return screenRect;
|
24
24
|
}
|
25
25
|
|
@@ -167,7 +167,7 @@ Gosu::Window::Window(unsigned width, unsigned height,
|
|
167
167
|
pimpl->window.obj().rootViewController = pimpl->controller.obj();
|
168
168
|
|
169
169
|
pimpl->graphics.reset(new Graphics(screenHeight(), screenWidth(), false));
|
170
|
-
pimpl->graphics->setResolution(
|
170
|
+
pimpl->graphics->setResolution(width, height);
|
171
171
|
pimpl->input.reset(new Input(gosuView, updateInterval));
|
172
172
|
pimpl->input->onTouchBegan = std::tr1::bind(&Window::touchBegan, this, _1);
|
173
173
|
pimpl->input->onTouchMoved = std::tr1::bind(&Window::touchMoved, this, _1);
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: gosu
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.9.0.pre1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Julian Raschke
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2015-
|
11
|
+
date: 2015-05-16 00:00:00.000000000 Z
|
12
12
|
dependencies: []
|
13
13
|
description: |2
|
14
14
|
2D game development library.
|
@@ -22,130 +22,74 @@ email: julian@raschke.de
|
|
22
22
|
executables: []
|
23
23
|
extensions:
|
24
24
|
- ext/gosu/extconf.rb
|
25
|
-
extra_rdoc_files:
|
25
|
+
extra_rdoc_files:
|
26
|
+
- README.txt
|
27
|
+
- COPYING
|
28
|
+
- rdoc/gosu.rb
|
26
29
|
files:
|
27
|
-
- Gosu/Audio.hpp
|
28
|
-
- Gosu/AutoLink.hpp
|
29
|
-
- Gosu/Bitmap.hpp
|
30
|
-
- Gosu/Buttons.hpp
|
31
|
-
- Gosu/Color.hpp
|
32
|
-
- Gosu/Directories.hpp
|
33
|
-
- Gosu/Font.hpp
|
34
|
-
- Gosu/Fwd.hpp
|
35
|
-
- Gosu/Gosu.hpp
|
36
|
-
- Gosu/Graphics.hpp
|
37
|
-
- Gosu/GraphicsBase.hpp
|
38
|
-
- Gosu/IO.hpp
|
39
|
-
- Gosu/Image.hpp
|
40
|
-
- Gosu/ImageData.hpp
|
41
|
-
- Gosu/Input.hpp
|
42
|
-
- Gosu/Inspection.hpp
|
43
|
-
- Gosu/Math.hpp
|
44
|
-
- Gosu/Platform.hpp
|
45
|
-
- Gosu/Sockets.hpp
|
46
|
-
- Gosu/TR1.hpp
|
47
|
-
- Gosu/Text.hpp
|
48
|
-
- Gosu/TextInput.hpp
|
49
|
-
- Gosu/Timing.hpp
|
50
|
-
- Gosu/Utility.hpp
|
51
|
-
- Gosu/Version.hpp
|
52
|
-
- Gosu/WinUtility.hpp
|
53
|
-
- Gosu/Window.hpp
|
54
|
-
- examples/ChipmunkIntegration.rb
|
55
|
-
- examples/CptnRuby.rb
|
56
|
-
- examples/GosuZen.rb
|
57
|
-
- examples/MoreChipmunkAndRMagick.rb
|
58
|
-
- examples/OpenGLIntegration.rb
|
59
|
-
- examples/RMagickIntegration.rb
|
60
|
-
- examples/TextInput.rb
|
61
|
-
- examples/Tutorial.rb
|
62
|
-
- examples/media/Beep.wav
|
63
|
-
- examples/media/CptnRuby Gem.png
|
64
|
-
- examples/media/CptnRuby Map.txt
|
65
|
-
- examples/media/CptnRuby Tileset.png
|
66
|
-
- examples/media/CptnRuby.png
|
67
|
-
- examples/media/Cursor.png
|
68
|
-
- examples/media/Earth.png
|
69
|
-
- examples/media/Explosion.wav
|
70
|
-
- examples/media/Landscape.svg
|
71
|
-
- examples/media/LargeStar.png
|
72
|
-
- examples/media/Smoke.png
|
73
|
-
- examples/media/Soldier.png
|
74
|
-
- examples/media/Space.png
|
75
|
-
- examples/media/Star.png
|
76
|
-
- examples/media/Starfighter.bmp
|
77
30
|
- ext/gosu/extconf.rb
|
78
|
-
- ext/gosu/gosu_wrap.cxx
|
79
|
-
- ext/gosu/gosu_wrap.h
|
80
|
-
- lib/gosu.rb
|
81
31
|
- lib/gosu/patches.rb
|
82
32
|
- lib/gosu/preview.rb
|
83
33
|
- lib/gosu/run.rb
|
84
34
|
- lib/gosu/swig_patches.rb
|
85
35
|
- lib/gosu/zen.rb
|
36
|
+
- lib/gosu.rb
|
37
|
+
- ext/gosu/gosu_wrap.h
|
38
|
+
- ext/gosu/gosu_wrap.cxx
|
39
|
+
- src/X11vroot.h
|
86
40
|
- src/Audio/ALChannelManagement.hpp
|
87
41
|
- src/Audio/AudioFile.hpp
|
88
|
-
- src/Audio/AudioOpenAL.cpp
|
89
|
-
- src/Audio/AudioOpenAL.mm
|
90
42
|
- src/Audio/AudioToolboxFile.hpp
|
91
43
|
- src/Audio/OggFile.hpp
|
92
44
|
- src/Audio/SndFile.hpp
|
45
|
+
- src/GosuView.hpp
|
46
|
+
- src/Graphics/BlockAllocator.hpp
|
47
|
+
- src/Graphics/ClipRectStack.hpp
|
48
|
+
- src/Graphics/Common.hpp
|
49
|
+
- src/Graphics/DrawOp.hpp
|
50
|
+
- src/Graphics/DrawOpQueue.hpp
|
51
|
+
- src/Graphics/LargeImageData.hpp
|
52
|
+
- src/Graphics/Macro.hpp
|
53
|
+
- src/Graphics/RenderState.hpp
|
54
|
+
- src/Graphics/TexChunk.hpp
|
55
|
+
- src/Graphics/Texture.hpp
|
56
|
+
- src/Graphics/TransformStack.hpp
|
57
|
+
- src/Iconv.hpp
|
58
|
+
- src/Input/AccelerometerReader.hpp
|
59
|
+
- src/MacUtility.hpp
|
60
|
+
- src/Sockets/Socket.hpp
|
61
|
+
- src/Text/FormattedString.hpp
|
62
|
+
- src/Audio/AudioOpenAL.cpp
|
93
63
|
- src/Bitmap/Bitmap.cpp
|
94
|
-
- src/Bitmap/BitmapApple.mm
|
95
64
|
- src/Bitmap/BitmapBMP.cpp
|
96
65
|
- src/Bitmap/BitmapColorKey.cpp
|
97
66
|
- src/Bitmap/BitmapFreeImage.cpp
|
98
67
|
- src/Bitmap/BitmapGDIplus.cpp
|
99
68
|
- src/Bitmap/BitmapUtils.cpp
|
100
|
-
- src/DirectoriesMac.mm
|
101
|
-
- src/DirectoriesTouch.mm
|
102
69
|
- src/DirectoriesUnix.cpp
|
103
70
|
- src/DirectoriesWin.cpp
|
104
71
|
- src/FileUnix.cpp
|
105
72
|
- src/FileWin.cpp
|
106
|
-
- src/GosuView.hpp
|
107
|
-
- src/GosuView.mm
|
108
73
|
- src/Graphics/BlockAllocator.cpp
|
109
|
-
- src/Graphics/BlockAllocator.hpp
|
110
|
-
- src/Graphics/ClipRectStack.hpp
|
111
74
|
- src/Graphics/Color.cpp
|
112
|
-
- src/Graphics/Common.hpp
|
113
|
-
- src/Graphics/DrawOp.hpp
|
114
|
-
- src/Graphics/DrawOpQueue.hpp
|
115
75
|
- src/Graphics/Graphics.cpp
|
116
76
|
- src/Graphics/Image.cpp
|
117
77
|
- src/Graphics/LargeImageData.cpp
|
118
|
-
- src/Graphics/LargeImageData.hpp
|
119
|
-
- src/Graphics/Macro.hpp
|
120
|
-
- src/Graphics/RenderState.hpp
|
121
78
|
- src/Graphics/Resolution.cpp
|
122
|
-
- src/Graphics/ResolutionApple.mm
|
123
79
|
- src/Graphics/TexChunk.cpp
|
124
|
-
- src/Graphics/TexChunk.hpp
|
125
80
|
- src/Graphics/Texture.cpp
|
126
|
-
- src/Graphics/Texture.hpp
|
127
81
|
- src/Graphics/Transform.cpp
|
128
|
-
- src/Graphics/TransformStack.hpp
|
129
|
-
- src/IO.cpp
|
130
|
-
- src/Iconv.hpp
|
131
|
-
- src/Input/AccelerometerReader.hpp
|
132
|
-
- src/Input/AccelerometerReader.mm
|
133
82
|
- src/Input/Input.cpp
|
134
|
-
- src/Input/InputTouch.mm
|
135
83
|
- src/Input/TextInput.cpp
|
136
|
-
- src/Input/TextInputMac.mm
|
137
84
|
- src/Inspection.cpp
|
138
|
-
- src/
|
85
|
+
- src/IO.cpp
|
139
86
|
- src/Math.cpp
|
140
87
|
- src/Sockets/CommSocket.cpp
|
141
88
|
- src/Sockets/ListenerSocket.cpp
|
142
89
|
- src/Sockets/MessageSocket.cpp
|
143
90
|
- src/Sockets/Socket.cpp
|
144
|
-
- src/Sockets/Socket.hpp
|
145
91
|
- src/Text/Font.cpp
|
146
|
-
- src/Text/FormattedString.hpp
|
147
92
|
- src/Text/Text.cpp
|
148
|
-
- src/Text/TextApple.mm
|
149
93
|
- src/Text/TextMac.cpp
|
150
94
|
- src/Text/TextTTFWin.cpp
|
151
95
|
- src/Text/TextUnix.cpp
|
@@ -154,18 +98,61 @@ files:
|
|
154
98
|
- src/TimingUnix.cpp
|
155
99
|
- src/TimingWin.cpp
|
156
100
|
- src/Utility.cpp
|
157
|
-
- src/UtilityApple.mm
|
158
101
|
- src/UtilityWin.cpp
|
102
|
+
- src/Window.cpp
|
159
103
|
- src/WinMain.cpp
|
160
104
|
- src/WinUtility.cpp
|
161
|
-
- src/
|
105
|
+
- src/Audio/AudioOpenAL.mm
|
106
|
+
- src/Bitmap/BitmapApple.mm
|
107
|
+
- src/DirectoriesMac.mm
|
108
|
+
- src/DirectoriesTouch.mm
|
109
|
+
- src/GosuView.mm
|
110
|
+
- src/Graphics/ResolutionApple.mm
|
111
|
+
- src/Input/AccelerometerReader.mm
|
112
|
+
- src/Input/InputTouch.mm
|
113
|
+
- src/Input/TextInputMac.mm
|
114
|
+
- src/Text/TextApple.mm
|
115
|
+
- src/UtilityApple.mm
|
162
116
|
- src/WindowTouch.mm
|
163
|
-
-
|
117
|
+
- Gosu/Audio.hpp
|
118
|
+
- Gosu/AutoLink.hpp
|
119
|
+
- Gosu/Bitmap.hpp
|
120
|
+
- Gosu/Buttons.hpp
|
121
|
+
- Gosu/Color.hpp
|
122
|
+
- Gosu/Directories.hpp
|
123
|
+
- Gosu/Font.hpp
|
124
|
+
- Gosu/Fwd.hpp
|
125
|
+
- Gosu/Gosu.hpp
|
126
|
+
- Gosu/Graphics.hpp
|
127
|
+
- Gosu/GraphicsBase.hpp
|
128
|
+
- Gosu/Image.hpp
|
129
|
+
- Gosu/ImageData.hpp
|
130
|
+
- Gosu/Input.hpp
|
131
|
+
- Gosu/Inspection.hpp
|
132
|
+
- Gosu/IO.hpp
|
133
|
+
- Gosu/Math.hpp
|
134
|
+
- Gosu/Platform.hpp
|
135
|
+
- Gosu/Sockets.hpp
|
136
|
+
- Gosu/Text.hpp
|
137
|
+
- Gosu/TextInput.hpp
|
138
|
+
- Gosu/Timing.hpp
|
139
|
+
- Gosu/TR1.hpp
|
140
|
+
- Gosu/Utility.hpp
|
141
|
+
- Gosu/Version.hpp
|
142
|
+
- Gosu/Window.hpp
|
143
|
+
- Gosu/WinUtility.hpp
|
144
|
+
- README.txt
|
145
|
+
- COPYING
|
146
|
+
- rdoc/gosu.rb
|
164
147
|
homepage: http://www.libgosu.org/
|
165
148
|
licenses: []
|
166
149
|
metadata: {}
|
167
150
|
post_install_message:
|
168
|
-
rdoc_options:
|
151
|
+
rdoc_options:
|
152
|
+
- -m
|
153
|
+
- README.txt
|
154
|
+
- -x
|
155
|
+
- lib
|
169
156
|
require_paths:
|
170
157
|
- lib
|
171
158
|
required_ruby_version: !ruby/object:Gem::Requirement
|
@@ -175,12 +162,12 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
175
162
|
version: 1.8.2
|
176
163
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
177
164
|
requirements:
|
178
|
-
- - '
|
165
|
+
- - '>'
|
179
166
|
- !ruby/object:Gem::Version
|
180
|
-
version:
|
167
|
+
version: 1.3.1
|
181
168
|
requirements: []
|
182
169
|
rubyforge_project:
|
183
|
-
rubygems_version: 2.
|
170
|
+
rubygems_version: 2.0.14
|
184
171
|
signing_key:
|
185
172
|
specification_version: 4
|
186
173
|
summary: 2D game development library.
|
@@ -1,275 +0,0 @@
|
|
1
|
-
## File: ChipmunkIntegration.rb
|
2
|
-
## Author: Dirk Johnson
|
3
|
-
## Version: 1.0.0
|
4
|
-
## Date: 2007-10-05
|
5
|
-
## License: Same as for Gosu (MIT)
|
6
|
-
## Comments: Based on the Gosu Ruby Tutorial, but incorporating the Chipmunk Physics Engine
|
7
|
-
## See https://github.com/jlnr/gosu/wiki/Ruby-Chipmunk-Integration for the accompanying text.
|
8
|
-
|
9
|
-
require 'rubygems'
|
10
|
-
require 'gosu'
|
11
|
-
require 'chipmunk'
|
12
|
-
|
13
|
-
SCREEN_WIDTH = 640
|
14
|
-
SCREEN_HEIGHT = 480
|
15
|
-
|
16
|
-
# The number of steps to process every Gosu update
|
17
|
-
# The Player ship can get going so fast as to "move through" a
|
18
|
-
# star without triggering a collision; an increased number of
|
19
|
-
# Chipmunk step calls per update will effectively avoid this issue
|
20
|
-
SUBSTEPS = 6
|
21
|
-
|
22
|
-
# Convenience method for converting from radians to a Vec2 vector.
|
23
|
-
class Numeric
|
24
|
-
def radians_to_vec2
|
25
|
-
CP::Vec2.new(Math::cos(self), Math::sin(self))
|
26
|
-
end
|
27
|
-
end
|
28
|
-
|
29
|
-
# Layering of sprites
|
30
|
-
module ZOrder
|
31
|
-
Background, Stars, Player, UI = *0..3
|
32
|
-
end
|
33
|
-
|
34
|
-
# This game will have one Player in the form of a ship
|
35
|
-
class Player
|
36
|
-
attr_reader :shape
|
37
|
-
|
38
|
-
def initialize(window, shape)
|
39
|
-
@image = Gosu::Image.new(window, "media/Starfighter.bmp", false)
|
40
|
-
@shape = shape
|
41
|
-
@shape.body.p = CP::Vec2.new(0.0, 0.0) # position
|
42
|
-
@shape.body.v = CP::Vec2.new(0.0, 0.0) # velocity
|
43
|
-
|
44
|
-
# Keep in mind that down the screen is positive y, which means that PI/2 radians,
|
45
|
-
# which you might consider the top in the traditional Trig unit circle sense is actually
|
46
|
-
# the bottom; thus 3PI/2 is the top
|
47
|
-
@shape.body.a = (3*Math::PI/2.0) # angle in radians; faces towards top of screen
|
48
|
-
end
|
49
|
-
|
50
|
-
# Directly set the position of our Player
|
51
|
-
def warp(vect)
|
52
|
-
@shape.body.p = vect
|
53
|
-
end
|
54
|
-
|
55
|
-
# Apply negative Torque; Chipmunk will do the rest
|
56
|
-
# SUBSTEPS is used as a divisor to keep turning rate constant
|
57
|
-
# even if the number of steps per update are adjusted
|
58
|
-
def turn_left
|
59
|
-
@shape.body.t -= 400.0/SUBSTEPS
|
60
|
-
end
|
61
|
-
|
62
|
-
# Apply positive Torque; Chipmunk will do the rest
|
63
|
-
# SUBSTEPS is used as a divisor to keep turning rate constant
|
64
|
-
# even if the number of steps per update are adjusted
|
65
|
-
def turn_right
|
66
|
-
@shape.body.t += 400.0/SUBSTEPS
|
67
|
-
end
|
68
|
-
|
69
|
-
# Apply forward force; Chipmunk will do the rest
|
70
|
-
# SUBSTEPS is used as a divisor to keep acceleration rate constant
|
71
|
-
# even if the number of steps per update are adjusted
|
72
|
-
# Here we must convert the angle (facing) of the body into
|
73
|
-
# forward momentum by creating a vector in the direction of the facing
|
74
|
-
# and with a magnitude representing the force we want to apply
|
75
|
-
def accelerate
|
76
|
-
@shape.body.apply_force((@shape.body.a.radians_to_vec2 * (3000.0/SUBSTEPS)), CP::Vec2.new(0.0, 0.0))
|
77
|
-
end
|
78
|
-
|
79
|
-
# Apply even more forward force
|
80
|
-
# See accelerate for more details
|
81
|
-
def boost
|
82
|
-
@shape.body.apply_force((@shape.body.a.radians_to_vec2 * (3000.0)), CP::Vec2.new(0.0, 0.0))
|
83
|
-
end
|
84
|
-
|
85
|
-
# Apply reverse force
|
86
|
-
# See accelerate for more details
|
87
|
-
def reverse
|
88
|
-
@shape.body.apply_force(-(@shape.body.a.radians_to_vec2 * (1000.0/SUBSTEPS)), CP::Vec2.new(0.0, 0.0))
|
89
|
-
end
|
90
|
-
|
91
|
-
# Wrap to the other side of the screen when we fly off the edge
|
92
|
-
def validate_position
|
93
|
-
l_position = CP::Vec2.new(@shape.body.p.x % SCREEN_WIDTH, @shape.body.p.y % SCREEN_HEIGHT)
|
94
|
-
@shape.body.p = l_position
|
95
|
-
end
|
96
|
-
|
97
|
-
def draw
|
98
|
-
@image.draw_rot(@shape.body.p.x, @shape.body.p.y, ZOrder::Player, @shape.body.a.radians_to_gosu)
|
99
|
-
end
|
100
|
-
end
|
101
|
-
|
102
|
-
# See how simple our Star is?
|
103
|
-
# Of course... it just sits around and looks good...
|
104
|
-
class Star
|
105
|
-
attr_reader :shape
|
106
|
-
|
107
|
-
def initialize(animation, shape)
|
108
|
-
@animation = animation
|
109
|
-
@color = Gosu::Color.new(0xff000000)
|
110
|
-
@color.red = rand(255 - 40) + 40
|
111
|
-
@color.green = rand(255 - 40) + 40
|
112
|
-
@color.blue = rand(255 - 40) + 40
|
113
|
-
@shape = shape
|
114
|
-
@shape.body.p = CP::Vec2.new(rand * SCREEN_WIDTH, rand * SCREEN_HEIGHT) # position
|
115
|
-
@shape.body.v = CP::Vec2.new(0.0, 0.0) # velocity
|
116
|
-
@shape.body.a = (3*Math::PI/2.0) # angle in radians; faces towards top of screen
|
117
|
-
end
|
118
|
-
|
119
|
-
def draw
|
120
|
-
img = @animation[Gosu::milliseconds / 100 % @animation.size];
|
121
|
-
img.draw(@shape.body.p.x - img.width / 2.0, @shape.body.p.y - img.height / 2.0, ZOrder::Stars, 1, 1, @color, :add)
|
122
|
-
end
|
123
|
-
end
|
124
|
-
|
125
|
-
# The Gosu::Window is always the "environment" of our game
|
126
|
-
# It also provides the pulse of our game
|
127
|
-
class GameWindow < Gosu::Window
|
128
|
-
def initialize
|
129
|
-
super(SCREEN_WIDTH, SCREEN_HEIGHT, false, 16)
|
130
|
-
self.caption = "Gosu & Chipmunk Integration Demo"
|
131
|
-
@background_image = Gosu::Image.new(self, "media/Space.png", true)
|
132
|
-
|
133
|
-
# Put the beep here, as it is the environment now that determines collision
|
134
|
-
@beep = Gosu::Sample.new(self, "media/Beep.wav")
|
135
|
-
|
136
|
-
# Put the score here, as it is the environment that tracks this now
|
137
|
-
@score = 0
|
138
|
-
@font = Gosu::Font.new(self, Gosu::default_font_name, 20)
|
139
|
-
|
140
|
-
# Time increment over which to apply a physics "step" ("delta t")
|
141
|
-
@dt = (1.0/60.0)
|
142
|
-
|
143
|
-
# Create our Space and set its damping
|
144
|
-
# A damping of 0.8 causes the ship bleed off its force and torque over time
|
145
|
-
# This is not realistic behavior in a vacuum of space, but it gives the game
|
146
|
-
# the feel I'd like in this situation
|
147
|
-
@space = CP::Space.new
|
148
|
-
@space.damping = 0.8
|
149
|
-
|
150
|
-
# Create the Body for the Player
|
151
|
-
body = CP::Body.new(10.0, 150.0)
|
152
|
-
|
153
|
-
# In order to create a shape, we must first define it
|
154
|
-
# Chipmunk defines 3 types of Shapes: Segments, Circles and Polys
|
155
|
-
# We'll use s simple, 4 sided Poly for our Player (ship)
|
156
|
-
# You need to define the vectors so that the "top" of the Shape is towards 0 radians (the right)
|
157
|
-
shape_array = [CP::Vec2.new(-25.0, -25.0), CP::Vec2.new(-25.0, 25.0), CP::Vec2.new(25.0, 1.0), CP::Vec2.new(25.0, -1.0)]
|
158
|
-
shape = CP::Shape::Poly.new(body, shape_array, CP::Vec2.new(0,0))
|
159
|
-
|
160
|
-
# The collision_type of a shape allows us to set up special collision behavior
|
161
|
-
# based on these types. The actual value for the collision_type is arbitrary
|
162
|
-
# and, as long as it is consistent, will work for us; of course, it helps to have it make sense
|
163
|
-
shape.collision_type = :ship
|
164
|
-
|
165
|
-
@space.add_body(body)
|
166
|
-
@space.add_shape(shape)
|
167
|
-
|
168
|
-
@player = Player.new(self, shape)
|
169
|
-
@player.warp(CP::Vec2.new(320, 240)) # move to the center of the window
|
170
|
-
|
171
|
-
@star_anim = Gosu::Image::load_tiles(self, "media/Star.png", 25, 25, false)
|
172
|
-
@stars = Array.new
|
173
|
-
|
174
|
-
# Here we define what is supposed to happen when a Player (ship) collides with a Star
|
175
|
-
# I create a @remove_shapes array because we cannot remove either Shapes or Bodies
|
176
|
-
# from Space within a collision closure, rather, we have to wait till the closure
|
177
|
-
# is through executing, then we can remove the Shapes and Bodies
|
178
|
-
# In this case, the Shapes and the Bodies they own are removed in the Gosu::Window.update phase
|
179
|
-
# by iterating over the @remove_shapes array
|
180
|
-
# Also note that both Shapes involved in the collision are passed into the closure
|
181
|
-
# in the same order that their collision_types are defined in the add_collision_func call
|
182
|
-
@remove_shapes = []
|
183
|
-
@space.add_collision_func(:ship, :star) do |ship_shape, star_shape|
|
184
|
-
@score += 10
|
185
|
-
@beep.play
|
186
|
-
@remove_shapes << star_shape
|
187
|
-
end
|
188
|
-
|
189
|
-
# Here we tell Space that we don't want one star bumping into another
|
190
|
-
# The reason we need to do this is because when the Player hits a Star,
|
191
|
-
# the Star will travel until it is removed in the update cycle below
|
192
|
-
# which means it may collide and therefore push other Stars
|
193
|
-
# To see the effect, remove this line and play the game, every once in a while
|
194
|
-
# you'll see a Star moving
|
195
|
-
@space.add_collision_func(:star, :star, &nil)
|
196
|
-
end
|
197
|
-
|
198
|
-
def update
|
199
|
-
# Step the physics environment SUBSTEPS times each update
|
200
|
-
SUBSTEPS.times do
|
201
|
-
# This iterator makes an assumption of one Shape per Star making it safe to remove
|
202
|
-
# each Shape's Body as it comes up
|
203
|
-
# If our Stars had multiple Shapes, as would be required if we were to meticulously
|
204
|
-
# define their true boundaries, we couldn't do this as we would remove the Body
|
205
|
-
# multiple times
|
206
|
-
# We would probably solve this by creating a separate @remove_bodies array to remove the Bodies
|
207
|
-
# of the Stars that were gathered by the Player
|
208
|
-
@remove_shapes.each do |shape|
|
209
|
-
@stars.delete_if { |star| star.shape == shape }
|
210
|
-
@space.remove_body(shape.body)
|
211
|
-
@space.remove_shape(shape)
|
212
|
-
end
|
213
|
-
@remove_shapes.clear # clear out the shapes for next pass
|
214
|
-
|
215
|
-
# When a force or torque is set on a Body, it is cumulative
|
216
|
-
# This means that the force you applied last SUBSTEP will compound with the
|
217
|
-
# force applied this SUBSTEP; which is probably not the behavior you want
|
218
|
-
# We reset the forces on the Player each SUBSTEP for this reason
|
219
|
-
@player.shape.body.reset_forces
|
220
|
-
|
221
|
-
# Wrap around the screen to the other side
|
222
|
-
@player.validate_position
|
223
|
-
|
224
|
-
# Check keyboard
|
225
|
-
if button_down? Gosu::KbLeft
|
226
|
-
@player.turn_left
|
227
|
-
end
|
228
|
-
if button_down? Gosu::KbRight
|
229
|
-
@player.turn_right
|
230
|
-
end
|
231
|
-
|
232
|
-
if button_down? Gosu::KbUp
|
233
|
-
if ( (button_down? Gosu::KbRightShift) || (button_down? Gosu::KbLeftShift) )
|
234
|
-
@player.boost
|
235
|
-
else
|
236
|
-
@player.accelerate
|
237
|
-
end
|
238
|
-
elsif button_down? Gosu::KbDown
|
239
|
-
@player.reverse
|
240
|
-
end
|
241
|
-
|
242
|
-
# Perform the step over @dt period of time
|
243
|
-
# For best performance @dt should remain consistent for the game
|
244
|
-
@space.step(@dt)
|
245
|
-
end
|
246
|
-
|
247
|
-
# Each update (not SUBSTEP) we see if we need to add more Stars
|
248
|
-
if rand(100) < 4 and @stars.size < 25 then
|
249
|
-
body = CP::Body.new(0.0001, 0.0001)
|
250
|
-
shape = CP::Shape::Circle.new(body, 25/2, CP::Vec2.new(0.0, 0.0))
|
251
|
-
shape.collision_type = :star
|
252
|
-
|
253
|
-
@space.add_body(body)
|
254
|
-
@space.add_shape(shape)
|
255
|
-
|
256
|
-
@stars.push(Star.new(@star_anim, shape))
|
257
|
-
end
|
258
|
-
end
|
259
|
-
|
260
|
-
def draw
|
261
|
-
@background_image.draw(0, 0, ZOrder::Background)
|
262
|
-
@player.draw
|
263
|
-
@stars.each { |star| star.draw }
|
264
|
-
@font.draw("Score: #{@score}", 10, 10, ZOrder::UI, 1.0, 1.0, 0xffffff00)
|
265
|
-
end
|
266
|
-
|
267
|
-
def button_down(id)
|
268
|
-
if id == Gosu::KbEscape
|
269
|
-
close
|
270
|
-
end
|
271
|
-
end
|
272
|
-
end
|
273
|
-
|
274
|
-
window = GameWindow.new
|
275
|
-
window.show
|