opal-phaser 0.0.4 → 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: db0834b0247989f1599e048000a70ccceaae04ff
4
- data.tar.gz: e278d3ff580eb053c7ef15c8af13d0f513cf7e04
3
+ metadata.gz: ba2df4d99da9e4b3ea41300062f9d44d9aeac31e
4
+ data.tar.gz: 635d8e5cfb6d3929fa9e4b5aeb9c8f96c8315df5
5
5
  SHA512:
6
- metadata.gz: 167e490d086a486a33a314871fc9a5868644a58712d9ad0a5f7fabc6d0c6493d9810eaf42517224864d417abde0bc6dca6d73e7012cfa520163e2088480dc421
7
- data.tar.gz: 97a6dd235d5671de12b6df63b57df0224669ed8a39fca02a20168b63bb84fe6b688d024ce1f38db8cea7fb86ddc1ecc164464a6b64e1ac5e99a8bf6de66f4a6f
6
+ metadata.gz: 67169bb7c05f882b8320c3195ea81354b5e51cc81b6657ca31fb750c10aedd5abbf532f3eaba9024020a2826d5f3e06f9b5ff9cbee50ae95a59ae4af69e2cc90
7
+ data.tar.gz: e862e90ddb873a15c5f7e02a7b1b061d407cd309ff9d9a68386edc510919f56bdccde11babaa7e0dcb275fd3074ce04a95785166e11fc1fb12d9ab98d26efa6e
data/demo/app/main.rb CHANGED
@@ -1,97 +1,220 @@
1
1
  require 'opal'
2
2
  require 'opal-phaser'
3
+ require 'pp'
3
4
 
4
- class Game
5
- def initialize
6
- @player = nil
7
- @platforms = nil
8
- @cursors = nil
9
- @stars = nil
5
+ class Score
6
+ attr_accessor :score
7
+ attr_reader :scoreText
8
+
9
+ def initialize(game)
10
+ @game = game
11
+ end
12
+
13
+ def preload
14
+ # nothing in here for this class
15
+ end
16
+
17
+ def create
18
+ @score = 0
19
+ @scoreText = @game.add.text(16, 16, 'score: 0', {fontSize: '32px', fill: '#000'})
20
+ end
21
+ end
22
+
23
+ class Star
24
+ attr_accessor :stars
25
+
26
+ def initialize(game)
27
+ @sprite_key = 'star'
28
+ @sprite_url = 'assets/star.png'
29
+ @game = game
30
+ end
31
+
32
+ def preload
33
+ @game.load.image(@sprite_key, @sprite_url)
34
+ end
35
+
36
+ def create
37
+ @stars = @game.add.group
38
+ stars.enable_body = true
39
+
40
+ 12.times do |n|
41
+ star = stars.create(n * 70, 0, 'star')
42
+ star.body.gravity.y = 6
43
+ star.body.bounce.y = 0.7 + rand * 0.2
44
+ end
45
+ end
46
+ end
47
+
48
+ class Sky
49
+ def initialize(game)
50
+ @sprite_key = 'sky'
51
+ @sprite_url = 'assets/sky.png'
52
+ @game = game
53
+ end
54
+
55
+ def preload
56
+ @game.load.image(@sprite_key, @sprite_url)
57
+ end
58
+
59
+ def create
60
+ @game.add.sprite(0, 0, @sprite_key)
61
+ end
62
+ end
63
+
64
+ class Platforms
65
+ attr_accessor :platforms
66
+
67
+ def initialize(game)
68
+ @sprite_key = 'ground'
69
+ @sprite_url = 'assets/platform.png'
70
+ @game = game
71
+ end
72
+
73
+ def preload
74
+ @game.load.image(@sprite_key, @sprite_url)
75
+ end
76
+
77
+ def create
78
+ @game.physics.start_system(Phaser::Physics::ARCADE)
79
+ @platforms = @game.add.group
80
+ @platforms.enable_body = true
10
81
 
11
- @phaser = Phaser::Game.new(800, 654) do |state|
12
- state.preload do |game|
13
- game.load.image('sky', 'assets/sky.png')
14
- game.load.image('ground', 'assets/platform.png')
15
- game.load.image('star', 'assets/star.png')
16
- game.load.spritesheet('dude', 'assets/dude.png', 32, 48)
17
- end
82
+ create_ground
83
+ create_ledge(400, 400)
84
+ create_ledge(-150, 250)
85
+ end
18
86
 
