Skip to content

Commit

Permalink
Basic physics working :) You can walk around on the blocks.
Browse files Browse the repository at this point in the history
  • Loading branch information
zzorn committed Apr 30, 2011
1 parent 347a4e3 commit c28d38c
Show file tree
Hide file tree
Showing 3 changed files with 99 additions and 22 deletions.
3 changes: 3 additions & 0 deletions src/main/scala/net/zzorn/Context.scala
@@ -1,6 +1,7 @@
package net.zzorn

import com.jme3.asset.AssetManager
import com.jme3.bullet.BulletAppState

/**
* Singleton for accessing services.
Expand All @@ -9,4 +10,6 @@ object Context {

def assetManager: AssetManager = Ludum20.getAssetManager

def physicsState: BulletAppState = Ludum20.bulletAppState

}
24 changes: 21 additions & 3 deletions src/main/scala/net/zzorn/Level.scala
Expand Up @@ -5,8 +5,12 @@ import simplex3d.math.float.functions._
import simplex3d.math.float._
import org.scalaprops.Bean
import org.scalaprops.ui.editors.SliderFactory
import utils.{XorShiftRng, RandomUtils, ShapeUtils}
import util.Random
import com.jme3.bullet.util.CollisionShapeFactory
import com.jme3.bullet.control.RigidBodyControl
import com.jme3.math.ColorRGBA
import net.zzorn.utils.VectorConversion._
import utils.{Colors, XorShiftRng, RandomUtils, ShapeUtils}


/**
Expand All @@ -15,7 +19,7 @@ import util.Random
class Level extends Bean {

val baseEditor = new SliderFactory[Float](0, 1)
val spreadEditor = new SliderFactory[Float](0, 1)
val spreadEditor = new SliderFactory[Float](0, 0.5f)

val seed = p('seed, 1234)

Expand All @@ -32,6 +36,13 @@ class Level extends Bean {
val areaY = p('areaY, 70f)
val areaZ = p('areaZ, 200f)

val skyHue = p('skyHue, 0.25f).editor(baseEditor)
val skySat = p('skySat, 0.5f).editor(baseEditor)
val skyLum = p('skyLum, 0.5f).editor(baseEditor)


def skyColor: ColorRGBA = Colors.HSLtoRGB(skyHue(), skySat(), skyLum(), 1f)

def generateSpatial(): Spatial = {
val level = new Node()

Expand All @@ -54,7 +65,14 @@ class Level extends Bean {
gaussian=true,
random = rng)

level.attachChild(ShapeUtils.createBox(pos, size, color))
val box = ShapeUtils.createBox(pos, size, color)

// Create physics collision shape
val shape = CollisionShapeFactory.createBoxShape(box)
box.addControl(new RigidBodyControl(shape, 0))
Context.physicsState.getPhysicsSpace().add(box)

level.attachChild(box)
}

level
Expand Down
94 changes: 75 additions & 19 deletions src/main/scala/net/zzorn/Ludum20.scala
Expand Up @@ -6,13 +6,24 @@ import com.jme3.material.Material
import com.jme3.math.{ColorRGBA, Vector3f}
import com.jme3.bullet.BulletAppState
import com.jme3.scene.{Spatial, Geometry}
import com.jme3.bullet.collision.shapes.CapsuleCollisionShape
import com.jme3.bullet.control.CharacterControl
import com.jme3.input.KeyInput
import com.jme3.input.controls.{ActionListener, KeyTrigger}
import com.jme3.light.{AmbientLight, DirectionalLight}

/**
*
*/
object Ludum20 extends SimpleApplication {

private val bulletAppState = new BulletAppState();
val bulletAppState = new BulletAppState()

private var left = false
private var right = false
private var up = false
private var down = false


def main(args: Array[ String ])
{
Expand All @@ -24,9 +35,11 @@ object Ludum20 extends SimpleApplication {
}

private var level: Level = new Level()
private var player: CharacterControl = null
private var levelToLoad: Level = null

private var levelNode: Spatial = null
private val walkDirection = new Vector3f()

def simpleInitApp() {

Expand All @@ -37,41 +50,55 @@ object Ludum20 extends SimpleApplication {

setupPhysics()

setupSky()

flyCam.setMoveSpeed(100)

setupLight()
//setupLight()

setupInput()

setupLandscape()

loadLevel(level)

player = setupPlayer()
//rootNode.attachChild(player)
Context.physicsState.getPhysicsSpace.add(player);
val actionListener = createActionListener(player)
setupInput(actionListener)


flyCam.setDragToRotate(true)
setPauseOnLostFocus(false)
}


override def update() {
super.update()

override def simpleUpdate(tpf: Float) {
// Load level if requested
if (levelToLoad != null) {
level = levelToLoad
levelToLoad = null
startLevel(level)
}

// Handle player movement
val camDir = cam.getDirection.clone().multLocal(0.6f)
val camLeft = cam.getLeft.clone().multLocal(0.4f)
walkDirection.set(0, 0, 0)
if (left) walkDirection.addLocal(camLeft)
if (right) walkDirection.addLocal(camLeft.negate())
if (up) walkDirection.addLocal(camDir)
if (down) walkDirection.addLocal(camDir.negate())
player.setWalkDirection(walkDirection)
cam.setLocation(player.getPhysicsLocation)
}


private def startLevel(level: Level) {
if (levelNode != null) {
rootNode.detachChild(levelNode)
}

levelNode = level.generateSpatial()

viewPort.setBackgroundColor(level.skyColor)

rootNode.attachChild(levelNode)
}

Expand All @@ -85,21 +112,50 @@ object Ludum20 extends SimpleApplication {
stateManager.attach(bulletAppState);
}

def setupSky() {
viewPort.setBackgroundColor(new ColorRGBA(0.7f, 0.8f, 1f, 1f));
}

def setupLight() {

val al = new AmbientLight()
al.setColor(ColorRGBA.White.mult(1.3f))
rootNode.addLight(al)
val dl = new DirectionalLight()
dl.setColor(ColorRGBA.White)
dl.setDirection(new Vector3f(2.8f, -2.8f, -2.8f).normalizeLocal())
rootNode.addLight(dl)
}

def setupInput() {


def setupInput(actionListener: ActionListener) {
inputManager.addMapping("Lefts", new KeyTrigger(KeyInput.KEY_A));
inputManager.addMapping("Rights", new KeyTrigger(KeyInput.KEY_D));
inputManager.addMapping("Ups", new KeyTrigger(KeyInput.KEY_W));
inputManager.addMapping("Downs", new KeyTrigger(KeyInput.KEY_S));
inputManager.addMapping("Jumps", new KeyTrigger(KeyInput.KEY_SPACE));
inputManager.addListener(actionListener, "Lefts");
inputManager.addListener(actionListener, "Rights");
inputManager.addListener(actionListener, "Ups");
inputManager.addListener(actionListener, "Downs");
inputManager.addListener(actionListener, "Jumps");
}

def setupLandscape() {
def setupPlayer(): CharacterControl = {
val capsuleShape = new CapsuleCollisionShape(0.25f, 2f, 1)
val player = new CharacterControl(capsuleShape, 0.1f)
player.setJumpSpeed(20)
player.setFallSpeed(30)
player.setGravity(30)
player.setPhysicsLocation(new Vector3f(0, 10, 0))
player
}

def createActionListener(player: CharacterControl): ActionListener = new ActionListener {
def onAction(binding: String, value: Boolean, tpf: Float) {
binding match {
case "Lefts" => left = value
case "Rights" => right = value
case "Downs" => down = value
case "Ups" => up = value
case "Jumps" => player.jump()
case _ => // Do nothing
}
}
}

}
Expand Down

0 comments on commit c28d38c

Please sign in to comment.