Permalink
Browse files

added sample 8 with collision detection. waves can now be run from co…

…mmand line
  • Loading branch information...
1 parent c18f52a commit 5ce89052abb812ef10886821bf8300cf3412887d @jwoertink committed Oct 1, 2011
Showing with 129 additions and 12 deletions.
  1. +2 −1 .gitignore
  2. +6 −11 README.rdoc
  3. BIN assets/town.zip
  4. +10 −0 bin/waves
  5. +111 −0 lib/sample8.rb
View
@@ -7,4 +7,5 @@ pkg
.DS_Store
*.tmproj
tmtags
-Gemfile.lock
+Gemfile.lock
+*.gemspec
View
@@ -4,18 +4,13 @@ This is just some JRuby(http://jruby.org/) and jMonkeyEngine(http://jmonkeyengin
Currently these are all samples from the jMonkeyEngine Hello tutorials re-written in JRuby. There are a few small modifications.
== Running
- rake console
- require 'waves'
- Waves.start
-
-You can view the different samples with
- Waves.start("2")
- Waves.start("3")
+The main application can be run by
+ jruby bin/waves
-sample1 is loaded by default.
-
-You can see all the sample names with
- Waves.samples
+You can view other samples by passing the sample number as an argument
+ jruby bin/waves 8
+
+Normal camera operations use W A S D to move. To exit just hit esc.
== Requirements
OSX (Because I haven't tried this on anything else)
View
Binary file not shown.
View
@@ -0,0 +1,10 @@
+#!/usr/bin/ruby
+# -*- coding: utf-8 -*-
+
+require 'rubygems'
+$:.unshift(File.join(File.dirname(__FILE__), "..", "lib"))
+require 'waves'
+
+sample = ARGV[0] || "1"
+
+Waves.start(sample)
View
@@ -0,0 +1,111 @@
+java_import "com.jme3.app.SimpleApplication"
+java_import "com.jme3.asset.plugins.ZipLocator"
+java_import "com.jme3.bullet.BulletAppState"
+java_import "com.jme3.bullet.collision.shapes.CapsuleCollisionShape"
+java_import "com.jme3.bullet.collision.shapes.CollisionShape"
+java_import "com.jme3.bullet.control.CharacterControl"
+java_import "com.jme3.bullet.control.RigidBodyControl"
+java_import "com.jme3.bullet.util.CollisionShapeFactory"
+java_import "com.jme3.input.KeyInput"
+java_import "com.jme3.input.controls.ActionListener"
+java_import "com.jme3.input.controls.KeyTrigger"
+java_import "com.jme3.light.AmbientLight"
+java_import "com.jme3.light.DirectionalLight"
+java_import "com.jme3.math.ColorRGBA"
+java_import "com.jme3.math.Vector3f"
+java_import "com.jme3.scene.Node"
+java_import "com.jme3.scene.Spatial"
+
+class Sample8 < SimpleApplication
+ include ActionListener
+
+ field_reader :cam
+ field_accessor :flyCam
+ attr_accessor :scene_model, :bullet_app_state, :landscape, :player
+
+ def initialize
+ @walk_direction = Vector3f.new
+ @left = false
+ @right = false
+ @up = false
+ @down = false
+ end
+
+ def simpleInitApp
+ self.bullet_app_state = BulletAppState.new
+ state_manager.attach(bullet_app_state)
+ view_port.background_color = ColorRGBA.new(0.7, 0.8, 1.0, 1.0)
+ flyCam.move_speed = 100
+ asset_manager.register_locator(File.join("assets", "town.zip"), ZipLocator.java_class.name)
+ self.scene_model = asset_manager.load_model("main.scene")
+ scene_model.local_scale = 2.0
+ scene_shape = CollisionShapeFactory.create_mesh_shape(scene_model)
+ self.landscape = RigidBodyControl.new(scene_shape, 0)
+ scene_model.add_control(landscape)
+ capsule_shape = CapsuleCollisionShape.new(1.5, 6.0, 1)
+ self.player = CharacterControl.new(capsule_shape, 0.05)
+ player.jump_speed = 20
+ player.fall_speed = 30
+ player.gravity = 30
+ player.physics_location = Vector3f.new(0, 10, 0)
+ root_node.attach_child(scene_model)
+ bullet_app_state.physics_space.add(landscape)
+ bullet_app_state.physics_space.add(player)
+ bullet_app_state.physics_space.enable_debug(asset_manager)
+ setup_keys!
+ setup_light!
+ end
+
+ def setup_keys!
+ input_manager.add_mapping("Left", KeyTrigger.new(KeyInput::KEY_A))
+ input_manager.add_mapping("Right", KeyTrigger.new(KeyInput::KEY_D))
+ input_manager.add_mapping("Up", KeyTrigger.new(KeyInput::KEY_W))
+ input_manager.add_mapping("Down", KeyTrigger.new(KeyInput::KEY_S))
+ input_manager.add_mapping("Jump", KeyTrigger.new(KeyInput::KEY_SPACE))
+ input_manager.add_listener(ControllerAction.new(self), "Left")
+ input_manager.add_listener(ControllerAction.new(self), "Right")
+ input_manager.add_listener(ControllerAction.new(self), "Up")
+ input_manager.add_listener(ControllerAction.new(self), "Down")
+ input_manager.add_listener(ControllerAction.new(self), "Jump")
+ end
+
+ def setup_light!
+ al = AmbientLight.new
+ al.color = ColorRGBA::White.mult(1.3)
+ root_node.add_light(al)
+ dl = DirectionalLight.new
+ dl.color = ColorRGBA::White
+ dl.direction = Vector3f.new(2.8, -2.8, -2.8).normalize_local
+ root_node.add_light(dl)
+ end
+
+ def simpleUpdate(tpf)
+ cam_dir = cam.direction.clone.mult_local(0.6)
+ cam_left = cam.left.clone.mult_local(0.4)
+ @walk_direction.set(0, 0, 0)
+ @walk_direction.add_local(cam_left) if @left
+ @walk_direction.add_local(cam_left.negate) if @right
+ @walk_direction.add_local(cam_dir) if @up
+ @walk_direction.add_local(cam_dir.negate) if @down
+ player.walk_direction = @walk_direction
+ cam.location = player.physics_location
+ end
+
+ class ControllerAction
+ include ActionListener
+
+ def initialize(obj)
+ @parent = obj
+ end
+
+ def on_action(binding, value, tpf)
+ if binding == "Jump"
+ @parent.player.jump
+ else
+ @parent.instance_variable_set("@#{binding.downcase}", value)
+ end
+ end
+
+ end
+
+end

0 comments on commit 5ce8905

Please sign in to comment.