19
- state.create do |game|
20
- @score = 0
21
- game.physics.startSystem(Phaser::Physics::ARCADE)
22
- game.add.sprite(0, 0, 'sky')
87
+ private
23
88
 
24
- @platforms = game.add.group()
25
- @platforms.enableBody = true
89
+ def create_ground
90
+ ground = @platforms.create(0, @game.world.height - 64, @sprite_key)
91
+ ground.scale.setTo(2, 2)
92
+ ground.body.immovable = true
93
+ end
26
94
 
27
- ground = @platforms.create(0, game.world.height - 64, 'ground')
95
+ def create_ledge(x, y)
96
+ ledge = @platforms.create(x, y, @sprite_key)
97
+ ledge.body.immovable = true
98
+ end
99
+ end
28
100
 
29
- ground.scale.setTo(2, 2)
30
- ground.body.immovable = true
101
+ class Player
102
+ attr_accessor :player
31
103
 
32
- ledge = @platforms.create(400, 400, 'ground')
33
- ledge.body.immovable = true
104
+ def initialize(game)
105
+ @game = game
34
106
 
35
- ledge = @platforms.create(-150, 250, 'ground')
36
- ledge.body.immovable = true
107
+ @sprite_key = 'dude'
108
+ @sprite_url = 'assets/dude.png'
37
109
 
38
- @player = game.add.sprite(32, game.world.height - 150, 'dude', 4)
110
+ @x = 32
111
+ @y = @game.world.height - 150
112
+ end
39
113
 
40
- game.physics.arcade.enable(@player)
114
+ def preload
115
+ @game.load.spritesheet(@sprite_key, @sprite_url, 32, 48)
116
+ end
41
117
 
42
- @player.body.bounce.y = 0.2
43
- @player.body.gravity.y = 300
44
- @player.body.collideWorldBounds = true
118
+ def create
119
+ @player = @game.add.sprite(@x, @y, @sprite_key)
45
120
 
46
- @player.animations.add('left', [0, 1, 2, 3], 10, true)
47
- @player.animations.add('right', [5, 6, 7, 8], 10, true)
121
+ @game.physics.arcade.enable(@player)
48
122
 
49
- @stars = game.add.group()
50
- @stars.enableBody = true
123
+ player.body.bounce.y = 0.2
124
+ player.body.gravity.y = 300
125
+ player.body.collideWorldBounds = true
51
126
 
52
- 12.times do |i|
53
- star = @stars.create(i*70, 0, 'star')
127
+ player.animations.add('left', [0, 1, 2, 3], 10, true)
128
+ player.animations.add('right', [5, 6, 7, 8], 10, true)
129
+ end
54
130
 
55
- star.body.gravity.y = 300
56
- star.body.bounce.y = 0.7 + rand * 0.2
57
- end
131
+ def update
132
+ cursors = @game.input.keyboard.createCursorKeys
133
+ movement(cursors)
134
+ end
58
135
 
59
- @scoreText = game.add.text(16, 16, 'score: 0', { fontSize: '32px', fill: '#000' })
136
+ def movement(cursors)
137
+ player.body.velocity.x = 0
138
+
139
+ case
140
+ when cursors.left.isDown
141
+ player.body.velocity.x = -150
142
+ player.animations.play('left')
143
+ when cursors.right.isDown
144
+ player.body.velocity.x = 150
145
+ player.animations.play('right')
146
+ else
147
+ player.animations.stop
148
+ player.frame = 4
149
+ end
60
150
 
61
- @cursors = game.input.keyboard.createCursorKeys()
62
- end
151
+ if cursors.up.isDown && player.body.touching.down
152
+ player.body.velocity.y = -350
153
+ end
154
+ end
155
+ end
63
156
 
64
- state.update do |game|
65
- game.physics.arcade.collide(@player, @platforms)
66
- game.physics.arcade.collide(@stars, @platforms)
157
+ class Game
158
+ def initialize
159
+ run
160
+ end
67
161
 
68
- collectStar = proc do |player, star|
69
- %x{ star.kill() }
70
- @score += 10
71
- @scoreText.text = "Score: #@score"
72
- end
73
- game.physics.arcade.overlap(@player, @stars, collectStar, nil, self)
162
+ def run
163
+ preload
164
+ create_game
165
+ update_game
74
166
 
