├── .gitignore ├── Gemfile ├── README.md ├── Rakefile ├── common └── images │ └── logo.png ├── demo ├── Gemfile ├── README.md ├── app │ └── main.rb ├── assets │ ├── baddie.png │ ├── diamond.png │ ├── dude.png │ ├── firstaid.png │ ├── platform.png │ ├── sky.png │ └── star.png ├── config.ru ├── index.html └── style.css ├── lib ├── opal-phaser.rb └── opal │ ├── phaser.rb │ └── phaser │ ├── animation │ ├── animation.rb │ ├── animation_manager.rb │ ├── animation_parser.rb │ ├── files.rb │ ├── frame.rb │ └── frame_data.rb │ ├── core │ ├── anchor.rb │ ├── camera.rb │ ├── files.rb │ ├── game.rb │ ├── group.rb │ ├── loader.rb │ ├── math.rb │ ├── scale_manager.rb │ ├── signal.rb │ ├── stage.rb │ ├── state.rb │ ├── state_manager.rb │ └── world.rb │ ├── game_objects │ ├── bitmap_data.rb │ ├── button.rb │ ├── emitter.rb │ ├── events.rb │ ├── files.rb │ ├── filter.rb │ ├── game_object_creator.rb │ ├── game_object_factory.rb │ ├── graphics.rb │ ├── image.rb │ ├── rope.rb │ ├── sprite.rb │ ├── text.rb │ └── tile_sprite.rb │ ├── geometry │ ├── point.rb │ └── rectangle.rb │ ├── input │ ├── cursor_keys.rb │ ├── files.rb │ ├── input.rb │ ├── key.rb │ ├── keyboard.rb │ ├── mouse_event.rb │ └── pointer.rb │ ├── loader │ ├── cache.rb │ └── files.rb │ ├── math │ ├── files.rb │ └── random_data_generator.rb │ ├── native_helpers.rb │ ├── physics │ ├── files.rb │ └── physics.rb │ ├── pixi │ ├── canvas_renderer.rb │ └── web_gl_renderer.rb │ ├── setup.rb │ ├── sound │ └── sound.rb │ ├── time │ ├── files.rb │ ├── time.rb │ ├── timer.rb │ └── timer_event.rb │ ├── tween │ ├── easing.rb │ ├── files.rb │ └── tween.rb │ └── version.rb ├── opal-phaser.gemspec ├── phaser.js └── spec ├── core ├── loader_spec.rb └── math_spec.rb ├── html ├── index.html.erb └── phaser.js └── spec_helper.rb /.gitignore: -------------------------------------------------------------------------------- 1 | ## Documentation cache and generated files: 2 | /.yardoc/ 3 | /_yardoc/ 4 | /doc/ 5 | /rdoc/ 6 | /demo/.bundle 7 | 8 | ## Ignore editors, tools and such: 9 | .idea 10 | 11 | ## Ignore demo's Gemfile.lock 12 | demo/Gemfile.lock 13 | *Gemfile.lock 14 | 15 | pkg 16 | build 17 | -------------------------------------------------------------------------------- /Gemfile: -------------------------------------------------------------------------------- 1 | source 'https://rubygems.org' 2 | gemspec 3 | 4 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # opal-phaser 2 | 3 | [![Gitter](https://badges.gitter.im/Join%20Chat.svg)](https://gitter.im/orbitalimpact/opal-phaser?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge) 4 | 5 | 6 | 7 | The goal of this project is to completely wrap the Phaser API in Opal, to allow developers to write HTML5 and WebGL games entirely from Ruby code. 8 | 9 | If you have not heard of Phaser, check out these links: 10 | 11 | * Phaser: https://github.com/photonstorm/phaser 12 | * Phaser Examples: https://github.com/photonstorm/phaser-examples 13 | 14 | ## Projects built with opal-phaser 15 | 16 | ### Examples 17 | 18 | Phaser usage examples converted to Ruby: https://github.com/orbitalimpact/opal-phaser-examples. 19 | 20 | ### Games 21 | 22 | Some of the games that have been made so far with opal-phaser: 23 | 24 | * [Official demo](https://github.com/orbitalimpact/opal-phaser/tree/master/demo) 25 | * [Flutterjump](https://github.com/orbitalimpact/flutterjump) 26 | * [A pong clone](https://github.com/opengamedev/pong-opal-phaser) 27 | * [Tankgame](https://github.com/tangentstorm/tankgame) 28 | 29 | ## Cloning this repository 30 | 31 | ``` 32 | $ git clone https://github.com/orbitalimpact/opal-phaser 33 | ``` 34 | 35 | ## Getting involved 36 | 37 | To contribute to this project, follow the steps below. 38 | 39 | 1. Fork the repo ( https://github.com/orbitalimpact/opal-phaser/fork ) 40 | 2. Create your feature branch (`git checkout -b new-branch`) 41 | 3. Commit your changes (`git commit -am 'description of commit'`) 42 | 4. Push to the branch (`git push origin new-branch`) 43 | 5. Create a Pull Request 44 | 45 | ## Licenses 46 | 47 | Phaser and all examples are released under the MIT License: http://opensource.org/licenses/MIT 48 | 49 | opal-phaser is released under the Berkeley Software Distribution (BSD) 3-Clause License: http://opensource.org/licenses/BSD-3-Clause 50 | 51 | Copyright (c) 2014, Orbital Impact 52 | All rights reserved. 53 | -------------------------------------------------------------------------------- /Rakefile: -------------------------------------------------------------------------------- 1 | require 'bundler' 2 | Bundler.require 3 | Bundler::GemHelper.install_tasks 4 | 5 | require 'opal/rspec/rake_task' 6 | Opal::RSpec::RakeTask.new(:default) do |s| 7 | s.index_path = 'spec/html/index.html.erb' 8 | end 9 | 10 | desc "Build build/opal-phaser.js" 11 | task :dist do 12 | require 'fileutils' 13 | FileUtils.mkdir_p 'build' 14 | 15 | src = Opal::Builder.build('opal-phaser') 16 | min = uglify src 17 | gzp = gzip min 18 | 19 | File.open('build/opal-phaser.js', 'w+') do |out| 20 | out << src 21 | end 22 | 23 | puts "development: #{src.size}, minified: #{min.size}, gzipped: #{gzp.size}" 24 | end 25 | 26 | # Used for uglifying source to minify 27 | def uglify(str) 28 | IO.popen('uglifyjs', 'r+') do |i| 29 | i.puts str 30 | i.close_write 31 | return i.read 32 | end 33 | rescue Errno::ENOENT 34 | $stderr.puts '"uglifyjs" command not found (install with: "npm install -g uglify-js")' 35 | nil 36 | end 37 | 38 | # Gzip code to check file size 39 | def gzip(str) 40 | IO.popen('gzip -f', 'r+') do |i| 41 | i.puts str 42 | i.close_write 43 | return i.read 44 | end 45 | rescue Errno::ENOENT 46 | $stderr.puts '"gzip" command not found, it is required to produce the .gz version' 47 | nil 48 | end 49 | -------------------------------------------------------------------------------- /common/images/logo.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/orbitalimpact/opal-phaser/e8273e49c0f3698abe7dfe18a4bdf582faf498ff/common/images/logo.png -------------------------------------------------------------------------------- /demo/Gemfile: -------------------------------------------------------------------------------- 1 | #Gemfile 2 | source 'https://rubygems.org' 3 | 4 | gem 'sinatra' 5 | gem 'opal-phaser', path: '..' 6 | -------------------------------------------------------------------------------- /demo/README.md: -------------------------------------------------------------------------------- 1 | # A demo of opal-phaser 2 | This directory is a demo of opal-phaser in action (based on the tutorial here: http://phaser.io/tutorials/making-your-first-phaser-game) 3 | 4 | If you haven't yet, clone the the repository as explained in the 5 | [README](https://github.com/salama/opal-phaser) and cd into the demo directory. Then: 6 | 7 | 8 | ``` 9 | cd demo 10 | bundle 11 | rackup # you may need to prepend this with `bundle exec` 12 | ``` 13 | 14 | and go to localhost:9292 (or whatever the server says) 15 | 16 | And enjoy playing your first browser game made in ruby! 17 | -------------------------------------------------------------------------------- /demo/app/main.rb: -------------------------------------------------------------------------------- 1 | require 'opal' 2 | require 'opal-phaser' 3 | 4 | class Score 5 | attr_accessor :score 6 | attr_reader :scoreText 7 | 8 | def initialize(game) 9 | @game = game 10 | end 11 | 12 | def preload 13 | # nothing in here for this class 14 | end 15 | 16 | def create 17 | @score = 0 18 | @scoreText = @game.add.text(16, 16, 'score: 0', {fontSize: '32px', fill: '#000'}) 19 | end 20 | end 21 | 22 | class Star 23 | attr_accessor :stars 24 | 25 | def initialize(game) 26 | @sprite_key = 'star' 27 | @sprite_url = 'assets/star.png' 28 | @game = game 29 | end 30 | 31 | def preload 32 | @game.load.image(@sprite_key, @sprite_url) 33 | end 34 | 35 | def create 36 | @stars = @game.add.group 37 | stars.enable_body = true 38 | 39 | 12.times do |n| 40 | star = @game.add.sprite(n * 70, 0, 'star') 41 | @game.physics.arcade.enable(star) 42 | star.body.gravity.y = 6 43 | star.body.bounce.y = 0.7 + rand * 0.2 44 | stars.add(star) 45 | end 46 | end 47 | end 48 | 49 | class Sky 50 | def initialize(game) 51 | @sprite_key = 'sky' 52 | @sprite_url = 'assets/sky.png' 53 | @game = game 54 | end 55 | 56 | def preload 57 | @game.load.image(@sprite_key, @sprite_url) 58 | end 59 | 60 | def create 61 | @game.add.sprite(0, 0, @sprite_key) 62 | end 63 | end 64 | 65 | class Platforms 66 | attr_accessor :platforms 67 | 68 | def initialize(game) 69 | @sprite_key = 'ground' 70 | @sprite_url = 'assets/platform.png' 71 | @game = game 72 | end 73 | 74 | def preload 75 | @game.load.image(@sprite_key, @sprite_url) 76 | end 77 | 78 | def create 79 | @game.physics.start_system(Phaser::Physics::ARCADE) 80 | @platforms = @game.add.group 81 | @platforms.enable_body = true 82 | 83 | create_ground 84 | create_ledge(400, 400) 85 | create_ledge(-150, 250) 86 | end 87 | 88 | private 89 | 90 | def create_ground 91 | ground = @platforms.create(0, @game.world.height - 64, @sprite_key) 92 | ground.scale.set(2, 2) 93 | ground.body.immovable = true 94 | end 95 | 96 | def create_ledge(x, y) 97 | ledge = @platforms.create(x, y, @sprite_key) 98 | ledge.body.immovable = true 99 | end 100 | end 101 | 102 | class Player 103 | attr_accessor :player 104 | 105 | def initialize(game) 106 | @game = game 107 | 108 | @sprite_key = 'dude' 109 | @sprite_url = 'assets/dude.png' 110 | 111 | @x = 32 112 | @y = @game.world.height - 150 113 | end 114 | 115 | def preload 116 | @game.load.spritesheet(@sprite_key, @sprite_url, 32, 48) 117 | end 118 | 119 | def create 120 | @player = @game.add.sprite(@x, @y, @sprite_key) 121 | 122 | @game.physics.arcade.enable(@player) 123 | 124 | player.body.bounce.y = 0.2 125 | player.body.gravity.y = 300 126 | player.body.collide_world_bounds = true 127 | 128 | player.animations.add('left', [0, 1, 2, 3], 10, true) 129 | player.animations.add('right', [5, 6, 7, 8], 10, true) 130 | end 131 | 132 | def update 133 | cursors = @game.input.keyboard.create_cursor_keys 134 | movement(cursors) 135 | end 136 | 137 | def movement(cursors) 138 | player.body.velocity.x = 0 139 | 140 | case 141 | when cursors.left.down? 142 | player.body.velocity.x = -150 143 | player.animations.play('left') 144 | when cursors.right.down? 145 | player.body.velocity.x = 150 146 | player.animations.play('right') 147 | else 148 | player.animations.stop 149 | player.frame = 4 150 | end 151 | 152 | if cursors.up.down? && player.body.touching.down 153 | player.body.velocity.y = -350 154 | end 155 | end 156 | end 157 | 158 | class Game 159 | def initialize 160 | run 161 | end 162 | 163 | def run 164 | $game = Phaser::Game.new 165 | state = MainLevel.new($game) 166 | $game.state.add(:main, state, true) 167 | end 168 | end 169 | 170 | class MainLevel < Phaser::State 171 | def preload 172 | pp game 173 | puts "preload" 174 | initialize_entities 175 | entities_call :preload 176 | end 177 | 178 | def create 179 | puts "create" 180 | entities_call :create 181 | end 182 | 183 | def update 184 | game.physics.arcade.collide(@player.player, @platforms.platforms) 185 | game.physics.arcade.collide(@star.stars, @platforms.platforms) 186 | game.physics.arcade.overlap(@player.player, @star.stars) do |p, s| 187 | s.kill 188 | @score.score += 10 189 | @score.scoreText.text = "score: #{@score.score}" 190 | end 191 | 192 | @player.update 193 | end 194 | 195 | private 196 | 197 | def collect_star(player, star, score) 198 | star = Phaser::Sprite.new(star) 199 | star.kill 200 | 201 | @score.score += 10 202 | @score.scoreText.text = "score: #{@score.score}" 203 | end 204 | 205 | def initialize_entities 206 | @sky = Sky.new(game) 207 | @platforms = Platforms.new(game) 208 | @player = Player.new(game) 209 | @star = Star.new(game) 210 | @score = Score.new(game) 211 | 212 | @entities ||= [@sky, @platforms, @player, @star, @score] 213 | end 214 | 215 | def entities_call(method) 216 | @entities.each { |e| e.send(method) } 217 | end 218 | end 219 | -------------------------------------------------------------------------------- /demo/assets/baddie.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/orbitalimpact/opal-phaser/e8273e49c0f3698abe7dfe18a4bdf582faf498ff/demo/assets/baddie.png -------------------------------------------------------------------------------- /demo/assets/diamond.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/orbitalimpact/opal-phaser/e8273e49c0f3698abe7dfe18a4bdf582faf498ff/demo/assets/diamond.png -------------------------------------------------------------------------------- /demo/assets/dude.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/orbitalimpact/opal-phaser/e8273e49c0f3698abe7dfe18a4bdf582faf498ff/demo/assets/dude.png -------------------------------------------------------------------------------- /demo/assets/firstaid.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/orbitalimpact/opal-phaser/e8273e49c0f3698abe7dfe18a4bdf582faf498ff/demo/assets/firstaid.png -------------------------------------------------------------------------------- /demo/assets/platform.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/orbitalimpact/opal-phaser/e8273e49c0f3698abe7dfe18a4bdf582faf498ff/demo/assets/platform.png -------------------------------------------------------------------------------- /demo/assets/sky.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/orbitalimpact/opal-phaser/e8273e49c0f3698abe7dfe18a4bdf582faf498ff/demo/assets/sky.png -------------------------------------------------------------------------------- /demo/assets/star.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/orbitalimpact/opal-phaser/e8273e49c0f3698abe7dfe18a4bdf582faf498ff/demo/assets/star.png -------------------------------------------------------------------------------- /demo/config.ru: -------------------------------------------------------------------------------- 1 | # config.ru 2 | require 'bundler' 3 | Bundler.require 4 | 5 | opal = Opal::Server.new { |s| 6 | s.append_path 'app' 7 | s.append_path 'assets' 8 | 9 | s.main = 'main' 10 | } 11 | 12 | # map opal.source_maps.prefix do 13 | # run opal.source_maps 14 | # end 15 | 16 | map '/assets' do 17 | run opal.sprockets 18 | end 19 | 20 | get '/' do 21 | send_file 'index.html' 22 | end 23 | 24 | run Sinatra::Application 25 | -------------------------------------------------------------------------------- /demo/index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | Opal-Phaser 6 | 7 | 8 | 9 | 10 | 11 | 12 | 18 | 19 | -------------------------------------------------------------------------------- /demo/style.css: -------------------------------------------------------------------------------- 1 | body { 2 | margin: 0; 3 | padding: 0; 4 | background-color: #000000; 5 | } -------------------------------------------------------------------------------- /lib/opal-phaser.rb: -------------------------------------------------------------------------------- 1 | require 'opal/phaser' 2 | -------------------------------------------------------------------------------- /lib/opal/phaser.rb: -------------------------------------------------------------------------------- 1 | require 'opal-pixi' 2 | if RUBY_ENGINE == 'opal' 3 | require 'opal/phaser/setup' 4 | 5 | else 6 | require 'opal' 7 | require 'opal/phaser/version' 8 | 9 | Opal.append_path File.expand_path('../..', __FILE__).untaint 10 | end 11 | -------------------------------------------------------------------------------- /lib/opal/phaser/animation/animation.rb: -------------------------------------------------------------------------------- 1 | require 'opal/phaser/native_helpers' 2 | 3 | module Phaser 4 | # The base Animation object that the Animation Manager creates. 5 | class Animation 6 | include Native 7 | 8 | # @!method initialize(game, parent, name, frame_data = 60, frames, frame_rate, loop = false) 9 | # An Animation instance contains a single animation and the controls to play it. 10 | # It is created by the AnimationManager, consists of Animation.Frame objects and belongs to a single Game Object such as a Sprite. 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, Array] An array of numbers or strings indicating which frames to play in which order. 16 | # @param frame_rate [Number] The speed at which the animation should play. The speed is given in frames per second. 17 | # @param loop [Boolean] Whether or not the animation is looped or just plays once. 18 | # @return [Animation] 19 | 20 | # @!attribute game 21 | # @return [Phaser.Game] A reference to the currently running Game. 22 | native_accessor :game 23 | 24 | # @!attribute _parent 25 | # @return [Phaser.Sprite] A reference to the parent Sprite that owns this Animation. 26 | # @!visibility private 27 | native_accessor :_parent 28 | 29 | # @!attribute _frame_data 30 | # @return [Phaser.FrameData] The FrameData the Animation uses. 31 | # @!visibility private 32 | native_accessor_alias :_frame_data, :_frameData 33 | 34 | # @!attribute name 35 | # @return [String] The user defined name given to this Animation. 36 | native_accessor :name 37 | 38 | # @!attribute _frames 39 | # @return [array] 40 | # @!visibility private 41 | native_accessor :_frames 42 | 43 | # @!attribute delay 44 | # @return [Number] The delay in ms between each frame of the Animation, based on the given frame_rate. 45 | native_accessor :delay 46 | 47 | # @!attribute loop 48 | # @return [Boolean] The loop state of the Animation. 49 | native_accessor :loop 50 | 51 | # @!attribute loop_count 52 | # @return [Number] The number of times the Animation has looped since it was last started. 53 | native_accessor_alias :loop_count, :loopCount 54 | 55 | # @!attribute kill_on_complete 56 | # @return [Boolean] Should the parent of this Animation be killed when the animation completes? 57 | native_accessor_alias :kill_on_complete, :killOnComplete 58 | 59 | # @!attribute is_finished 60 | # @return [Boolean] The finished state of the Animation. Set to true once playback completes, false during playback. 61 | native_accessor_alias :is_finished, :isFinished 62 | 63 | # @!attribute is_playing 64 | # @return [Boolean] The playing state of the Animation. Set to false once playback completes, true during playback. 65 | native_accessor_alias :is_playing, :isPlaying 66 | 67 | # @!attribute [r] is_playing? 68 | # @return [Boolean] The playing state of the Animation. Set to false once playback completes, true during playback. 69 | # legacy 70 | # @see is_playing 71 | native_reader_alias :is_playing?, :isPlaying 72 | 73 | # @!attribute is_paused 74 | # @return [Boolean] The paused state of the Animation. 75 | native_accessor_alias :is_paused, :isPaused 76 | 77 | # @!attribute _pause_start_time 78 | # @return [Boolean] The time the Animation paused. 79 | # @!visibility private 80 | native_accessor_alias :_pause_start_time, :_pauseStartTime 81 | 82 | # @!attribute _frame_index 83 | # @return [Number] 84 | # @!visibility private 85 | native_accessor_alias :_frame_index, :_frameIndex 86 | 87 | # @!attribute _frame_diff 88 | # @return [Number] 89 | # @!visibility private 90 | native_accessor_alias :_frame_diff, :_frameDiff 91 | 92 | # @!attribute _frame_skip 93 | # @return [Number] 94 | # @!visibility private 95 | native_accessor_alias :frame_skip, :_frameSkip 96 | 97 | # @!attribute current_frame 98 | # @return [Phaser.Frame] The currently displayed frame of the Animation. 99 | native_accessor_alias :current_frame, :currentFrame 100 | 101 | # @!attribute on_start 102 | # @return [Phaser.Signal] This event is dispatched when this Animation starts playback. 103 | native_accessor_alias :on_start, :onStart 104 | 105 | # @!attribute on_update 106 | # This event is dispatched when the Animation changes frame. 107 | # By default this event is disabled due to its intensive nature. Enable it with: `Animation.enable_update = true`. 108 | # @return [Phaser.Signal, null] 109 | native_accessor_alias :on_update, :onUpdate 110 | 111 | # @!attribute on_complete 112 | # @return [Phaser.Signal] This event is dispatched when this Animation completes playback. If the Animation is set to loop this is never fired, listen for on_loop instead. 113 | native_accessor_alias :on_complete, :onComplete 114 | 115 | # @!method on_completion 116 | # @see on_complete 117 | # legacy 118 | native_accessor_alias :on_completion, :onComplete 119 | 120 | # @!attribute on_loop 121 | # @return [Phaser.Signal] This event is dispatched when this Animation loops. 122 | native_accessor_alias :on_loop, :onLoop 123 | 124 | ### 125 | # methods 126 | ######### 127 | 128 | # @!method play(frame_rate = nil, loop = false, kill_on_complete = false) 129 | # Plays the Animation. 130 | # @param frame_rate [Number] The framerate to play the animation at. The speed is given in frames per second. If not provided the previously set frame_rate of the Animation is used. 131 | # @param loop [Boolean] Should the animation be looped after playback. If not provided the previously set loop value of the Animation is used. 132 | # @param kill_on_complete [Boolean] If set to true when the animation completes (only happens if loop=false) the parent Sprite will be killed. 133 | # @return [Phaser.Animation] A reference to this Animation instance. 134 | alias_native :play 135 | 136 | # @!method restart 137 | # Sets this animation back to the first frame and restarts the animation. 138 | alias_native :restart 139 | 140 | # @!method set_frame(frame_id = 'nil', use_local_frame_index = false) 141 | # Sets this animations playback to a given frame with the given ID. 142 | # @param frame_id [String, Number] The identifier of the frame to set. Can be the name of the frame, the sprite index of the frame, or the animation-local frame index. 143 | # @param use_local_frame_index [Boolean] If you provide a number for frame_id, should it use the numeric indexes of the frame_data, or the 0-indexed frame index local to the animation. 144 | alias_native :set_frame, :setFrame 145 | 146 | # @!method stop(reset_frame = false, dispatch_complete = false) 147 | # Stops playback of this animation and set it to a finished state. If a resetFrame is provided it will stop playback and set frame to the first in the animation. 148 | # If dispatch_complete is true it will dispatch the complete events, otherwise they'll be ignored. 149 | # @param reset_frame [Boolean] If true after the animation stops the currentFrame value will be set to the first frame in this animation. 150 | # @param dispatch_complete [Boolean] Dispatch the Animation.onComplete and parent.onAnimationComplete events? 151 | alias_native :stop 152 | 153 | # @!method on_pause 154 | # Called when the Game enters a paused state. 155 | alias_native :on_pause, :onPause 156 | 157 | # @!method on_resume 158 | # Called when the Game resumes from a paused state. 159 | alias_native :on_resume, :onResume 160 | 161 | # @!method update 162 | # Updates this animation. Called automatically by the AnimationManager. 163 | alias_native :update 164 | 165 | # @!method update_current_frame(signal_update, from_play) 166 | # Changes the current_frame per the _frame_index, updates the display state, 167 | # and triggers the update signal. 168 | # 169 | # Returns true if the current frame update was 'successful', false otherwise. 170 | # @!visibility private 171 | # @param [Boolean] signal_update - If true the `Animation.onUpdate` signal will be dispatched. 172 | # @param [Boolean] from_play - Was this call made from the playing of a new animation? 173 | # @return [Boolean] True if the current frame was updated, otherwise false. 174 | alias_native :update_current_frame, :updateCurrentFrame 175 | 176 | # @!method next(quantity = 1) 177 | # Advances by the given number of frames in the Animation, taking the loop value into consideration. 178 | # @param quantity [Number] The number of frames to advance. 179 | alias_native :next 180 | 181 | # @!method previous(quantity = 1) 182 | # Moves backwards the given number of frames in the Animation, taking the loop value into consideration. 183 | # @param quantity [Number] The number of frames to move back. 184 | alias_native :previous 185 | 186 | # @!method update_frame_data(frame_data) 187 | # Changes the FrameData object this Animation is using. 188 | # @param frame_data [Phaser.FrameData] The FrameData object that contains all frames used by this Animation. 189 | alias_native :update_frame_data, :updateFrameData 190 | 191 | # @!method destroy 192 | # Cleans up this animation ready for deletion. Nulls all values and references. 193 | alias_native :destroy 194 | 195 | # @!method complete 196 | # Called internally when the animation finishes playback. 197 | # Sets the is_playing and is_finished states and dispatches the on_animation_complete event if it exists on the parent and local on_complete event. 198 | alias_native :complete 199 | 200 | ## 201 | # Prototype constructor 202 | ## 203 | 204 | # @!attribute paused 205 | # @return [Boolean] Gets and sets the paused state of this Animation. 206 | native_accessor :paused 207 | 208 | # @!attribute [r] frame_total 209 | # @return [Number] The total number of frames in the currently loaded FrameData, or -1 if no FrameData is loaded. 210 | native_reader_alias :frame_total, :frame_Total 211 | 212 | # @!attribute frame 213 | # @return [Number] Gets or sets the current frame index and updates the Texture Cache for display. 214 | native_accessor :frame 215 | 216 | # @!attribute speed 217 | # @return [Number] Gets or sets the current speed of the animation in frames per second. Changing this in a playing animation will take effect from the next frame. Minimum value is 1. 218 | native_accessor :speed 219 | 220 | # @!attribute enable_update 221 | # @return [Boolean] Gets or sets if this animation will dispatch the on_update events upon changing frame. 222 | native_accessor_alias :enable_update, :enableUpdate 223 | 224 | ## 225 | # Helper methods 226 | ## 227 | 228 | # @!method self.generate_frame_names(prefix, start, stop, suffix='', zero_pad = 0) 229 | # Really handy function for when you are creating arrays of animation data but it's using frame names and not numbers. 230 | # For example imagine you've got 30 frames named: 'explosion_0001-large' to 'explosion_0030-large' 231 | # You could use this function to generate those by doing: Phaser.Animation.generateFrameNames('explosion_', 1, 30, '-large', 4); 232 | # @param prefix [String] The start of the filename. If the filename was 'explosion_0001-large' the prefix would be 'explosion_'. 233 | # @param start [Number] The number to start sequentially counting from. If your frames are named 'explosion_0001' to 'explosion_0034' the start is 1. 234 | # @param stop [Number] The number to count to. If your frames are named 'explosion_0001' to 'explosion_0034' the stop value is 34. 235 | # @param suffix [String] The end of the filename. If the filename was 'explosion_0001-large' the prefix would be '-large'. 236 | # @param zero_pad [Number] The number of zeros to pad the min and max values with. If your frames are named 'explosion_0001' to 'explosion_0034' then the zeroPad is 4. 237 | # @return [Array] An array of frame names. 238 | def self.generate_frame_names(args = {}) 239 | optional_args = { suffix: '', zeros_padding: 0 } 240 | 241 | optional_args.each do |optional_arg, default_value| 242 | args[optional_arg] = default_value unless args.include?(optional_arg) 243 | end 244 | 245 | `Phaser.Animation.generateFrameNames(#{args[:prefix]}, #{args[:start_num]}, #{args[:stop_num]}, #{args[:suffix]}, #{args[:zeros_padding]})` 246 | end 247 | end 248 | end 249 | -------------------------------------------------------------------------------- /lib/opal/phaser/animation/animation_manager.rb: -------------------------------------------------------------------------------- 1 | require 'opal/phaser/native_helpers' 2 | require 'opal/phaser/animation/animation' 3 | 4 | module Phaser 5 | # Adds, plays and updates animations on Sprite Game Objects. 6 | class AnimationManager 7 | include Native 8 | 9 | # @!method initalize(sprite) 10 | # The Animation Manager is used to add, play and update Phaser Animations. 11 | # Any Game Object such as Phaser.Sprite that supports animation contains a single AnimationManager instance. 12 | # @param [Phaser.Sprite] sprite - A reference to the Game Object that owns this AnimationManager. 13 | 14 | # @!attribute sprite 15 | # @return [Phaser.Sprite] A reference to the parent Sprite that owns this AnimationManager. 16 | native_accessor :sprite 17 | 18 | # @!attribute game 19 | # @return [Phaser.Game] A reference to the currently running Game. 20 | native_accessor :game 21 | 22 | # @!attribute current_frame 23 | # This property is only set once an Animation starts playing. Until that point it remains set as `null`. 24 | # @return [Phaser.Frame] The currently displayed Frame of animation, if any. 25 | native_accessor_alias :current_frame, :currentFrame 26 | 27 | # @!attribute current_anim 28 | # @return [Phaser.Animation] The currently displayed animation, if any. 29 | native_accessor_alias :current_anim, :currentAnim 30 | 31 | # @!attribute update_if_visible 32 | # @return [Boolean] Should the animation data continue to update even if the Sprite.visible is set to false. 33 | native_accessor_alias :update_if_visible, :updateIfVisible 34 | 35 | # @!attribute is_loaded 36 | # @return [Boolean] Set to true once animation data has been loaded. 37 | native_accessor_alias :is_loaded, :isLoaded 38 | 39 | # @!attribute _frame_data 40 | # @return [Phaser.FrameData] A temporary variable for holding the currently playing Animations FrameData. 41 | # @!visibility private 42 | native_accessor_alias :_frame_data, :frameData 43 | 44 | # @!attribute _anims 45 | # @return [Object] An internal object that stores all of the Animation instances. 46 | # @!visibility private 47 | native_accessor :_anims 48 | 49 | # @!attribute _outputFrames 50 | # @return [Object] An internal object to help avoid garbage collection. 51 | # @!visibility private 52 | native_accessor_alias :_output_frames, :_outputFrames 53 | 54 | ### 55 | # methods 56 | ######### 57 | 58 | # @!method load_frame_data(frame_data, frame) 59 | # Loads FrameData into the internal temporary vars and resets the frame index to zero. 60 | # This is called automatically when a new Sprite is created. 61 | # @!visibility private 62 | # @param frame_data [Phaser.FrameData] The FrameData set to load. 63 | # @param frame [String, Number] The frame to default to. 64 | # @return [Boolean] Returns `true` if the frame data was loaded successfully, otherwise `false` 65 | alias_native :load_frame_data, :loadFrameData 66 | 67 | # @!method copy_frame_data(frame_data, frame) 68 | # Loads FrameData into the internal temporary vars and resets the frame index to zero. 69 | # This is called automatically when a new Sprite is created. 70 | # @!visibility private 71 | # @param frame_data[Phaser.FrameData] The FrameData set to load. 72 | # @param frame [String, Number] The frame to default to. 73 | # @return [Boolean] Returns `true` if the frame data was loaded successfully, otherwise `false` 74 | alias_native :copy_frame_data, :copyFrameData 75 | 76 | # @!method add(name, frames = nil, frame_rate = 60, loop = false, use_numeric_index = true) 77 | # Adds a new animation under the given key. Optionally set the frames, frame rate and loop. 78 | # Animations added in this way are played back with the play function. 79 | # 80 | # @param name [String] name - The unique (within this Sprite) name for the animation, i.e. "run", "fire", "walk". 81 | # @param frames [Array] An array of numbers/strings that correspond to the frames to add to this animation and in which order. e.g. [1, 2, 3] or ['run0', 'run1', run2]). If null then all frames will be used. 82 | # @param frame_rate [Number] The speed at which the animation should play. The speed is given in frames per second. 83 | # @param loop [Boolean] Whether or not the animation is looped or just plays once. 84 | # @param use_numeric_index [Boolean] Are the given frames using numeric indexes (default) or strings? 85 | # @return [Phaser.Animation] The Animation object that was created. 86 | alias_native :add, :add, as: Animation 87 | 88 | # @!method validate_frames(frames, use_numeric_index = true) 89 | # Check whether the frames in the given array are valid and exist. 90 | # @param frames [Array] An array of frames to be validated. 91 | # @param use_numeric_index [Boolean] Validate the frames based on their numeric index (true) or string index (false) 92 | # @return [Boolean] True if all given Frames are valid, otherwise false. 93 | alias_native :validate_frames, :validateFrames 94 | 95 | # @!method play(name, frame_rate = null, loop = false, kill_on_complete = false) 96 | # Play an animation based on the given key. The animation should previously have been added via `animations.add` 97 | # If the requested animation is already playing this request will be ignored. 98 | # If you need to reset an already running animation do so directly on the Animation object itself. 99 | # @param name [String] The name of the animation to be played, e.g. "fire", "walk", "jump". 100 | # @param frame_rate [Number] The framerate to play the animation at. The speed is given in frames per second. If not provided the previously set frame_rate of the Animation is used. 101 | # @param loop [Boolean] Should the animation be looped after playback. If not provided the previously set loop value of the Animation is used. 102 | # @param kill_on_complete [Boolean] If set to true when the animation completes (only happens if loop=false) the parent Sprite will be killed. 103 | # @return [Phaser.Animation] A reference to playing Animation instance. 104 | alias_native :play, :play, as: Animation 105 | 106 | # @!method stop(name = nil, reset_frame = false) 107 | # Stop playback of an animation. If a name is given that specific animation is stopped, otherwise the current animation is stopped. 108 | # The currentAnim property of the AnimationManager is automatically set to the animation given. 109 | # @param name [String] The name of the animation to be stopped, e.g. "fire". If none is given the currently running animation is stopped. 110 | # @param reset_frame [Boolean] When the animation is stopped should the currentFrame be set to the first frame of the animation (true) or paused on the last frame displayed (false) 111 | alias_native :stop 112 | 113 | # @!method update 114 | # The main update function is called by the Sprites update loop. It's responsible for updating animation frames and firing related events. 115 | # @!visibility protected 116 | # @return [Boolean] True if a new animation frame has been set, otherwise false. 117 | alias_native :update 118 | 119 | # @!method next(quantity = 1) 120 | # Advances by the given number of frames in the current animation, taking the loop value into consideration. 121 | # @param quantity [Number] The number of frames to advance. 122 | alias_native :quantity 123 | 124 | # @!method previous(quantity = 1) 125 | # Moves backwards the given number of frames in the current animation, taking the loop value into consideration. 126 | # @param quantity [Number] The number of frames to move back. 127 | alias_native :previous 128 | 129 | # @!method get_animation(name) 130 | # Returns an animation that was previously added by name. 131 | # @param [String] name - The name of the animation to be returned, e.g. "fire". 132 | # @return [Phaser.Animation] The Animation instance, if found, otherwise null. 133 | alias_native :get_animation, :get_animation, as: Animation 134 | 135 | # @!method refresh_frame 136 | # Refreshes the current frame data back to the parent Sprite and also resets the texture data. 137 | alias_native :refresh_frame, :refreshFrame 138 | 139 | # @!method destroy 140 | # Destroys all references this AnimationManager contains. 141 | # Iterates through the list of animations stored in this manager and calls destroy on each of them. 142 | alias_native :destroy 143 | 144 | ### 145 | # constructor properties 146 | ######################## 147 | 148 | # @!attribute [r] frame_data 149 | # @return The current animations FrameData. 150 | native_reader_alias :frame_data, :frameData 151 | 152 | # @!attribute [r] frame_total 153 | # @return [Number] The total number of frames in the currently loaded FrameData, or -1 if no FrameData is loaded. 154 | native_reader_alias :frame_total, :frameTotal 155 | 156 | # @!attribute paused 157 | # @return [Boolean] Gets and sets the paused state of the current animation. 158 | native_accessor :paused 159 | 160 | # @!attribute name 161 | # @return [String] Gets the current animation name, if set. 162 | native_accessor :name 163 | 164 | # @!attribute frame 165 | # @return [Number] Gets or sets the current frame index and updates the Texture Cache for display. 166 | native_accessor :frame 167 | 168 | # @!attribute frame_name 169 | # @return [String] frameName - Gets or sets the current frame name and updates the Texture Cache for display. 170 | native_accessor_alias :frame_name, :frameName 171 | end 172 | end 173 | -------------------------------------------------------------------------------- /lib/opal/phaser/animation/animation_parser.rb: -------------------------------------------------------------------------------- 1 | require 'opal/phaser/native_helpers' 2 | require 'opal/phaser/animation/frame_data' 3 | 4 | module Phaser 5 | # Responsible for parsing sprite sheet and JSON data into the internal FrameData format that Phaser uses for animations. 6 | class AnimationParser 7 | include Native 8 | 9 | # Responsible for parsing sprite sheet and JSON data into the internal FrameData format that Phaser uses for animations. 10 | # @!method intialize 11 | 12 | # @!method sprite_sheet(game, key, frame_width, frame_height, frame_max = -1, margin = 0, spacing = 0) 13 | # Parse a Sprite Sheet and extract the animation frame data from it. 14 | # @param game [Phaser.Game] A reference to the currently running game. 15 | # @param key [String, Image] The Game.Cache asset key of the Sprite Sheet image or an actual HTML Image element. 16 | # @param frame_width [Number] The fixed width of each frame of the animation. 17 | # @param frame_height [Number] The fixed height of each frame of the animation. 18 | # @param frame_max [Number] The total number of animation frames to extract from the Sprite Sheet. The default value of -1 means "extract all frames". 19 | # @param margin [Number] If the frames have been drawn with a margin, specify the amount here. 20 | # @param spacing [Number] If the frames have been drawn with spacing between them, specify the amount here. 21 | # @return [Phaser.FrameData] A FrameData object containing the parsed frames. 22 | alias_native :sprite_sheet, :spriteSheet, as: FrameData 23 | 24 | # @!method json_data(game, json) 25 | # Parse the JSON data and extract the animation frame data from it. 26 | # @param game [Phaser.Game] A reference to the currently running game. 27 | # @param json [Object] The JSON data from the Texture Atlas. Must be in Array format. 28 | # @return [Phaser.FrameData] A FrameData object containing the parsed frames. 29 | alias_native :json_data, :JSONData, as: FrameData 30 | 31 | # @!method json_data_pyxel(game, json) 32 | # Parse the JSON data and extract the animation frame data from it. 33 | # @param game [Phaser.Game] A reference to the currently running game. 34 | # @param json [Object] The JSON data from the Texture Atlas. Must be in Pyxel JSON format. 35 | # @return [Phaser.FrameData] A FrameData object containing the parsed frames. 36 | alias_native :json_data_pyxel, :JSONDataPyxel, as: FrameData 37 | 38 | # @!method json_data_hash(game, json) 39 | # Parse the JSON data and extract the animation frame data from it. 40 | # @param game [Phaser.Game] A reference to the currently running game. 41 | # @param json [Object] The JSON data from the Texture Atlas. Must be in JSON Hash format. 42 | # @return [Phaser.FrameData] A FrameData object containing the parsed frames. 43 | alias_native :json_data_hash, :JSONDataHash, as: FrameData 44 | 45 | # @!method xml_data(game, xml) 46 | # Parse the XML data and extract the animation frame data from it. 47 | # @param game [Phaser.Game] A reference to the currently running game. 48 | # @param xml [Object] The XML data from the Texture Atlas. Must be in Starling XML format. 49 | # @return [Phaser.FrameData] A FrameData object containing the parsed frames. 50 | alias_native :xml_data, :XMLData, as: FrameData 51 | end 52 | end 53 | -------------------------------------------------------------------------------- /lib/opal/phaser/animation/files.rb: -------------------------------------------------------------------------------- 1 | # These requires must be loaded in order of dependency: 2 | require 'opal/phaser/animation/animation' 3 | require 'opal/phaser/animation/animation_manager' 4 | require 'opal/phaser/animation/frame' 5 | require 'opal/phaser/animation/frame_data' 6 | require 'opal/phaser/animation/animation_parser' 7 | -------------------------------------------------------------------------------- /lib/opal/phaser/animation/frame.rb: -------------------------------------------------------------------------------- 1 | require 'opal/phaser/native_helpers' 2 | require 'opal/phaser/geometry/rectangle' 3 | 4 | module Phaser 5 | class Frame 6 | include Native 7 | 8 | # A Frame is a single frame of an animation and is part of a FrameData collection. 9 | # @param index [Number] The index of this Frame within the FrameData set it is being added to. 10 | # @param x [Number] X position of the frame within the texture image. 11 | # @param y [Number] Y position of the frame within the texture image. 12 | # @param width [Number] Width of the frame within the texture image. 13 | # @param height [Number] Height of the frame within the texture image. 14 | # @param name [String] The name of the frame. In Texture Atlas data this is usually set to the filename. 15 | def initalize(index, x, y, width, height, name) 16 | super `new Phaser.Frame(index, x, width, height, name)` 17 | end 18 | 19 | ### 20 | # properties 21 | ############ 22 | 23 | # @!attribute index 24 | # @return [Number] The index of this Frame within the FrameData set it is being added to. 25 | native_accessor :index 26 | 27 | # @!attribute x 28 | # @return [Number] X position within the image to cut from. 29 | native_accessor :x 30 | 31 | # @!attribute y 32 | # @return [Number] Y position within the image to cut from. 33 | native_accessor :y 34 | 35 | # @!attribute width 36 | # @return [Number] Width of the frame. 37 | native_accessor :width 38 | 39 | # @!attribute height 40 | # @return [Number] Height of the frame. 41 | native_accessor :height 42 | 43 | # @!attribute name 44 | # @return [String] Useful for Texture Atlas files (is set to the filename value). 45 | native_accessor :name 46 | 47 | # @!attribute center_x 48 | # @return [Number] Center X position within the image to cut from. 49 | native_accessor_alias :center_y, :centerX 50 | 51 | # @!attribute center_y 52 | # @return [Number] Center Y position within the image to cut from. 53 | native_accessor_alias :center_y, :centerY 54 | 55 | # @!attribute distance 56 | # @return [Number] The distance from the top left to the bottom-right of this Frame. 57 | native_accessor :distance 58 | 59 | # @!attribute rotated 60 | # @return [Boolean] Rotated? (not yet implemented) 61 | native_accessor :rotated 62 | 63 | # @!attribute rotation_directon 64 | # @return [String] Either 'cw' or 'ccw', rotation is always 90 degrees. 65 | native_accessor_alias :rotation_direction, :rotationDirection 66 | 67 | # @!attribute trimmed 68 | # @return {boolean} Was it trimmed when packed? 69 | native_accessor :trimmed 70 | 71 | # @!attribute source_size_w 72 | # @return [Number] Width of the original sprite before it was trimmed. 73 | native_accessor_alias :source_size_w, :sourceSizeW 74 | 75 | # @!attribute source_size_h 76 | # @return [Number] Height of the original sprite before it was trimmed. 77 | native_accessor_alias :source_size_h, :sourceSizeH 78 | 79 | # @!attribute sprite_source_size_x 80 | # @return [Number] X position of the trimmed sprite inside original sprite. 81 | native_accessor_alias :sprite_source_size_x, :spriteSourceSizeX 82 | 83 | # @!attribute sprite_source_size_y 84 | # @return [Number] Y position of the trimmed sprite inside original sprite. 85 | native_accessor_alias :sprite_source_size_y, :spriteSourceSizeY 86 | 87 | # @!attribute sprite_source_size_w 88 | # @return [Number] Width of the trimmed sprite. 89 | native_accessor_alias :sprite_source_size_w, :spriteSourceSizeW 90 | 91 | # @!attribute sprite_source_size_h 92 | # @return [Number] Height of the trimmed sprite. 93 | native_accessor_alias :sprite_source_size_h, :spriteSourceSizeH 94 | 95 | # @!attribute right 96 | # @return [Number] The right of the Frame (x + width). 97 | native_accessor_alias :right #= this.x + this.width; 98 | 99 | # @!attribute bottom 100 | # @return [Number] The bottom of the frame (y + height). 101 | native_accessor :bottom 102 | 103 | ### 104 | # methods 105 | ######### 106 | 107 | # @!method resize(width, height) 108 | # Adjusts of all the Frame properties based on the given width and height values. 109 | # @param {integer} width - The new width of the Frame. 110 | # @param {integer} height - The new height of the Frame. 111 | alias_native :resize 112 | 113 | # @!method set_trim(trimmed, actual_width, actual_height, dest_x, dest_y, dest_width, dest_height) 114 | # If the frame was trimmed when added to the Texture Atlas this records the trim and source data. 115 | # @param trimmed [Boolean] If this frame was trimmed or not. 116 | # @param actual_width [Number] The width of the frame before being trimmed. 117 | # @param actual_height [Number] The height of the frame before being trimmed. 118 | # @param dest_x [Number] The destination X position of the trimmed frame for display. 119 | # @param dest_y [Number] The destination Y position of the trimmed frame for display. 120 | # @param dest_width [Number] The destination width of the trimmed frame for display. 121 | # @param dest_height [Number] The destination height of the trimmed frame for display. 122 | alias_native :set_trim, :setTrim 123 | 124 | # @!method clone 125 | # Clones this Frame into a new Phaser.Frame object and returns it. 126 | # Note that all properties are cloned, including the name, index and UUID. 127 | # @return {Phaser.Frame} An exact copy of this Frame object. 128 | alias_native :clone 129 | 130 | # @!method rectangle(out) 131 | # Returns a Rectangle set to the dimensions of this Frame. 132 | # @param out [Phaser.Rectangle] A rectangle to copy the frame dimensions to. 133 | # @return [Phaser.Rectangle] A rectangle. 134 | alias_native :get_rectangle, :getRect, as: Rectangle 135 | end 136 | end 137 | -------------------------------------------------------------------------------- /lib/opal/phaser/animation/frame_data.rb: -------------------------------------------------------------------------------- 1 | require 'opal/phaser/native_helpers' 2 | require 'opal/phaser/animation/frame' 3 | 4 | module Phaser 5 | class FrameData 6 | include Native 7 | 8 | # @!method initialize 9 | # FrameData is a container for Frame objects, which are the internal representation of animation data in Phaser. 10 | 11 | # @!attribute _frames 12 | # @return [Array] Local array of frames. 13 | # @!visibility private 14 | native_accessor :_frames 15 | 16 | # @!attribute _frame_names 17 | # @return [Array] Local array of frame names for name to index conversions. 18 | # @!visibility private 19 | native_accessor_alias :_frame_names, :_frameNames 20 | 21 | ## 22 | # methods 23 | ######### 24 | 25 | # @!method add_frame(frame) 26 | # Adds a new Frame to this FrameData collection. Typically called by the Animation.Parser and not directly. 27 | # @param frame [Phaser.Frame] The frame to add to this FrameData set. 28 | # @return [Phaser.Frame] The frame that was just added. 29 | alias_native :add_frame, :addFrame, as: Frame 30 | 31 | # @!method get_frame(index) 32 | # Get a Frame by its numerical index. 33 | # @param index [Number] The index of the frame you want to get. 34 | # @return [Phaser.Frame] The frame, if found. 35 | alias_native :get_frame, :getFrame, as: Frame 36 | 37 | # @!method get_frame_by_name(name) 38 | # Get a Frame by its frame name. 39 | # @param name [String] The name of the frame you want to get. 40 | # @return [Phaser.Frame] The frame, if found. 41 | alias_native :get_frame_by_name, :getFrameByName, as: Frame 42 | 43 | # @!method check_frame_name(name) 44 | # Check if there is a Frame with the given name. 45 | # @param name [String] The name of the frame you want to check. 46 | # @return [Boolean] True if the frame is found, otherwise false. 47 | alias_native :check_frame_name, :checkFrameName 48 | 49 | # @!method clone 50 | # Makes a copy of this FrameData including copies (not references) to all of the Frames it contains. 51 | # @return [Phaser.FrameData] A clone of this object, including clones of the Frame objects it contains. 52 | native_accessor :clone 53 | 54 | # @!method get_frame_range(start, end, output) 55 | # Returns a range of frames based on the given start and end frame indexes and returns them in an Array. 56 | # @param start [Number] The starting frame index. 57 | # @param end [Number] The ending frame index. 58 | # @param output [Array] If given the results will be appended to the end of this array otherwise a new array will be created. 59 | # @return [Array] An array of Frames between the start and end index values, or an empty array if none were found. 60 | native_accessor :get_frame_range, :FrameRange 61 | 62 | # @!method get_frames(frames, use_numeric_index = true, output) 63 | # Returns all of the Frames in this FrameData set where the frame index is found in the input array. 64 | # The frames are returned in the output array, or if none is provided in a new Array object. 65 | # @param frames [Array] An Array containing the indexes of the frames to retrieve. If the array is empty or undefined then all frames in the FrameData are returned. 66 | # @param use_numeric_index [Boolean] Are the given frames using numeric indexes (default) or strings? (false) 67 | # @param output [Array] If given the results will be appended to the end of this array otherwise a new array will be created. 68 | # @return [Array] An array of all Frames in this FrameData set matching the given names or IDs. 69 | alias_native :get_frames, :getFrames 70 | 71 | # @!method get_frame_indexes(frames, use_numberic_index = true, output) 72 | # Returns all of the Frame indexes in this FrameData set. 73 | # The frames indexes are returned in the output array, or if none is provided in a new Array object. 74 | # @param frames [Array] An Array containing the indexes of the frames to retrieve. If undefined or the array is empty then all frames in the FrameData are returned. 75 | # @param use_numberic_index [Boolean] Are the given frames using numeric indexes (default) or strings? (false) 76 | # @param output [Array] If given the results will be appended to the end of this array otherwise a new array will be created. 77 | # @return [Array] An array of all Frame indexes matching the given names or IDs. 78 | alias_native :get_frame_indexes, :getFrameIndexes 79 | 80 | # @!attribute [r] total 81 | # @return [Number] The total number of frames in this FrameData set. 82 | native_reader :total 83 | end 84 | end 85 | -------------------------------------------------------------------------------- /lib/opal/phaser/core/anchor.rb: -------------------------------------------------------------------------------- 1 | module Phaser 2 | class Anchor 3 | include Native 4 | 5 | alias_native :set_to, :setTo 6 | alias_native :set 7 | end 8 | end 9 | -------------------------------------------------------------------------------- /lib/opal/phaser/core/camera.rb: -------------------------------------------------------------------------------- 1 | module Phaser 2 | class Camera 3 | include Native 4 | 5 | alias_native :x 6 | alias_native :x= 7 | alias_native :y 8 | alias_native :y= 9 | 10 | alias_native :follow 11 | alias_native :deadzone, as: Phaser::Rectangle 12 | def deadzone=(zone) 13 | `#@native.deadzone = #{zone.to_n}` 14 | end 15 | end 16 | end 17 | -------------------------------------------------------------------------------- /lib/opal/phaser/core/files.rb: -------------------------------------------------------------------------------- 1 | # These requires must be loaded in order of dependency: 2 | # TODO: look into why `puts File.dirname(__FILE__)` is not working for Opal 3 | 4 | require 'opal/phaser/core/state' 5 | require 'opal/phaser/core/anchor' 6 | require 'opal/phaser/core/group' 7 | require 'opal/phaser/core/signal' 8 | require 'opal/phaser/core/world' 9 | require 'opal/phaser/core/stage' 10 | -------------------------------------------------------------------------------- /lib/opal/phaser/core/game.rb: -------------------------------------------------------------------------------- 1 | require 'opal/phaser/loader/cache' 2 | require 'opal/phaser/game_objects/game_object_factory' 3 | require 'opal/phaser/game_objects/game_object_creator' 4 | require 'opal/phaser/core/world' 5 | require 'opal/phaser/core/stage' 6 | require 'opal/phaser/core/state_manager' 7 | require 'opal/phaser/input/input' 8 | require 'opal/phaser/math/random_data_generator' 9 | require 'opal/phaser/core/camera' 10 | require 'opal/phaser/core/loader' 11 | require 'opal/phaser/core/scale_manager' 12 | require 'opal/phaser/pixi/web_gl_renderer' 13 | require 'opal/pixi/canvas_renderer' 14 | require 'opal/phaser/core/math' 15 | 16 | module Phaser 17 | AUTO = `Phaser.AUTO` 18 | WEBGL = `Phaser.WEBGL` 19 | CANVAS = `Phaser.CANVAS` 20 | 21 | class Game 22 | include Native 23 | 24 | def initialize(arg_hash = {}, &block) 25 | 26 | width = arg_hash.fetch(:width) { 800 } 27 | height = arg_hash.fetch(:height) { 600 } 28 | renderer = arg_hash.fetch(:renderer) { Phaser::AUTO } 29 | parent = arg_hash.fetch(:parent) { "" } 30 | state = arg_hash.fetch(:state) { nil } 31 | transparent = arg_hash.fetch(:transparent) { false } 32 | antialias = arg_hash.fetch(:antialias) { true } 33 | physics = arg_hash.fetch(:physics) { nil } 34 | 35 | if state 36 | state.game = self 37 | end 38 | 39 | if block_given? 40 | state = State.new(self) if state.nil? 41 | state.instance_eval(&block) 42 | end 43 | 44 | @native = %x{ 45 | new Phaser.Game(width, height, renderer, parent, #{state.to_n}, transparent, 46 | antialias, physics) 47 | } 48 | end 49 | 50 | alias_native :cache, :cache, as: Cache 51 | alias_native :add, :add, as: GameObjectFactory 52 | alias_native :world, :world, as: World 53 | alias_native :stage, :stage, as: Stage 54 | alias_native :state, :state, as: StateManager 55 | alias_native :physics, :physics, as: Physics 56 | alias_native :input, :input, as: Input 57 | #alias_native :time, :time, as: Time 58 | alias_native :rnd, :rnd, as: RandomDataGenerator 59 | alias_native :camera, :camera, as: Camera 60 | alias_native :scale, :scale, as: ScaleManager 61 | alias_native :math, :math, as: Phaser::Math 62 | 63 | alias_native :make, :make, as: GameObjectCreator 64 | alias_native :load, :load, as: Loader 65 | alias_native :debug 66 | alias_native :device 67 | alias_native :width 68 | alias_native :height 69 | alias_native :paused 70 | alias_native :destroy 71 | 72 | def time 73 | Phaser::Time.new(`#@native.time`) 74 | end 75 | 76 | def paused=(bool) 77 | `#@native.paused = bool` 78 | end 79 | 80 | def renderer 81 | if `#@native.renderer instanceof PIXI.WebGLRenderer` 82 | PIXI::WebGLRenderer.new(`#@native.renderer`) 83 | else 84 | PIXI::CanvasRenderer.new(`#@native.renderer`) 85 | end 86 | end 87 | 88 | def is_booted? 89 | `#@native.isBooted` 90 | end 91 | end 92 | end 93 | -------------------------------------------------------------------------------- /lib/opal/phaser/core/group.rb: -------------------------------------------------------------------------------- 1 | require 'opal/phaser/game_objects/sprite' 2 | module Phaser 3 | class Group 4 | include Native 5 | 6 | alias_native :cursor 7 | alias_native :physics_body_type, :physicsBodyType 8 | alias_native :enable_body_debug, :enableBodyDebug 9 | alias_native :add 10 | alias_native :add_child, :addChild 11 | alias_native :set_all, :setAll 12 | alias_native :call_all, :callAll 13 | alias_native :children 14 | # alias_native :create 15 | alias_native :create, :create, as: Sprite 16 | alias_native :destroy 17 | alias_native :remove 18 | alias_native :total 19 | 20 | alias_native :enable_body?, :enableBody 21 | 22 | def enable_body=(bool) 23 | `#@native.enableBody = bool` 24 | end 25 | end 26 | end -------------------------------------------------------------------------------- /lib/opal/phaser/core/loader.rb: -------------------------------------------------------------------------------- 1 | module Phaser 2 | class Loader 3 | include Native 4 | 5 | def initialize(game) 6 | if native?(game) 7 | super 8 | else 9 | super(`new Phaser.Loader(#{game.to_n})`) 10 | end 11 | end 12 | 13 | alias_native :image 14 | alias_native :spritesheet 15 | alias_native :atlas 16 | alias_native :start 17 | alias_native :audio 18 | 19 | alias_native :onFileComplete 20 | alias_native :onFileError 21 | alias_native :onFileStart 22 | alias_native :onLoadComplete 23 | alias_native :onLoadStart 24 | alias_native :atlasJSONArray 25 | alias_native :atlasJSONHash 26 | alias_native :atlasXML 27 | alias_native :atlas_xml, :atlasXML 28 | 29 | alias_native :set_preload_sprite, :setPreloadSprite 30 | alias_native :cross_origin=, :crossOrigin 31 | alias_native :bitmap_font, :bitmapFont 32 | 33 | def cross_origin=(value) 34 | `#@native.crossOrigin = #{value}` 35 | end 36 | 37 | def on(type, context, &block) 38 | case type.to_sym 39 | when :file_complete 40 | `#@native.onFileComplete.add(#{block.to_n}, #{context})` 41 | when :file_error 42 | `#@native.onFileError.add(#{block.to_n}, #{context})` 43 | when :file_start 44 | `#@native.onFileStart.add(#{block.to_n}, #{context})` 45 | when :load_complete 46 | `#@native.onLoadComplete.add(#{block.to_n}, #{context})` 47 | when :load_start 48 | `#@native.onLoadStart.add(#{block.to_n}, #{context})` 49 | else 50 | raise ArgumentError, "Unrecognized event type #{type}" 51 | end 52 | end 53 | 54 | private 55 | def self.async_image(game, options = {}, &block) 56 | that = new(game) 57 | that.async_image(options, &block) 58 | that 59 | end 60 | 61 | def async_image(options = {}, &block) 62 | override = options.fetch(:override) { false } 63 | key = options.fetch(:key) { raise ArgumentError, ":key is a required argument for this method" } 64 | url = options.fetch(:url) { raise ArgumentError, ":url is a required argument for this method" } 65 | self.cross_origin = options[:cross_origin] if options.has_key?(:cross_origin) 66 | image(key, url, override) 67 | on(:load_complete, options[:context] || self, &block) 68 | start 69 | end 70 | end 71 | end 72 | -------------------------------------------------------------------------------- /lib/opal/phaser/core/math.rb: -------------------------------------------------------------------------------- 1 | module Phaser 2 | class Math 3 | include Native 4 | alias_native :pi2, :PI2 5 | alias_native :angle_between, :angleBetween 6 | alias_native :angle_between_points, :angleBetweenPoints 7 | alias_native :angle_between_points_y, :angleBetweenPointsY 8 | alias_native :angle_between_y, :angleBetweenY 9 | alias_native :average, :average 10 | # bernstein method is documented as internal 11 | alias_native :bezier_interpolation, :bezierInterpolation 12 | # catmullRom method is documented as internal 13 | alias_native :catmull_rom_interpolation, :catmullRomInterpolation 14 | alias_native :ceil_to, :ceilTo 15 | alias_native :chance_roll, :chanceRoll 16 | alias_native :clamp, :clamp 17 | alias_native :clamp_bottom, :clampBottom 18 | alias_native :deg_to_rad, :degToRad 19 | alias_native :difference, :difference 20 | alias_native :distance, :distance 21 | alias_native :distance_pow, :distancePow 22 | alias_native :distance_sq, :distanceSq 23 | alias_native :factorial, :factorial 24 | alias_native :floor_to, :floorTo 25 | alias_native :fuzzy_ceil, :fuzzyCeil 26 | alias_native :fuzzy_equal, :fuzzyEqual 27 | alias_native :fuzzy_floor, :fuzzyFloor 28 | alias_native :fuzzy_greater_than, :fuzzyGreaterThan 29 | alias_native :fuzzy_less_than, :fuzzyLessThan 30 | 31 | # isEven, isOdd reimplemented as phaser version return 0 for false 32 | def even?(n) 33 | n.even? 34 | end 35 | 36 | def odd?(n) 37 | n.odd? 38 | end 39 | 40 | alias_native :linear, :linear 41 | alias_native :linear_interpolation, :linearInterpolation 42 | alias_native :map_linear, :mapLinear 43 | alias_native :max, :max 44 | alias_native :max_add, :maxAdd 45 | alias_native :max_property, :maxProperty 46 | alias_native :min, :min 47 | alias_native :min_property, :minProperty 48 | alias_native :min_sub, :minSub 49 | alias_native :normalize_angle, :normalizeAngle 50 | alias_native :percent, :percent 51 | alias_native :rad_to_deg, :radToDeg 52 | alias_native :reverse_angle, :reverseAngle 53 | alias_native :round_away_from_zero, :roundAwayFromZero 54 | alias_native :round_to, :roundTo 55 | alias_native :shear, :shear 56 | alias_native :sign, :sign 57 | 58 | def sin_cos_generator(length, sinAmplitude, cosAmplitude, frequency) 59 | Hash.new(`#@native.sinCosGenerator(length, sinAmplitude, cosAmplitude, frequency)`) 60 | end 61 | 62 | alias_native :smootherstep, :smootherstep 63 | alias_native :smoothstep, :smoothstep 64 | alias_native :snap_to, :snapTo 65 | alias_native :snap_to_ceil, :snapToCeil 66 | alias_native :snap_to_floor, :snapToFloor 67 | alias_native :within, :within 68 | alias_native :wrap, :wrap 69 | alias_native :wrap_angle, :wrapAngle 70 | alias_native :wrap_value, :wrapValue 71 | end 72 | end 73 | -------------------------------------------------------------------------------- /lib/opal/phaser/core/scale_manager.rb: -------------------------------------------------------------------------------- 1 | module Phaser 2 | class ScaleManager 3 | include Native 4 | 5 | # A scale mode that stretches content to fill all available space 6 | EXACT_FIT = 0; 7 | 8 | # A scale mode that prevents any scaling 9 | NO_SCALE = 1; 10 | 11 | # A scale mode that shows the entire game while maintaining proportions 12 | SHOW_ALL = 2; 13 | 14 | # A scale mode that causes the Game size to change 15 | RESIZE = 3; 16 | 17 | # A scale mode that allows a custom scale factor 18 | USER_SCALE = 4; 19 | 20 | alias_native :scale_mode, :scaleMode 21 | alias_native :refresh 22 | 23 | native_accessor_alias :page_align_horizontally, :pageAlignHorizontally 24 | native_accessor_alias :page_align_vertically, :pageAlignVertically 25 | 26 | def scale_mode=(mode) 27 | `#@native.scaleMode = #{mode}` 28 | end 29 | end 30 | end 31 | -------------------------------------------------------------------------------- /lib/opal/phaser/core/signal.rb: -------------------------------------------------------------------------------- 1 | module Phaser 2 | class Signal 3 | include Native 4 | 5 | alias_native :add 6 | alias_native :add_once, :addOnce 7 | alias_native :dispose 8 | end 9 | end -------------------------------------------------------------------------------- /lib/opal/phaser/core/stage.rb: -------------------------------------------------------------------------------- 1 | module Phaser 2 | class Stage 3 | include Native 4 | 5 | def background_color=(color) 6 | `#@native.backgroundColor = color` 7 | end 8 | end 9 | end -------------------------------------------------------------------------------- /lib/opal/phaser/core/state.rb: -------------------------------------------------------------------------------- 1 | require 'pp' 2 | require 'opal/phaser/core/game' 3 | module Phaser 4 | class State 5 | include Native 6 | def initialize(game = nil, &block) 7 | @native = `new Phaser.State` 8 | self.game = game 9 | 10 | if block_given? 11 | block.call(@game) 12 | end 13 | super(@native) 14 | end 15 | 16 | def game=(g_) 17 | @game = g_ 18 | `#@native.game = #@game` 19 | end 20 | 21 | def game 22 | @game 23 | end 24 | 25 | def init(*) 26 | end 27 | 28 | def preload 29 | end 30 | 31 | def create 32 | end 33 | 34 | def update 35 | end 36 | 37 | def render 38 | end 39 | 40 | def to_n 41 | _preload = proc { preload } 42 | _create = proc { create } 43 | _update = proc { update } 44 | _render = proc { render } 45 | _init = proc {|*args| init(*args) } 46 | %x{ 47 | var obj = { preload: #{_preload}, create: #{_create}, update: #{_update}, 48 | render: #{_render}, init: #{_init} } 49 | } 50 | 51 | return %x{ obj } 52 | end 53 | end 54 | end 55 | -------------------------------------------------------------------------------- /lib/opal/phaser/core/state_manager.rb: -------------------------------------------------------------------------------- 1 | module Phaser 2 | class StateManager 3 | include Native 4 | 5 | alias_native :add 6 | alias_native :start 7 | alias_native :restart 8 | end 9 | end 10 | -------------------------------------------------------------------------------- /lib/opal/phaser/core/world.rb: -------------------------------------------------------------------------------- 1 | require 'opal/phaser/native_helpers' 2 | require 'opal/phaser/geometry/rectangle' 3 | 4 | module Phaser 5 | class World 6 | include Native 7 | 8 | alias_native :width 9 | alias_native :height 10 | 11 | alias_native :set_bounds, :setBounds 12 | alias_native :bounds, :bounds, as: Phaser::Rectangle 13 | 14 | native_reader_alias :center_x, :centerX 15 | alias_native :x_center, :centerX 16 | alias_native :y_center, :centerY 17 | alias_native :random_y, :randomY 18 | alias_native :random_x, :randomX 19 | end 20 | end 21 | -------------------------------------------------------------------------------- /lib/opal/phaser/game_objects/bitmap_data.rb: -------------------------------------------------------------------------------- 1 | module Phaser 2 | class BitmapData 3 | include Native 4 | 5 | alias_native :draw 6 | alias_native :rect 7 | alias_native :fill 8 | alias_native :clear 9 | alias_native :canvas 10 | alias_native :resize 11 | alias_native :alpha_mask, :alphaMask 12 | 13 | end 14 | end -------------------------------------------------------------------------------- /lib/opal/phaser/game_objects/button.rb: -------------------------------------------------------------------------------- 1 | require 'opal/phaser/geometry/point' 2 | module Phaser 3 | class Button 4 | include Native 5 | 6 | alias_native :anchor, :anchor, as: Point 7 | alias_native :load_texture, :loadTexture 8 | end 9 | end 10 | -------------------------------------------------------------------------------- /lib/opal/phaser/game_objects/emitter.rb: -------------------------------------------------------------------------------- 1 | module Phaser 2 | class Emitter 3 | include Native 4 | # Properties 5 | native_accessor :alive 6 | native_accessor :alpha 7 | native_accessor :alpha_data, :alphaData 8 | native_accessor :angle 9 | native_accessor :angular_drag, :angularDrag 10 | native_accessor :area 11 | native_accessor :auto_alpha, :autoAlpha 12 | native_accessor :auto_scale, :autoScale 13 | native_accessor :blend_mode, :blendMode 14 | native_reader :bottom 15 | native_accessor :bounce 16 | native_accessor :cache_as_bitmap, :cacheAsBitmap 17 | native_accessor :camera_offset, :cameraOffset 18 | native_reader :children 19 | native_accessor :class_type, :classType 20 | native_reader :cursor 21 | native_accessor :cursor_index, :cursorIndex 22 | native_accessor :emit_x, :emitX 23 | native_accessor :emit_y, :emitY 24 | native_accessor :enable_body, :enableBody 25 | native_accessor :enable_body_debug, :enableBodyDebug 26 | native_accessor :exists 27 | native_accessor :filter_area, :filterArea 28 | native_accessor :filters 29 | native_accessor :fixed_to_camera, :fixedToCamera 30 | native_accessor :frequency 31 | native_accessor :gravity 32 | native_accessor :hash 33 | native_accessor :height 34 | native_accessor :hit_area, :hitArea 35 | native_accessor :ignore_destroy, :ignoreDestroy 36 | native_reader :left 37 | native_reader :length 38 | native_accessor :lifespan 39 | native_accessor :mask 40 | native_accessor :max_particle_alpha, :maxParticleAlpha 41 | native_accessor :max_particle_scale, :maxParticleScale 42 | native_accessor :max_particle_speed, :maxParticleSpeed 43 | native_accessor :max_particles, :maxParticles 44 | native_accessor :max_rotation, :maxRotation 45 | native_accessor :min_particle_alpha, :minParticleAlpha 46 | native_accessor :min_particle_scale, :minParticleScale 47 | native_accessor :min_particle_speed, :minParticleSpeed 48 | native_accessor :min_rotation, :minRotation 49 | native_accessor :name 50 | native_accessor :on 51 | native_accessor :on_destroy, :onDestroy 52 | native_reader :parent 53 | native_accessor :particle_anchor, :particleAnchor 54 | native_accessor :particle_bring_to_top, :particleBringToTop 55 | native_accessor :particle_class, :particleClass 56 | native_accessor :particle_drag, :particleDrag 57 | native_accessor :particle_send_to_back, :particleSendToBack 58 | native_accessor :pending_destroy, :pendingDestroy 59 | native_accessor :physics_body_type, :physicsBodyType 60 | native_accessor :physics_sort_direction, :physicsSortDirection 61 | native_reader :physics_type, :physicsType 62 | native_accessor :pivot 63 | native_accessor :position 64 | native_accessor :renderable 65 | native_reader :right 66 | native_accessor :rotation 67 | native_accessor :scale 68 | native_accessor :scale_data, :scaleData 69 | native_reader :stage 70 | native_reader :top 71 | native_reader :total 72 | native_accessor :transform_callback, :transformCallback 73 | native_accessor :transform_callback_context, :transformCallbackContext 74 | native_accessor :visible 75 | native_accessor :width 76 | native_reader :world_alpha, :worldAlpha 77 | native_reader :world_position, :worldPosition 78 | native_reader :world_rotation, :worldRotation 79 | native_reader :world_scale, :worldScale 80 | native_reader :world_visible, :worldVisible 81 | native_accessor :x 82 | native_accessor :y 83 | native_accessor :z 84 | 85 | # Methods 86 | alias_native :add 87 | alias_native :add_all, :addAll 88 | alias_native :add_at, :addAt 89 | alias_native :add_child, :addChild 90 | alias_native :add_child_at, :addChildAt 91 | alias_native :add_multiple, :addMultiple 92 | alias_native :add_to_hash, :addToHash 93 | alias_native :at 94 | alias_native :bring_to_top, :bringToTop 95 | alias_native :call_all, :callAll 96 | alias_native :call_all_exists, :callAllExists 97 | alias_native :check_all, :checkAll 98 | alias_native :check_property, :checkProperty 99 | alias_native :count_dead, :countDead 100 | alias_native :count_living, :countLiving 101 | alias_native :create 102 | alias_native :create_multiple, :createMultiple 103 | alias_native :custom_sort, :customSort 104 | alias_native :destroy 105 | alias_native :divide_all, :divideAll 106 | alias_native :emit_particle, :emitParticle 107 | alias_native :explode 108 | alias_native :filter 109 | alias_native :flow 110 | alias_native :for_each, :forEach 111 | alias_native :for_each_alive, :forEachAlive 112 | alias_native :for_each_dead, :forEachDead 113 | alias_native :for_each_exists, :forEachExists 114 | alias_native :generate_texture, :generateTexture 115 | alias_native :get_at, :getAt 116 | alias_native :get_bottom, :getBottom 117 | alias_native :get_bounds, :getBounds 118 | alias_native :get_child_at, :getChildAt 119 | alias_native :get_child_index, :getChildIndex 120 | alias_native :get_first_alive, :getFirstAlive 121 | alias_native :get_first_dead, :getFirstDead 122 | alias_native :get_first_exists, :getFirstExists 123 | alias_native :get_index, :getIndex 124 | alias_native :get_local_bounds, :getLocalBounds 125 | alias_native :get_random, :getRandom 126 | alias_native :get_top, :getTop 127 | alias_native :has_property, :hasProperty 128 | alias_native :iterate 129 | alias_native :kill 130 | alias_native :make_particles, :makeParticles 131 | alias_native :move_all, :moveAll 132 | alias_native :move_down, :moveDown 133 | alias_native :move_up, :moveUp 134 | alias_native :multiply_all, :multiplyAll 135 | alias_native :next 136 | alias_native :previous 137 | alias_native :remove 138 | alias_native :remove_all, :removeAll 139 | alias_native :remove_between, :removeBetween 140 | alias_native :remove_child, :removeChild 141 | alias_native :remove_child_at, :removeChildAt 142 | alias_native :remove_children, :removeChildren 143 | alias_native :remove_from_hash, :removeFromHash 144 | alias_native :remove_stage_reference, :removeStageReference 145 | alias_native :replace 146 | alias_native :reset_cursor, :resetCursor 147 | alias_native :reverse 148 | alias_native :revive 149 | alias_native :send_to_back, :sendToBack 150 | alias_native :set 151 | alias_native :set_all, :setAll 152 | alias_native :set_all_children, :setAllChildren 153 | alias_native :set_alpha, :setAlpha 154 | alias_native :set_child_index, :setChildIndex 155 | alias_native :set_property, :setProperty 156 | alias_native :set_rotation, :setRotation 157 | alias_native :set_scale, :setScale 158 | alias_native :set_size, :setSize 159 | alias_native :set_stage_reference, :setStageReference 160 | alias_native :set_x_speed, :setXSpeed 161 | alias_native :set_y_speed, :setYSpeed 162 | alias_native :sort 163 | alias_native :start 164 | alias_native :sub_all, :subAll 165 | alias_native :swap 166 | alias_native :swap_children, :swapChildren 167 | alias_native :to_global, :toGlobal 168 | alias_native :to_local, :toLocal 169 | alias_native :update 170 | alias_native :update_cache, :updateCache 171 | alias_native :xy 172 | end 173 | end 174 | -------------------------------------------------------------------------------- /lib/opal/phaser/game_objects/events.rb: -------------------------------------------------------------------------------- 1 | require 'opal/phaser/core/signal' 2 | 3 | module Phaser 4 | class Events 5 | include Native 6 | 7 | alias_native :on_input_down, :onInputDown, as: Signal 8 | 9 | def on(type, context, &block) 10 | case type.to_sym 11 | when :up 12 | `#@native.onInputUp.add(#{block.to_n}, #{context})` 13 | when :down 14 | `#@native.onInputDown.add(#{block.to_n}, #{context})` 15 | when :out 16 | `#@native.onInputOut.add(#{block.to_n}, #{context})` 17 | when :over 18 | `#@native.onInputOver.add(#{block.to_n}, #{context})` 19 | when :out_of_bounds 20 | `#@native.onOutOfBounds.add(#{block.to_n}, #{context})` 21 | else 22 | raise ArgumentError, "Unrecognized event type #{type}" 23 | end 24 | end 25 | end 26 | end 27 | -------------------------------------------------------------------------------- /lib/opal/phaser/game_objects/files.rb: -------------------------------------------------------------------------------- 1 | # These requires must be loaded in order of dependency: 2 | require 'opal/phaser/game_objects/events' 3 | require 'opal/phaser/game_objects/sprite' 4 | require 'opal/phaser/game_objects/tile_sprite' 5 | require 'opal/phaser/game_objects/text' 6 | require 'opal/phaser/game_objects/image' 7 | require 'opal/phaser/game_objects/bitmap_data' 8 | require 'opal/phaser/game_objects/button' 9 | require 'opal/phaser/game_objects/filter' 10 | require 'opal/phaser/game_objects/rope' 11 | require 'opal/phaser/game_objects/game_object_factory' 12 | -------------------------------------------------------------------------------- /lib/opal/phaser/game_objects/filter.rb: -------------------------------------------------------------------------------- 1 | module Phaser 2 | class Filter 3 | include Native 4 | alias_native :set_resolution, :setResolution 5 | alias_native :update 6 | 7 | def initialize(game, uniforms, source) 8 | @native = `new Phaser.Filter(#{game.to_n}, #{uniforms.to_n}, #{source.to_n})` 9 | end 10 | end 11 | end 12 | -------------------------------------------------------------------------------- /lib/opal/phaser/game_objects/game_object_creator.rb: -------------------------------------------------------------------------------- 1 | require 'opal/phaser/game_objects/sprite' 2 | require 'opal/phaser/core/group' 3 | require 'opal/phaser/game_objects/text' 4 | require 'opal/phaser/game_objects/image' 5 | require 'opal/phaser/game_objects/bitmap_data' 6 | require 'opal/phaser/tween/tween' 7 | require 'opal/phaser/game_objects/tile_sprite' 8 | require 'opal/phaser/game_objects/button' 9 | 10 | module Phaser 11 | class GameObjectCreator 12 | include Native 13 | 14 | alias_native :sprite, :sprite, as: Sprite 15 | alias_native :group, :group, as: Group 16 | alias_native :text, :text, as: Text 17 | alias_native :image, :image, as: Image 18 | alias_native :bitmap_data, :bitmapData, as: BitmapData 19 | alias_native :tween, :tween, as: Tween 20 | alias_native :tile_sprite, :tileSprite, as: TileSprite 21 | alias_native :button, :button, as: Button 22 | end 23 | end 24 | -------------------------------------------------------------------------------- /lib/opal/phaser/game_objects/game_object_factory.rb: -------------------------------------------------------------------------------- 1 | require 'opal/phaser/game_objects/sprite' 2 | require 'opal/phaser/core/group' 3 | require 'opal/phaser/game_objects/text' 4 | require 'opal/phaser/game_objects/image' 5 | require 'opal/phaser/game_objects/bitmap_data' 6 | require 'opal/phaser/tween/tween' 7 | require 'opal/phaser/game_objects/tile_sprite' 8 | require 'opal/phaser/game_objects/button' 9 | require 'opal/phaser/sound/sound' 10 | require 'opal/phaser/game_objects/graphics' 11 | require 'opal/phaser/game_objects/emitter' 12 | require 'opal/phaser/game_objects/rope' 13 | 14 | module Phaser 15 | class GameObjectFactory 16 | include Native 17 | 18 | alias_native :sprite, :sprite, as: Sprite 19 | alias_native :group, :group, as: Group 20 | alias_native :text, :text, as: Text 21 | alias_native :image, :image, as: Image 22 | alias_native :bitmap_data, :bitmapData, as: BitmapData 23 | alias_native :tween, :tween, as: Tween 24 | alias_native :tile_sprite, :tileSprite, as: TileSprite 25 | alias_native :button, :button, as: Button 26 | alias_native :audio, :audio, as: Sound 27 | alias_native :graphics, :graphics, as: Graphics 28 | alias_native :emitter, :emitter, as: Emitter 29 | alias_native :rope, :rope, as: Rope 30 | alias_native :bitmap_text, :bitmapText 31 | end 32 | end 33 | -------------------------------------------------------------------------------- /lib/opal/phaser/game_objects/graphics.rb: -------------------------------------------------------------------------------- 1 | module Phaser 2 | class Graphics 3 | include Native 4 | # Properties 5 | native_accessor :alive 6 | native_accessor :alpha 7 | native_accessor :angle 8 | native_accessor :animations 9 | native_accessor :auto_cull, :autoCull 10 | native_accessor :blend_mode, :blendMode 11 | native_accessor :body 12 | native_reader :bottom 13 | native_accessor :bounds_padding, :boundsPadding 14 | native_accessor :camera_offset, :cameraOffset 15 | native_accessor :check_world_bounds, :checkWorldBounds 16 | native_reader :children 17 | native_accessor :components 18 | native_accessor :debug 19 | native_reader :destroy_phase, :destroyPhase 20 | native_accessor :events 21 | native_accessor :exists 22 | native_accessor :fill_alpha, :fillAlpha 23 | native_accessor :filter_area, :filterArea 24 | native_accessor :filters 25 | native_accessor :fixed_to_camera, :fixedToCamera 26 | native_reader :fresh 27 | native_accessor :game 28 | native_accessor :height 29 | native_accessor :hit_area, :hitArea 30 | native_reader :in_camera, :inCamera 31 | native_accessor :input 32 | native_accessor :input_enabled, :inputEnabled 33 | native_reader :in_world, :inWorld 34 | native_accessor :is_mask, :isMask 35 | native_accessor :key 36 | native_reader :left 37 | native_accessor :lifespan 38 | native_accessor :line_color, :lineColor 39 | native_accessor :line_width, :lineWidth 40 | native_accessor :mask 41 | native_accessor :name 42 | native_reader :offset_x, :offsetX 43 | native_reader :offset_y, :offsetY 44 | native_accessor :out_of_bounds_kill, :outOfBoundsKill 45 | native_reader :parent 46 | native_accessor :pending_destroy, :pendingDestroy 47 | native_reader :physics_type, :physicsType 48 | native_accessor :pivot 49 | native_accessor :position 50 | native_reader :previous_position, :previousPosition 51 | native_reader :previous_rotation, :previousRotation 52 | native_accessor :renderable 53 | native_reader :render_order_id, :renderOrderID 54 | native_reader :right 55 | native_accessor :rotation 56 | native_accessor :scale 57 | native_reader :stage 58 | native_accessor :tint 59 | native_reader :top 60 | native_accessor :transform_callback, :transformCallback 61 | native_accessor :transform_callback_context, :transformCallbackContext 62 | native_accessor :type 63 | native_accessor :visible 64 | native_accessor :width 65 | native_accessor :world 66 | native_reader :world_alpha, :worldAlpha 67 | native_reader :world_position, :worldPosition 68 | native_reader :world_rotation, :worldRotation 69 | native_reader :world_scale, :worldScale 70 | native_reader :world_visible, :worldVisible 71 | native_accessor :x 72 | native_accessor :y 73 | native_accessor :z 74 | 75 | # Methods 76 | alias_native :add_child, :addChild 77 | alias_native :add_child_at, :addChildAt 78 | alias_native :arc 79 | alias_native :begin_fill, :beginFill 80 | alias_native :bezier_curve_to, :bezierCurveTo 81 | alias_native :clear 82 | alias_native :destroy 83 | alias_native :destory_cached_sprite, :destroyCachedSprite 84 | alias_native :draw_circle, :drawCircle 85 | alias_native :draw_ellipse, :drawEllipse 86 | alias_native :draw_polygon, :drawPolygon 87 | alias_native :draw_rect, :drawRect 88 | alias_native :draw_rounded_rect, :drawRoundedRect 89 | alias_native :draw_shape, :drawShape 90 | alias_native :end_fill, :endFill 91 | alias_native :generate_texture, :generateTexture 92 | alias_native :get_bounds, :getBounds 93 | alias_native :get_child_at, :getChildAt 94 | alias_native :git_child_index, :getChildIndex 95 | alias_native :git_local_bounds, :getLocalBounds 96 | alias_native :kill 97 | alias_native :line_style, :lineStyle 98 | alias_native :line_to, :lineTo 99 | alias_native :move_to, :moveTo 100 | alias_native :post_update, :postUpdate 101 | alias_native :pre_update, :preUpdate 102 | alias_native :quadratic_curve_to, :quadraticCurveTo 103 | alias_native :remove_child, :removeChild 104 | alias_native :remove_child_at, :removeChildAt 105 | alias_native :remove_children, :removeChildren 106 | alias_native :remove_stage_reference, :removeStageReference 107 | alias_native :reset 108 | alias_native :revive 109 | alias_native :set_child_index, :setChildIndex 110 | alias_native :set_stage_reference, :setStageReference 111 | alias_native :swap_children, :swapChildren 112 | alias_native :to_global, :toGlobal 113 | alias_native :to_local, :toLocal 114 | alias_native :update 115 | alias_native :update_cache, :updateCache 116 | alias_native :update_local_bounds, :updateLocalBounds 117 | end 118 | end 119 | -------------------------------------------------------------------------------- /lib/opal/phaser/game_objects/image.rb: -------------------------------------------------------------------------------- 1 | require 'opal/phaser/core/anchor' 2 | require 'opal/phaser/game_objects/events' 3 | 4 | module Phaser 5 | class Image < PIXI::Sprite 6 | include Native 7 | 8 | alias_native :input 9 | alias_native :name 10 | alias_native :kill 11 | 12 | alias_native :smoothed= 13 | 14 | def input_enabled=(bool) 15 | `#@native.inputEnabled = bool` 16 | end 17 | 18 | alias_native :load_texture, :loadTexture 19 | alias_native :events, :events, as: Events 20 | end 21 | end -------------------------------------------------------------------------------- /lib/opal/phaser/game_objects/rope.rb: -------------------------------------------------------------------------------- 1 | module Phaser 2 | class Rope 3 | include Native 4 | # This is bad wrapper and would be nice to make it better 5 | native_accessor :updateAnimation 6 | alias_native :points 7 | end 8 | end 9 | -------------------------------------------------------------------------------- /lib/opal/phaser/game_objects/sprite.rb: -------------------------------------------------------------------------------- 1 | require 'opal/phaser/physics/physics' 2 | require 'opal/phaser/core/anchor' 3 | require 'opal/phaser/animation/animation_manager' 4 | require 'opal/phaser/game_objects/events' 5 | 6 | module Phaser 7 | class Sprite < PIXI::Sprite 8 | include Native 9 | 10 | alias_native :key 11 | alias_native :bounce 12 | 13 | alias_native :play 14 | alias_native :crop 15 | 16 | alias_native :exists= 17 | alias_native :alive= 18 | alias_native :frame= 19 | alias_native :smoothed= 20 | 21 | alias_native :right 22 | 23 | alias_native :kill 24 | 25 | def input_enabled=(bool) 26 | `#@native.inputEnabled = bool` 27 | end 28 | 29 | def frame_name=(name) 30 | `#@native.frameName = name` 31 | end 32 | 33 | def auto_cull=(bool) 34 | `#@native.autoCull = bool` 35 | end 36 | 37 | def check_world_bounds=(bool) 38 | `#@native.checkWorldBounds = bool` 39 | end 40 | 41 | def update_crop 42 | `#@native.updateCrop()` 43 | end 44 | 45 | alias_native :load_texture, :loadTexture 46 | 47 | alias_native :body, :body, as: Physics::Arcade::Body 48 | alias_native :animations, :animations, as: AnimationManager 49 | alias_native :events, :events, as: Events 50 | 51 | def on(type, context, &block) 52 | events.on(type, context, &block) 53 | end 54 | 55 | alias_native :add_child, :addChild 56 | native_accessor :angle 57 | native_accessor :frame 58 | native_accessor :name 59 | native_accessor_alias :fixed_to_camera, :fixedToCamera 60 | native_accessor :key 61 | end 62 | end 63 | -------------------------------------------------------------------------------- /lib/opal/phaser/game_objects/text.rb: -------------------------------------------------------------------------------- 1 | require 'opal/phaser/geometry/point' 2 | module Phaser 3 | class Text 4 | include Native 5 | 6 | alias_native :destroy 7 | alias_native :anchor, :anchor, as: Point 8 | 9 | native_accessor :x 10 | native_accessor :y 11 | native_accessor :angle 12 | native_accessor :fill 13 | native_accessor :visible 14 | native_accessor :text 15 | 16 | native_accessor :font, :stroke, :align 17 | native_accessor_alias :font_size, :fontSize 18 | native_accessor_alias :font_weight, :fontWeight 19 | native_accessor_alias :stroke_thickness, :strokeThickness 20 | native_accessor_alias :fixed_to_camera, :fixedToCamera 21 | alias_native :events, as: Phaser::Events 22 | native_accessor :inputEnabled 23 | alias_native :set_shadow, :setShadow 24 | end 25 | end 26 | -------------------------------------------------------------------------------- /lib/opal/phaser/game_objects/tile_sprite.rb: -------------------------------------------------------------------------------- 1 | require 'opal/phaser/geometry/point' 2 | module Phaser 3 | class TileSprite < PIXI::Extras::TilingSprite 4 | include Native 5 | 6 | alias_native :auto_scroll, :autoScroll 7 | alias_native :stop_scroll, :stopScroll 8 | alias_native :tile_position, :tilePosition, as: Point 9 | alias_native :destroy 10 | 11 | alias_native :body 12 | end 13 | end -------------------------------------------------------------------------------- /lib/opal/phaser/geometry/point.rb: -------------------------------------------------------------------------------- 1 | module Phaser 2 | class Point 3 | include Native 4 | 5 | # When its automatically aliased we just pass the object to super 6 | def initialize(_x = 0, _y = 0) 7 | if `typeof(#{_x})` == "object" 8 | super(_x) 9 | else 10 | @native = `new Phaser.Point(#{_x}, #{_y})` 11 | super(@native) 12 | end 13 | end 14 | 15 | alias_native :x 16 | alias_native :x= 17 | alias_native :y 18 | alias_native :y= 19 | alias_native :set 20 | alias_native :setTo, :set_to 21 | end 22 | end 23 | -------------------------------------------------------------------------------- /lib/opal/phaser/geometry/rectangle.rb: -------------------------------------------------------------------------------- 1 | module Phaser 2 | class Rectangle 3 | include Native 4 | 5 | def initialize(x, y, w, h) 6 | super `new Phaser.Rectangle(x, y, w, h)` 7 | end 8 | 9 | alias_native :x 10 | alias_native :x= 11 | alias_native :y 12 | alias_native :y= 13 | end 14 | end 15 | -------------------------------------------------------------------------------- /lib/opal/phaser/input/cursor_keys.rb: -------------------------------------------------------------------------------- 1 | module Phaser 2 | class CursorKeys 3 | include Native 4 | alias_native :left, as: Key 5 | alias_native :right, as: Key 6 | alias_native :up, as: Key 7 | alias_native :down, as: Key 8 | end 9 | end 10 | -------------------------------------------------------------------------------- /lib/opal/phaser/input/files.rb: -------------------------------------------------------------------------------- 1 | # These requires must be loaded in order of dependency: 2 | require 'opal/phaser/input/key' 3 | require 'opal/phaser/input/cursor_keys' 4 | require 'opal/phaser/input/keyboard' 5 | require 'opal/phaser/input/input' 6 | -------------------------------------------------------------------------------- /lib/opal/phaser/input/input.rb: -------------------------------------------------------------------------------- 1 | require 'opal/phaser/input/keyboard' 2 | require 'opal/phaser/input/pointer' 3 | require 'opal/phaser/input/mouse_event' 4 | require 'opal/phaser/core/signal' 5 | 6 | module Phaser 7 | class Input 8 | include Native 9 | 10 | alias_native :x 11 | alias_native :y 12 | 13 | alias_native :keyboard, :keyboard, as: Keyboard 14 | alias_native :mouse_pointer, :mousePointer, as: Pointer 15 | 16 | def on(type, &block) 17 | cast_and_yield = proc do |pointer, event| 18 | pointer = Phaser::Pointer.new(pointer) 19 | event = event && Phaser::MouseEvent.new(event) 20 | block.call(pointer, event) 21 | end 22 | 23 | if block_given? 24 | case type.to_sym 25 | when :down 26 | `#@native.onDown.add(#{cast_and_yield.to_n})` 27 | when :up 28 | `#@native.onUp.add(#{cast_and_yield.to_n})` 29 | when :tap 30 | `#@native.onTap.add(#{cast_and_yield.to_n})` 31 | when :hold 32 | `#@native.onHold.add(#{cast_and_yield.to_n})` 33 | else 34 | raise ArgumentError, "Unrecognized event type #{type}" 35 | end 36 | else 37 | Signal.new 38 | end 39 | end 40 | alias_native :active_pointer, :activePointer, as: Pointer 41 | end 42 | end 43 | -------------------------------------------------------------------------------- /lib/opal/phaser/input/key.rb: -------------------------------------------------------------------------------- 1 | module Phaser 2 | class Key 3 | include Native 4 | 5 | def on(type, &block) 6 | case type.to_sym 7 | when :down 8 | `#@native.onDown.add(#{block.to_n})` 9 | when :up 10 | `#@native.onUp.add(#{block.to_n})` 11 | else 12 | raise ArgumentError, "Unrecognized event type #{type}" 13 | end 14 | end 15 | 16 | def reset(hard) 17 | if hard == "hard".to_sym 18 | `#@native.reset(true)` 19 | else 20 | `#@native.reset(false)` 21 | end 22 | end 23 | 24 | alias_native :down?, :isDown 25 | end 26 | end 27 | -------------------------------------------------------------------------------- /lib/opal/phaser/input/keyboard.rb: -------------------------------------------------------------------------------- 1 | require 'opal/phaser/input/key' 2 | require 'opal/phaser/input/cursor_keys' 3 | module Phaser 4 | class Keyboard 5 | include Native 6 | 7 | SPACEBAR = `Phaser.Keyboard.SPACEBAR` 8 | LEFT = `Phaser.Keyboard.LEFT` 9 | RIGHT = `Phaser.Keyboard.RIGHT` 10 | A = `Phaser.Keyboard.A` 11 | D = `Phaser.Keyboard.D` 12 | ESC = `Phaser.Keyboard.ESC` 13 | 14 | alias_native :create_cursor_keys, :createCursorKeys 15 | alias_native :remove_key, :removeKey 16 | 17 | alias_native :add_key, :addKey, as: Key 18 | 19 | alias_native :down?, :isDown 20 | 21 | alias_native :create_cursor_keys, :createCursorKeys, as: CursorKeys 22 | def on_down_callback=(callback) 23 | `#@native.onDownCallback = #{callback.to_n}` 24 | end 25 | end 26 | end 27 | -------------------------------------------------------------------------------- /lib/opal/phaser/input/mouse_event.rb: -------------------------------------------------------------------------------- 1 | # This is basic browser class, not part of phaser 2 | # https://developer.mozilla.org/en/docs/Web/API/MouseEvent 3 | class MouseEvent 4 | include Native 5 | alias_native :client_x, :clientX 6 | alias_native :client_y, :clientY 7 | alias_native :movement_x, :movementX 8 | alias_native :movement_y, :movementY 9 | alias_native :offset_x, :offsetX 10 | alias_native :offset_y, :offsetY 11 | alias_native :page_x, :pageX 12 | alias_native :page_y, :pageY 13 | alias_native :x 14 | alias_native :y 15 | 16 | alias_native :region 17 | alias_native :target 18 | alias_native :current_target, :currentTarget 19 | alias_native :related_target, :relatedTarget 20 | alias_native :screen_x, :screenX 21 | alias_native :screen_y, :screenY 22 | 23 | alias_native :alt_key, :altKey 24 | alias_native :shift_key, :shiftKey 25 | alias_native :meta_key, :metaKey 26 | alias_native :which 27 | alias_native :button 28 | alias_native :buttons 29 | 30 | alias_native :timestamp, :timeStamp 31 | alias_native :webkit_force, :webkitForce 32 | 33 | alias_native :prevent_default, :preventDefault 34 | end 35 | -------------------------------------------------------------------------------- /lib/opal/phaser/input/pointer.rb: -------------------------------------------------------------------------------- 1 | require 'opal/phaser/geometry/point' 2 | module Phaser 3 | class Pointer 4 | include Native 5 | 6 | alias_native :down?, :isDown 7 | alias_native :up?, :isUp 8 | 9 | alias_native :target_object, :targetObject 10 | 11 | alias_native :reset 12 | 13 | alias_native :position, :position, as: Point 14 | 15 | native_accessor_alias :world_x, :worldX 16 | native_accessor_alias :world_y, :worldY 17 | alias_native :x 18 | alias_native :y 19 | end 20 | end 21 | -------------------------------------------------------------------------------- /lib/opal/phaser/loader/cache.rb: -------------------------------------------------------------------------------- 1 | require 'opal/phaser/game_objects/image' 2 | module Phaser 3 | class Cache 4 | include Native 5 | 6 | alias_native :add_sprite_sheet, :addSpriteSheet 7 | alias_native :get_image, :getImage, as: Image 8 | 9 | alias_native :check_image, :checkImageKey 10 | end 11 | end -------------------------------------------------------------------------------- /lib/opal/phaser/loader/files.rb: -------------------------------------------------------------------------------- 1 | # These requires must be loaded in order of dependency: 2 | require 'opal/phaser/loader/cache' 3 | -------------------------------------------------------------------------------- /lib/opal/phaser/math/files.rb: -------------------------------------------------------------------------------- 1 | # These requires must be loaded in order of dependency: 2 | require 'opal/phaser/math/random_data_generator' -------------------------------------------------------------------------------- /lib/opal/phaser/math/random_data_generator.rb: -------------------------------------------------------------------------------- 1 | module Phaser 2 | class RandomDataGenerator 3 | include Native 4 | 5 | alias_native :integer_in_range, :integerInRange 6 | alias_native :between 7 | end 8 | end -------------------------------------------------------------------------------- /lib/opal/phaser/native_helpers.rb: -------------------------------------------------------------------------------- 1 | def native_accessor_alias(new, old) 2 | native_reader_alias(new, old) 3 | native_writer_alias(new, old) 4 | end 5 | 6 | def native_reader_alias(new, old) 7 | define_method new do 8 | Native(`#@native[old]`) 9 | end 10 | end 11 | 12 | def native_writer_alias(new, old) 13 | define_method "#{new}=" do |value| 14 | Native(`#@native[old] = value`) 15 | end 16 | end 17 | -------------------------------------------------------------------------------- /lib/opal/phaser/physics/files.rb: -------------------------------------------------------------------------------- 1 | # These requires must be loaded in order of dependency: 2 | require 'opal/phaser/physics/physics' 3 | -------------------------------------------------------------------------------- /lib/opal/phaser/physics/physics.rb: -------------------------------------------------------------------------------- 1 | require 'opal/phaser/native_helpers' 2 | module Phaser 3 | class Physics 4 | include Native 5 | 6 | class Arcade 7 | include Native 8 | 9 | alias_native :enable 10 | alias_native :collide 11 | # alias_native :overlap 12 | alias_native :distance_to_pointer, :distanceToPointer 13 | alias_native :move_to_pointer, :moveToPointer 14 | alias_native :check_collision, :checkCollision 15 | alias_native :enable_body, :enableBody 16 | 17 | def overlap(object1, object2, &block) 18 | wrapper = nil 19 | if block_given? 20 | wrapper = proc { |o1, o2| 21 | o1 = Phaser::Sprite.new(o1) 22 | o2 = Phaser::Sprite.new(o2) 23 | block.call(o1, o2) 24 | }.to_n 25 | end 26 | `#@native.overlap(#{object1.to_n}, #{object2.to_n}, #{wrapper})` 27 | end 28 | 29 | class Body 30 | include Native 31 | 32 | alias_native :gravity 33 | alias_native :velocity 34 | alias_native :bounce 35 | alias_native :touching 36 | alias_native :position 37 | alias_native :set_size, :setSize 38 | alias_native :on_floor?, :onFloor 39 | alias_native :blocked 40 | native_accessor_alias :max_velocity, :maxVelocity 41 | 42 | def collide_world_bounds=(bool) 43 | `#@native.collideWorldBounds = bool` 44 | end 45 | 46 | def immovable=(bool) 47 | `#@native.immovable = bool` 48 | end 49 | end 50 | end 51 | 52 | ARCADE = `Phaser.Physics.ARCADE` 53 | P2JS = `Phaser.Physics.P2JS` 54 | NINJA = `Phaser.Physics.NINJA` 55 | BOX2D = `Phaser.Physics.BOX2D` 56 | 57 | alias_native :start_system, :startSystem 58 | alias_native :arcade, :arcade, as: Arcade 59 | alias_native :enable 60 | end 61 | end 62 | -------------------------------------------------------------------------------- /lib/opal/phaser/pixi/canvas_renderer.rb: -------------------------------------------------------------------------------- 1 | class PIXI::CanvasRenderer 2 | end 3 | -------------------------------------------------------------------------------- /lib/opal/phaser/pixi/web_gl_renderer.rb: -------------------------------------------------------------------------------- 1 | class PIXI::WebGLRenderer 2 | alias_native :offset, :offset, as: Phaser::Point 3 | end 4 | -------------------------------------------------------------------------------- /lib/opal/phaser/setup.rb: -------------------------------------------------------------------------------- 1 | # We need the Opal Native support library here: 2 | require 'native' 3 | 4 | # These requires must be loaded in order of dependency: 5 | require 'opal/phaser/core/files' 6 | require 'opal/phaser/animation/files' 7 | require 'opal/phaser/tween/files' 8 | require 'opal/phaser/physics/files' 9 | require 'opal/phaser/game_objects/files' 10 | require 'opal/phaser/input/files' 11 | require 'opal/phaser/loader/files' 12 | require 'opal/phaser/time/files' 13 | require 'opal/phaser/math/files' 14 | 15 | # TODO: This is odd here as it doesn't fit the files.rb require pattern... how can we clean this up? 16 | require 'opal/phaser/core/game' 17 | -------------------------------------------------------------------------------- /lib/opal/phaser/sound/sound.rb: -------------------------------------------------------------------------------- 1 | module Phaser 2 | class Sound 3 | include Native 4 | 5 | def play(args = {}) 6 | arg_list = {marker: "", position: 0, volume: 1, loop: false, force_restart: true} 7 | 8 | arg_list.each do |name, default_value| 9 | unless args.include?(name) 10 | args[name] = default_value 11 | end 12 | end 13 | 14 | `#@native.play(#{args[:marker]}, #{args[:position]}, #{args[:volume]}, #{args[:loop]}, #{args[:force_restart]})` 15 | end 16 | 17 | alias_native :stop 18 | alias_native :destroy 19 | alias_native :fade_in, :fadeIn 20 | 21 | native_accessor :volume 22 | native_accessor :mute 23 | native_accessor :loop 24 | end 25 | end 26 | -------------------------------------------------------------------------------- /lib/opal/phaser/time/files.rb: -------------------------------------------------------------------------------- 1 | # These requires must be loaded in order of dependency: 2 | require 'opal/phaser/time/timer_event' 3 | require 'opal/phaser/time/timer' 4 | require 'opal/phaser/time/time' -------------------------------------------------------------------------------- /lib/opal/phaser/time/time.rb: -------------------------------------------------------------------------------- 1 | module Phaser 2 | class Time 3 | include Native 4 | 5 | alias_native :events, :events, as: Timer 6 | alias_native :fps 7 | alias_native :physics_elapsed, :physicsElapsed 8 | alias_native :physics_elapsed_ms, :physicsElapsedMS 9 | alias_native :now 10 | 11 | def advanced_timing=(bool) 12 | `#@native.advancedTiming = bool` 13 | end 14 | end 15 | end 16 | -------------------------------------------------------------------------------- /lib/opal/phaser/time/timer.rb: -------------------------------------------------------------------------------- 1 | module Phaser 2 | class Timer 3 | include Native 4 | 5 | SECOND = `Phaser.Timer.SECOND` 6 | 7 | alias_native :loop, :loop, as: TimerEvent 8 | alias_native :start 9 | alias_native :add 10 | end 11 | end 12 | -------------------------------------------------------------------------------- /lib/opal/phaser/time/timer_event.rb: -------------------------------------------------------------------------------- 1 | module Phaser 2 | class TimerEvent 3 | include Native 4 | 5 | alias_native :start 6 | alias_native :timer 7 | end 8 | end -------------------------------------------------------------------------------- /lib/opal/phaser/tween/easing.rb: -------------------------------------------------------------------------------- 1 | module Phaser 2 | module Easing 3 | def self.Default 4 | `Phaser.Easing.Default` 5 | end 6 | 7 | module Quadratic 8 | def self.In 9 | `Phaser.Easing.Quadratic.In` 10 | end 11 | 12 | def self.InOut 13 | `Phaser.Easing.Quadratic.InOut` 14 | end 15 | 16 | def self.Out 17 | `Phaser.Easing.Quadratic.Out` 18 | end 19 | end 20 | 21 | module Linear 22 | def self.None 23 | `Phaser.Easing.Linear.None` 24 | end 25 | end 26 | end 27 | end -------------------------------------------------------------------------------- /lib/opal/phaser/tween/files.rb: -------------------------------------------------------------------------------- 1 | # These requires must be loaded in order of dependency: 2 | require 'opal/phaser/tween/tween' 3 | require 'opal/phaser/tween/easing' -------------------------------------------------------------------------------- /lib/opal/phaser/tween/tween.rb: -------------------------------------------------------------------------------- 1 | module Phaser 2 | class Tween 3 | include Native 4 | 5 | alias_native :start 6 | 7 | def to(args = {}) 8 | optional_args = {duration: 1000, ease: Phaser::Easing.Default, auto_start: false, delay: 0, repeat: 0, yoyo: false} 9 | 10 | optional_args.each do |name, default_value| 11 | unless args.include?(name) 12 | args[name] = default_value 13 | end 14 | end 15 | 16 | Tween.new `#@native.to(#{args[:properties].to_n}, #{args[:duration]}, #{args[:ease]}, #{args[:auto_start]}, #{args[:delay]}, #{args[:repeat]}, #{args[:yoyo]})` 17 | end 18 | 19 | def on(type, context = nil, &block) 20 | case type.to_sym 21 | when :child_complete 22 | `#@native.onChildComplete.add(#{block.to_n}, #{context})` 23 | when :complete 24 | `#@native.onComplete.add(#{block.to_n}, #{context})` 25 | when :loop 26 | `#@native.onLoop.add(#{block.to_n}, #{context})` 27 | when :repeat 28 | `#@native.onRepeat.add(#{block.to_n}, #{context})` 29 | when start 30 | `#@native.onStart.add(#{block.to_n}, #{context})` 31 | else 32 | raise ArgumentError, "Unrecognized event type #{type}" 33 | end 34 | end 35 | end 36 | end 37 | -------------------------------------------------------------------------------- /lib/opal/phaser/version.rb: -------------------------------------------------------------------------------- 1 | module Opal 2 | module Phaser 3 | VERSION = '0.4.6' 4 | end 5 | end 6 | -------------------------------------------------------------------------------- /opal-phaser.gemspec: -------------------------------------------------------------------------------- 1 | # -*- encoding: utf-8 -*- 2 | require File.expand_path('../lib/opal/phaser/version', __FILE__) 3 | 4 | Gem::Specification.new do |s| 5 | s.name = 'opal-phaser' 6 | s.version = Opal::Phaser::VERSION 7 | s.author = 'George Plymale' 8 | s.email = 'george@orbitalimpact.com' 9 | s.homepage = 'http://opalphaser.com' 10 | s.summary = 'Ruby access to Phaser' 11 | s.description = 'Opal wrapper library for the Phaser framework' 12 | 13 | s.files = `git ls-files`.split("\n") 14 | s.executables = `git ls-files -- bin/*`.split("\n").map { |f| File.basename(f) } 15 | s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n") 16 | s.require_paths = ['lib'] 17 | 18 | s.add_runtime_dependency 'opal', '~> 0.10.0' 19 | s.add_runtime_dependency 'opal-pixi' 20 | s.add_development_dependency 'opal-rspec' 21 | s.add_development_dependency 'yard' 22 | s.add_development_dependency 'rake' 23 | end 24 | -------------------------------------------------------------------------------- /spec/core/loader_spec.rb: -------------------------------------------------------------------------------- 1 | require 'spec_helper' 2 | 3 | describe Phaser::Loader do 4 | let!(:game) { Phaser::Game.new } 5 | context "#async_image" do 6 | let(:load) { Phaser::Loader.new(game) } 7 | 8 | it "raises error without key argument" do 9 | expect { load.async_image }.to raise_error(ArgumentError, /key/) 10 | end 11 | 12 | it "raises error without url argument" do 13 | expect { load.async_image }.to raise_error(ArgumentError, /key/) 14 | end 15 | 16 | async "it loads image" do 17 | load.async_image({ key: "test", url: "common/images/logo.png"}) do 18 | run_async { expect(game.cache.check_image("test")).to be_truthy } 19 | end 20 | end 21 | end 22 | end 23 | -------------------------------------------------------------------------------- /spec/core/math_spec.rb: -------------------------------------------------------------------------------- 1 | require 'spec_helper' 2 | 3 | describe "Phaser::Math" do 4 | let!(:game) { Phaser::Game.new } 5 | 6 | it "PI2" do 7 | expect(game.math.pi2).to eq(2 * Math::PI) 8 | end 9 | 10 | # This is between points, not vectors 11 | # 0 12 | # | 13 | # | 14 | #-0.5---*--- 0.5 15 | # | 16 | # | 17 | # | 18 | # 1 19 | it "angle_between" do 20 | expect(game.math.angle_between(1,1,2,1)).to eq(0) 21 | expect(game.math.angle_between(1,1,1,2)).to eq(Math::PI/2) 22 | expect(game.math.angle_between(3,3,4,4)).to eq(Math::PI/4) 23 | expect(game.math.angle_between(2,1,1,1)).to eq(Math::PI) 24 | expect(game.math.angle_between(1,2,1,1)).to eq(-Math::PI/2) 25 | expect(game.math.angle_between(0,0,-7,-7)).to eq(-3*Math::PI/4) 26 | end 27 | 28 | it "angle_between_points" do 29 | expect(game.math.angle_between_points({x:1,y:2}, {x:3,y:4})).to eq( 30 | game.math.angle_between(1,2,3,4)) 31 | end 32 | 33 | # 0.5 34 | # | 35 | # | 36 | # 1 ---*--- 0 37 | # | 38 | # | 39 | # | 40 | # -0.5 41 | it "angle_between_y" do 42 | expect(game.math.angle_between_y(1,1,1,2)).to eq(0) 43 | expect(game.math.angle_between_y(1,1,2,1)).to eq(Math::PI/2) 44 | expect(game.math.angle_between_y(3,3,4,4)).to eq(Math::PI/4) 45 | expect(game.math.angle_between_y(1,2,1,1)).to eq(Math::PI) 46 | expect(game.math.angle_between_y(2,1,1,1)).to eq(-Math::PI/2) 47 | expect(game.math.angle_between_y(0,0,-7,-7)).to eq(-3*Math::PI/4) 48 | end 49 | 50 | it "angle_between_points_y" do 51 | expect(game.math.angle_between_points_y({x:1,y:2}, {x:3,y:4})).to eq( 52 | game.math.angle_between_y(1,2,3,4)) 53 | end 54 | 55 | it "average" do 56 | expect(game.math.average(1,3,11)).to eq(5) 57 | end 58 | 59 | it "bezier_interpolation" do 60 | expect(game.math.bezier_interpolation([10,20,40], 0)).to eq(10) 61 | expect(game.math.bezier_interpolation([10,20,40], 0.5)).to eq(22.5) 62 | expect(game.math.bezier_interpolation([10,20,40], 1)).to eq(40) 63 | end 64 | 65 | it "catmull_rom_interpolation" do 66 | expect(game.math.catmull_rom_interpolation([10,20,40], 0)).to eq(10) 67 | expect(game.math.catmull_rom_interpolation([10,20,40], 0.5)).to eq(20) 68 | expect(game.math.catmull_rom_interpolation([10,20,40], 1)).to eq(40) 69 | end 70 | 71 | it "ceil_to" do 72 | expect(game.math.ceil_to(3.14159)).to eq(4) 73 | expect(game.math.ceil_to(3.14159, -2)).to eq(3.15) 74 | expect(game.math.ceil_to(3.14159, -1, 16)).to eq(3.1875) 75 | expect(game.math.ceil_to(1234,3)).to eq(2000) 76 | expect(game.math.ceil_to(1234,2,16)).to eq(0x500) 77 | end 78 | 79 | it "chance_roll" do 80 | expect(game.math.chance_roll(100)).to eq(true) 81 | expect(game.math.chance_roll(0)).to eq(false) 82 | end 83 | 84 | it "clamp" do 85 | expect(game.math.clamp(1,2,3)).to eq(2) 86 | expect(game.math.clamp(2.5,2,3)).to eq(2.5) 87 | expect(game.math.clamp(5,2,3)).to eq(3) 88 | end 89 | 90 | it "clamp_bottom" do 91 | expect(game.math.clamp_bottom(1,2)).to eq(2) 92 | expect(game.math.clamp_bottom(2.5,2)).to eq(2.5) 93 | end 94 | 95 | it "deg_to_rad" do 96 | expect(game.math.deg_to_rad(90)).to eq(Math::PI/2) 97 | end 98 | 99 | it "difference" do 100 | expect(game.math.difference(3,5)).to eq(2) 101 | expect(game.math.difference(5,3)).to eq(2) 102 | end 103 | 104 | it "distance" do 105 | expect(game.math.distance(3,0,0,4)).to eq(5) 106 | end 107 | 108 | it "distance_pow" do 109 | # Note this is not correct implementation of p-norm 110 | # https://en.wikipedia.org/wiki/Norm_(mathematics)#p-norm 111 | expect(game.math.distance_pow(3,0,0,4)).to eq(5) 112 | expect(game.math.distance_pow(3,0,0,4,2)).to eq(5) 113 | expect(game.math.distance_pow(3,0,0,4,4)).to eq(Math.sqrt(3**4 + 4**4)) 114 | end 115 | 116 | # That's in 2.4+ 117 | # it "distance_sq" do 118 | # expect(game.math.distance_sq(3,0,0,4)).to eq(25) 119 | # end 120 | 121 | it "factorial" do 122 | expect(game.math.factorial(5)).to eq(5*4*3*2*1) 123 | end 124 | 125 | it "floor_to" do 126 | expect(game.math.floor_to(3.14159)).to eq(3) 127 | expect(game.math.floor_to(3.14159, -2)).to eq(3.14) 128 | expect(game.math.floor_to(3.14159, -1, 16)).to eq(3.125) 129 | expect(game.math.floor_to(1234,3)).to eq(1000) 130 | expect(game.math.floor_to(1234,2,16)).to eq(1024) 131 | end 132 | 133 | it "fuzzy_ceil" do 134 | expect(game.math.fuzzy_ceil(6.990, 0.01)).to eq(7) 135 | expect(game.math.fuzzy_ceil(7.001, 0.01)).to eq(7) 136 | expect(game.math.fuzzy_ceil(7.201, 0.01)).to eq(8) 137 | end 138 | 139 | it "fuzzy_equal" do 140 | expect(game.math.fuzzy_equal(7.001, 6.998, 0.01)).to eq(true) 141 | expect(game.math.fuzzy_equal(7.001, 6.998, 0.001)).to eq(false) 142 | end 143 | 144 | it "fuzzy_floor" do 145 | expect(game.math.fuzzy_floor(8.001, 0.01)).to eq(8) 146 | expect(game.math.fuzzy_floor(6.999, 0.01)).to eq(7) 147 | expect(game.math.fuzzy_floor(4.001, 0.01)).to eq(4) 148 | expect(game.math.fuzzy_floor(2.201, 0.01)).to eq(2) 149 | end 150 | 151 | it "fuzzy_greater_than" do 152 | expect(game.math.fuzzy_greater_than(7.01, 6.58, 0.05)).to eq(true) 153 | expect(game.math.fuzzy_greater_than(7.01, 7.00, 0.05)).to eq(true) 154 | expect(game.math.fuzzy_greater_than(7.01, 7.02, 0.05)).to eq(true) 155 | expect(game.math.fuzzy_greater_than(7.01, 7.24, 0.05)).to eq(false) 156 | end 157 | 158 | it "fuzzy_less_than" do 159 | expect(game.math.fuzzy_less_than(6.58, 7.01, 0.05)).to eq(true) 160 | expect(game.math.fuzzy_less_than(7.00, 7.01, 0.05)).to eq(true) 161 | expect(game.math.fuzzy_less_than(7.02, 7.01, 0.05)).to eq(true) 162 | expect(game.math.fuzzy_less_than(7.24, 7.01, 0.05)).to eq(false) 163 | end 164 | 165 | it "even?" do 166 | expect(game.math.even?(4)).to eq(true) 167 | expect(game.math.even?(5)).to eq(false) 168 | # expect(game.math.even?(4.5)).to eq(false) 169 | end 170 | 171 | it "odd?" do 172 | expect(game.math.odd?(4)).to eq(false) 173 | expect(game.math.odd?(5)).to eq(true) 174 | # expect(game.math.odd?(4.5)).to eq(false) 175 | end 176 | 177 | it "linear" do 178 | expect(game.math.linear(10,20,0.3)).to eq(13) 179 | expect(game.math.linear(10,20,1.7)).to eq(27) 180 | end 181 | 182 | it "linear_interpolation" do 183 | expect(game.math.linear_interpolation([10,20,40], 0)).to eq(10) 184 | expect(game.math.linear_interpolation([10,20,40], 0.25)).to eq(15) 185 | expect(game.math.linear_interpolation([10,20,40], 0.5)).to eq(20) 186 | expect(game.math.linear_interpolation([10,20,40], 1)).to eq(40) 187 | # Interpolation continues past end points 188 | expect(game.math.linear_interpolation([10,20,40], -1)).to eq(-10) 189 | expect(game.math.linear_interpolation([10,20,40], 2)).to eq(80) 190 | end 191 | 192 | it "map_linear" do 193 | expect(game.math.map_linear(13,10,20,100,200)).to eq(130) 194 | end 195 | 196 | it "max" do 197 | expect(game.math.max(1,7,2)).to eq(7) 198 | end 199 | 200 | it "max_add" do 201 | expect(game.math.max_add(5,3,10)).to eq(8) 202 | expect(game.math.max_add(5,7,10)).to eq(10) 203 | end 204 | 205 | it "max_property" do 206 | expect(game.math.max_property(:x, [{x:2, y:7}, {x:3, y:4}])).to eq(3) 207 | expect(game.math.max_property(:y, [{x:2, y:7}, {x:3, y:4}])).to eq(7) 208 | end 209 | 210 | it "min" do 211 | expect(game.math.min(2,7,1)).to eq(1) 212 | end 213 | 214 | it "min_property" do 215 | expect(game.math.min_property(:x, [{x:2, y:7}, {x:3, y:4}])).to eq(2) 216 | expect(game.math.min_property(:y, [{x:2, y:7}, {x:3, y:4}])).to eq(4) 217 | end 218 | 219 | it "min_sub" do 220 | expect(game.math.min_sub(15,3,10)).to eq(12) 221 | expect(game.math.min_sub(15,7,10)).to eq(10) 222 | end 223 | 224 | it "normalize_angle" do 225 | expect(game.math.normalize_angle(1.40*Math::PI, true)).to eq(1.40*Math::PI) 226 | expect(game.math.normalize_angle(-0.20*Math::PI, true)).to eq(1.80*Math::PI) 227 | expect(game.math.normalize_angle(2.70*Math::PI, true)).to eq(0.70*Math::PI) 228 | end 229 | 230 | it "percent" do 231 | expect(game.math.percent(10,40)).to eq(0.25) 232 | expect(game.math.percent(-10,40)).to eq(0.00) 233 | expect(game.math.percent(60,40)).to eq(1.00) 234 | expect(game.math.percent(30,50,10)).to eq(0.4) 235 | expect(game.math.percent(5,50,10)).to eq(0) 236 | expect(game.math.percent(-20,40,10)).to eq(0.00) 237 | expect(game.math.percent(70,40,10)).to eq(1.00) 238 | end 239 | 240 | it "rad_to_deg" do 241 | expect(game.math.rad_to_deg(0)).to eq(0) 242 | expect(game.math.rad_to_deg(Math::PI/2)).to eq(90) 243 | end 244 | 245 | it "reverse_angle" do 246 | expect(game.math.reverse_angle(0)).to eq(Math::PI) 247 | expect(game.math.reverse_angle(1)).to eq(Math::PI+1) 248 | end 249 | 250 | it "round_away_from_zero" do 251 | expect(game.math.round_away_from_zero(3.14159)).to eq(4) 252 | expect(game.math.round_away_from_zero(-3.14159)).to eq(-4) 253 | end 254 | 255 | it "round_to" do 256 | expect(game.math.round_to(3.14159)).to eq(3) 257 | expect(game.math.round_to(3.14159,-2)).to eq(3.14) 258 | expect(game.math.round_to(3.14159,-1,16)).to eq(3.125) 259 | expect(game.math.round_to(972,3)).to eq(1000) 260 | expect(game.math.round_to(972,2,16)).to eq(0x400) 261 | end 262 | 263 | it "shear" do 264 | expect(game.math.shear(7.12).round(6)).to eq(0.12) 265 | # Why not 0.91? 266 | expect(game.math.shear(-2.09).round(6)).to eq(-0.09) 267 | end 268 | 269 | it "sign" do 270 | expect(game.math.sign(5)).to eq(1) 271 | expect(game.math.sign(0)).to eq(0) 272 | expect(game.math.sign(-5)).to eq(-1) 273 | end 274 | 275 | it "sin_cos_generator" do 276 | # This is not sensible API use example, it is unstable at such low sample rates, 277 | # but it fits test size better 278 | expect(game.math.sin_cos_generator(4, 10, 20, 3)).to eq({ 279 | sin: [1.607365047719263, -15.70880205094555, 54.184840645432374, -176.73692136808745], 280 | cos: [-3.5619449019234466, -7.3492095710873295, 29.663783268872784, -98.00623971184518], 281 | length:4 282 | }) 283 | end 284 | 285 | it "smootherstep" do 286 | expect(game.math.smootherstep(7,10,20)).to eq(0) 287 | expect(game.math.smootherstep(27,10,20)).to eq(1) 288 | expect(game.math.smootherstep(15,10,20)).to eq(0.5) 289 | expect(game.math.smootherstep(11,10,20).round(6)).to eq(0.00856) 290 | expect(game.math.smootherstep(12,10,20).round(6)).to eq(0.05792) 291 | end 292 | 293 | it "smoothstep" do 294 | expect(game.math.smoothstep(7,10,20)).to eq(0) 295 | expect(game.math.smoothstep(27,10,20)).to eq(1) 296 | expect(game.math.smoothstep(15,10,20)).to eq(0.5) 297 | expect(game.math.smoothstep(11,10,20).round(5)).to eq(0.028) 298 | end 299 | 300 | it "snap_to" do 301 | expect(game.math.snap_to(20, 10)).to eq(20) 302 | expect(game.math.snap_to(17, 10)).to eq(20) 303 | expect(game.math.snap_to(11, 10)).to eq(10) 304 | expect(game.math.snap_to(20, 10, 5)).to eq(25) 305 | expect(game.math.snap_to(17, 10, 5)).to eq(15) 306 | expect(game.math.snap_to(11, 10, 5)).to eq(15) 307 | end 308 | 309 | it "snap_to_ceil" do 310 | expect(game.math.snap_to_ceil(20, 10)).to eq(20) 311 | expect(game.math.snap_to_ceil(17, 10)).to eq(20) 312 | expect(game.math.snap_to_ceil(20, 10, 5)).to eq(25) 313 | expect(game.math.snap_to_ceil(17, 10, 5)).to eq(25) 314 | end 315 | 316 | it "snap_to_floor" do 317 | expect(game.math.snap_to_floor(20, 10)).to eq(20) 318 | expect(game.math.snap_to_floor(17, 10)).to eq(10) 319 | expect(game.math.snap_to_floor(20, 10, 5)).to eq(15) 320 | expect(game.math.snap_to_floor(17, 10, 5)).to eq(15) 321 | end 322 | 323 | it "within" do 324 | expect(game.math.within(7.01, 7.02, 0.05)).to eq(true) 325 | expect(game.math.within(7.01, 7.22, 0.05)).to eq(false) 326 | end 327 | 328 | it "wrap" do 329 | expect(game.math.wrap(72,10,20)).to eq(12) 330 | expect(game.math.wrap(-6,10,20)).to eq(14) 331 | end 332 | 333 | it "wrap_angle" do 334 | expect(game.math.wrap_angle(130)).to eq(130) 335 | expect(game.math.wrap_angle(290)).to eq(-70) 336 | expect(game.math.wrap_angle(290, false)).to eq(-70) 337 | expect(game.math.wrap_angle(1.40*Math::PI, true)).to eq(-0.60*Math::PI) 338 | expect(game.math.wrap_angle(0.20*Math::PI, true)).to eq(0.20*Math::PI) 339 | end 340 | 341 | it "wrap_value" do 342 | expect(game.math.wrap_value(1,4,10)).to eq(5) 343 | expect(game.math.wrap_value(8,4,10)).to eq(2) 344 | end 345 | end 346 | -------------------------------------------------------------------------------- /spec/html/index.html.erb: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | <%= javascript_include_tag 'html/phaser' %> 7 | <%= javascript_include_tag @server.main %> 8 | 9 | 10 | -------------------------------------------------------------------------------- /spec/spec_helper.rb: -------------------------------------------------------------------------------- 1 | require 'opal-rspec' 2 | require 'opal-phaser' 3 | 4 | --------------------------------------------------------------------------------