75
- @player.body.velocity.x = 0
167
+ Phaser::Game.new(width: 800, height: 600, renderer: Phaser::AUTO, parent: '', state: state, transparent: false, antialias: true, physics: nil)
168
+ end
76
169
 
77
- if @cursors.left.isDown
78
- @player.body.velocity.x = -150
79
- @player.animations.play('left')
80
- elsif @cursors.right.isDown
81
- @player.body.velocity.x = 150
82
- @player.animations.play('right')
83
- else
84
- @player.animations.stop
85
- @player.frame = 4
86
- end
170
+ private
87
171
 
88
- if @cursors.up.isDown && @player.body.touching.down
89
- @player.body.velocity.y = -350
90
- end
91
- end
172
+ def preload
173
+ state.preload do |game|
174
+ initialize_entities(game)
175
+ entities_call :preload
92
176
  end
93
177
  end
94
178
 
95
- def collectStar
179
+ def create_game
180
+ state.create do
181
+ entities_call :create
182
+ end
183
+ end
184
+
185
+ def update_game
186
+ collect_star = proc do |player, star, score|
187
+ star = Phaser::Sprite.new(star)
188
+ star.kill
189
+
190
+ @score.score += 10
191
+ @score.scoreText.text = "score: #{@score.score}"
192
+ end
193
+
194
+ state.update do |game|
195
+ game.physics.arcade.collide(@player.player, @platforms.platforms)
196
+ game.physics.arcade.collide(@star.stars, @platforms.platforms)
197
+ game.physics.arcade.overlap(@player.player, @star.stars, collect_star)
198
+
199
+ @player.update
200
+ end
201
+ end
202
+
203
+ def state
204
+ @state ||= Phaser::State.new
205
+ end
206
+
207
+ def initialize_entities(game)
208
+ @sky = Sky.new(game)
209
+ @platforms = Platforms.new(game)
210
+ @player = Player.new(game)
211
+ @star = Star.new(game)
212
+ @score = Score.new(game)
213
+
214
+ @entities ||= [@sky, @platforms, @player, @star, @score]
215
+ end
216
+
217
+ def entities_call(method)
218
+ @entities.each { |e| e.send(method) }
96
219
  end
97
220
  end
data/demo/config.ru CHANGED
@@ -9,6 +9,10 @@ opal = Opal::Server.new { |s|
9
9
  s.main = 'main'
10
10
  }
11
11
 
12
+ map opal.source_maps.prefix do
13
+ run opal.source_maps
14
+ end
15
+
12
16
  map '/assets' do
13
17
  run opal.sprockets
14
18
  end
data/demo/index.html CHANGED
@@ -11,7 +11,7 @@
11
11
  <body>
12
12
  <script>
13
13
  window.onload = function() {
14
- Opal.Game.$new()
14
+ opal_game = Opal.Game.$new()
15
15
  }
16
16
  </script>
17
17
  </body>
@@ -1,3 +1,13 @@
1
+ require 'native'
2
+
1
3
  require 'opal/phaser/core/state'
2
- require 'opal/phaser/core/game'
3
- require 'opal/phaser/core/physics'
4
+ require 'opal/phaser/core/physics'
5
+ require 'opal/phaser/core/group'
6
+ require 'opal/phaser/core/animation'
7
+ require 'opal/phaser/core/animation_manager'
8
+ require 'opal/phaser/core/sprite'
9
+ require 'opal/phaser/core/image'
10
+ require 'opal/phaser/core/text'
11
+ require 'opal/phaser/core/cache'
12
+ require 'opal/phaser/core/game_object_factory'
13
+ require 'opal/phaser/core/game'
@@ -0,0 +1,23 @@
1
+ module Phaser
2
+
3
+ # An Animation instance contains a single animation and the controls to play it.
4
+ # It is created by the AnimationManager, consists of Animation.Frame objects and belongs to a single Game Object such as a Sprite.
5
+ #
6
+ # @see http://docs.phaser.io/Phaser.Animation.html
7
+ class Animation
8
+ include Native
9
+ attr_accessor :native
10
+
11
+ # @param game [Phaser.Game] A reference to the currently running game.
12
+ # @param parent [Phaser.Sprite] A reference to the owner of this Animation.
13
+ # @param name [String] The unique name for this animation, used in playback commands.
14
+ # @param frame_data [Phaser.FrameData] The FrameData object that contains all frames used by this Animation.
15
+ # @param frames [Array<Integer>, Array<String>] An array of numbers or strings indicating which frames to play in which order.
16
+ # @param frame_rate [Integer] [frameRate=60] The speed at which the animation should play. The speed is given in frames per second.
17
+ # @param loop [True, False] [loop=false] Whether or not the animation is looped or just plays once.
18
+ def initialize(game, parent, name, frameData, frames, frameRate = 60, _loop = false)
19
+ @native_game = game
20
+ @native = `new Phaser.Animation(#@native_game, parent, name, frameData, frames, frameRate, _loop)`
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,9 @@
1
+ module Phaser
2
+ class AnimationManager
3
+ include Native
4
+
5
+ alias_native :add
6
+ alias_native :stop
7
+ alias_native :play
8
+ end
9
+ end
@@ -0,0 +1,5 @@
1
+ module Phaser
2
+ class Cache
3
+ include Native
4
+ end
5
+ end
@@ -0,0 +1,5 @@
1
+ module Phaser
2
+ class Frame
3
+ include Native
4
+ end
5
+ end
@@ -1,14 +1,21 @@
1
- require 'native'
2
1
  module Phaser
3
- AUTO = %x{ Phaser.AUTO }
4
- WEBGL = %x{ Phaser.WEBGL }
5
- CANVAS = %x{ Phaser.CANVAS }
2
+ AUTO = `Phaser.AUTO`
3
+ WEBGL = `Phaser.WEBGL`
4
+ CANVAS = `Phaser.CANVAS`
6
5
 
7
6
  class Game
8
- include ::Native
9
- def initialize(width, height, renderer = Phaser::AUTO,
10
- parent = '', state = nil, transparent = false, antialias = true,
11
- physics = nil, &block)
7
+ include Native
8
+
9
+ def initialize(arg_hash = {}, &block)
10
+
11
+ width = arg_hash[:width]
12
+ height = arg_hash[:height]
13
+ renderer = arg_hash[:renderer]
14
+ parent = arg_hash[:parent]
15
+ state = arg_hash[:state]
16
+ transparent = arg_hash[:transparent]
17
+ antialias = arg_hash[:antialias]
18
+ physics = arg_hash[:physics]
12
19
 
13
20
  if state
14
21
  state.game = self
@@ -20,21 +27,22 @@ module Phaser
20
27
  state.instance_eval(&block)
21
28
  end
22
29
 
23
- _native = %x{
30
+ @native = %x{
24
31
  new Phaser.Game(width, height, renderer, parent, #{state.to_n}, transparent,
25
32
  antialias, physics)
26
33
  }
27
- super(_native)
28
34
  end
29
35
 
30
- alias_native :load, :load
31
- alias_native :add, :add
32
- alias_native :world, :world
33
- alias_native :stage, :stage
34
- alias_native :physics, :physics
35
- alias_native :debug, :debug
36
- alias_native :input, :input
36
+ alias_native :cache, :cache, as: Cache
37
+ alias_native :add, :add, as: GameObjectFactory
37
38
 
39
+ alias_native :load
40
+ alias_native :world
41
+ alias_native :stage
42
+ alias_native :physics, :physics, as: Physics
43
+ alias_native :debug
44
+ alias_native :input
45
+ alias_native :width
46
+ alias_native :time
38
47
  end
39
-
40
48
  end
@@ -0,0 +1,10 @@
1
+ module Phaser
2
+ class GameObjectFactory
3
+ include Native
4
+
5
+ alias_native :sprite, :sprite, as: Sprite
6
+ alias_native :group, :group, as: Group
7
+ alias_native :text, :text, as: Text
8
+ alias_native :tile_sprite, :tileSprite
9
+ end
10
+ end
@@ -0,0 +1,18 @@
1
+ module Phaser
2
+ class Group
3
+ include Native
4
+
5
+ alias_native :cursor
6
+ alias_native :physics_body_type, :physicsBodyType
7
+ alias_native :enable_body_debug, :enableBodyDebug
8
+ alias_native :add_child, :addChild
9
+ alias_native :children
10
+ alias_native :create
11
+
12
+ alias_native :enable_body?, :enableBody
13
+
14
+ def enable_body=(bool)
15
+ `#@native.enableBody = bool`
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,10 @@
1
+ module Phaser
2
+ class Image
3
+ def initialize (game, x, y, key)
4
+ @game = game.to_n
5
+ @native = `new Phaser.Image(#{@game}, x, y, key)`
6
+ end
7
+
8
+ alias_native :scale, :scale
9
+ end
10
+ end
@@ -1,5 +1,19 @@
1
1
  module Phaser
2
- module Physics
3
- ARCADE = %x{Phaser.Physics.ARCADE}
2
+ class Physics
3
+ include Native
4
+
5
+ class Arcade
6
+ class Body
7
+ end
8
+ end
9
+
10
+ ARCADE = `Phaser.Physics.ARCADE`
11
+ P2JS = `Phaser.Physics.P2JS`
12
+ NINJA = `Phaser.Physics.NINJA`
13
+ BOX2D = `Phaser.Physics.BOX2D`
14
+
15
+ alias_native :start_system, :startSystem
16
+ alias_native :arcade
17
+
4
18
  end
5
19
  end
@@ -0,0 +1,28 @@
1
+ module Phaser
2
+ class Sprite
3
+ include Native
4
+
5
+ alias_native :anchor
6
+ alias_native :events
7
+ alias_native :scale
8
+ alias_native :body
9
+ alias_native :bounce
10
+
11
+ alias_native :kill
12
+
13
+ alias_native :visible=
14
+ alias_native :exists=
15
+ alias_native :alive=
16
+ alias_native :frame=
17
+
18
+ alias_native :z
19
+ alias_native :z=
20
+ alias_native :x
21
+ alias_native :x=
22
+ alias_native :y
23
+ alias_native :y=
24
+ alias_native :width
25
+
26
+ alias_native :animations, :animations, as: AnimationManager
27
+ end
28
+ end
@@ -3,7 +3,7 @@ module Phaser
3
3
  attr_writer :game
4
4
  def initialize(game = nil, &block)
5
5
  @game = game
6
- @native = %x{ new Phaser.State() }
6
+ @native = `new Phaser.State`
7
7
 
8
8
  @preload = proc {}
9
9
  @create = proc {}
@@ -0,0 +1,9 @@
1
+ module Phaser
2
+ class Text
3
+ include Native
4
+
5
+ def text=(text)
6
+ `#@native.text = text`
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,4 @@
1
+ module Phaser
2
+ class Tween < `Phaser.Tween`
3
+ end
4
+ end
@@ -1,5 +1,5 @@
1
1
  module Opal
2
2
  module Phaser
3
- VERSION = '0.0.4'
3
+ VERSION = '0.1.0'
4
4
  end
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: opal-phaser
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.4
4
+ version: 0.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - George Plymale
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-01-16 00:00:00.000000000 Z
11
+ date: 2015-03-11 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: opal
@@ -79,9 +79,19 @@ files:
79
79
  - lib/opal-phaser.rb
80
80
  - lib/opal/phaser.rb
81
81
  - lib/opal/phaser/core.rb
82
+ - lib/opal/phaser/core/animation.rb
83
+ - lib/opal/phaser/core/animation_manager.rb
84
+ - lib/opal/phaser/core/cache.rb
85
+ - lib/opal/phaser/core/frame.rb
82
86
  - lib/opal/phaser/core/game.rb
87
+ - lib/opal/phaser/core/game_object_factory.rb
88
+ - lib/opal/phaser/core/group.rb
89
+ - lib/opal/phaser/core/image.rb
83
90
  - lib/opal/phaser/core/physics.rb
91
+ - lib/opal/phaser/core/sprite.rb
84
92
  - lib/opal/phaser/core/state.rb
93
+ - lib/opal/phaser/core/text.rb
94
+ - lib/opal/phaser/core/tween.rb
85
95
  - lib/opal/phaser/version.rb
86
96
  - opal-phaser.gemspec
87
97
  homepage: http://github.com/orbitalimpact/opal-phaser
@@ -103,7 +113,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
103
113
  version: '0'
104
114
  requirements: []
105
115
  rubyforge_project:
106
- rubygems_version: 2.2.2
116
+ rubygems_version: 2.4.5
107
117
  signing_key:
108
118
  specification_version: 4
109
119
  summary: Opal access to phaser