diff --git a/examples/lookWithMouse.js b/examples/lookWithMouse.js index f404019bc1..460663c054 100644 --- a/examples/lookWithMouse.js +++ b/examples/lookWithMouse.js @@ -9,29 +9,36 @@ // // +var alwaysLook = true; // if you want the mouse look to happen only when you click, change this to false var isMouseDown = false; var lastX = 0; var lastY = 0; var yawFromMouse = 0; var pitchFromMouse = 0; +var wantDebugging = false; function mousePressEvent(event) { - print("mousePressEvent event.x,y=" + event.x + ", " + event.y); + if (wantDebugging) { + print("mousePressEvent event.x,y=" + event.x + ", " + event.y); + } isMouseDown = true; lastX = event.x; lastY = event.y; } function mouseReleaseEvent(event) { - print("mouseReleaseEvent event.x,y=" + event.x + ", " + event.y); + if (wantDebugging) { + print("mouseReleaseEvent event.x,y=" + event.x + ", " + event.y); + } isMouseDown = false; } function mouseMoveEvent(event) { - print("mouseMoveEvent event.x,y=" + event.x + ", " + event.y); + if (wantDebugging) { + print("mouseMoveEvent event.x,y=" + event.x + ", " + event.y); + } - if (isMouseDown) { - print("isMouseDown... attempting to change pitch..."); + if (alwaysLook || isMouseDown) { var MOUSE_YAW_SCALE = -0.25; var MOUSE_PITCH_SCALE = -12.5; var FIXED_MOUSE_TIMESTEP = 0.016; @@ -43,12 +50,26 @@ function mouseMoveEvent(event) { } function update() { + if (wantDebugging) { + print("update()..."); + } // rotate body yaw for yaw received from mouse - MyAvatar.orientation = Quat.multiply(MyAvatar.orientation, Quat.fromVec3( { x: 0, y: yawFromMouse, z: 0 } )); + var newOrientation = Quat.multiply(MyAvatar.orientation, Quat.fromVec3( { x: 0, y: yawFromMouse, z: 0 } )); + if (wantDebugging) { + print("changing orientation" + + " [old]MyAvatar.orientation="+MyAvatar.orientation.x + "," + MyAvatar.orientation.y + "," + + MyAvatar.orientation.z + "," + MyAvatar.orientation.w + + " newOrientation="+newOrientation.x + "," + newOrientation.y + "," + newOrientation.z + "," + newOrientation.w); + } + MyAvatar.orientation = newOrientation; yawFromMouse = 0; // apply pitch from mouse - MyAvatar.headPitch = MyAvatar.headPitch + pitchFromMouse; + var newPitch = MyAvatar.headPitch + pitchFromMouse; + if (wantDebugging) { + print("changing pitch [old]MyAvatar.headPitch="+MyAvatar.headPitch+ " newPitch="+newPitch); + } + MyAvatar.headPitch = newPitch; pitchFromMouse = 0; } diff --git a/examples/lookWithTouch.js b/examples/lookWithTouch.js new file mode 100644 index 0000000000..bb54a055c4 --- /dev/null +++ b/examples/lookWithTouch.js @@ -0,0 +1,86 @@ +// +// lookWithTouch.js +// hifi +// +// Created by Brad Hefta-Gaub on 1/28/14. +// Copyright (c) 2014 HighFidelity, Inc. All rights reserved. +// +// This is an example script that demonstrates use of the Controller class +// +// + +var lastX = 0; +var lastY = 0; +var yawFromMouse = 0; +var pitchFromMouse = 0; +var wantDebugging = false; + +function touchBeginEvent(event) { + if (wantDebugging) { + print("touchBeginEvent event.x,y=" + event.x + ", " + event.y); + } + lastX = event.x; + lastY = event.y; +} + +function touchEndEvent(event) { + if (wantDebugging) { + print("touchEndEvent event.x,y=" + event.x + ", " + event.y); + } +} + +function touchUpdateEvent(event) { + if (wantDebugging) { + print("touchUpdateEvent event.x,y=" + event.x + ", " + event.y); + } + + var MOUSE_YAW_SCALE = -0.25; + var MOUSE_PITCH_SCALE = -12.5; + var FIXED_MOUSE_TIMESTEP = 0.016; + yawFromMouse += ((event.x - lastX) * MOUSE_YAW_SCALE * FIXED_MOUSE_TIMESTEP); + pitchFromMouse += ((event.y - lastY) * MOUSE_PITCH_SCALE * FIXED_MOUSE_TIMESTEP); + lastX = event.x; + lastY = event.y; +} + +function update() { + // rotate body yaw for yaw received from mouse + var newOrientation = Quat.multiply(MyAvatar.orientation, Quat.fromVec3( { x: 0, y: yawFromMouse, z: 0 } )); + if (wantDebugging) { + print("changing orientation" + + " [old]MyAvatar.orientation="+MyAvatar.orientation.x + "," + MyAvatar.orientation.y + "," + + MyAvatar.orientation.z + "," + MyAvatar.orientation.w + + " newOrientation="+newOrientation.x + "," + newOrientation.y + "," + newOrientation.z + "," + newOrientation.w); + } + MyAvatar.orientation = newOrientation; + yawFromMouse = 0; + + // apply pitch from mouse + var newPitch = MyAvatar.headPitch + pitchFromMouse; + if (wantDebugging) { + print("changing pitch [old]MyAvatar.headPitch="+MyAvatar.headPitch+ " newPitch="+newPitch); + } + MyAvatar.headPitch = newPitch; + pitchFromMouse = 0; +} + +// Map the mouse events to our functions +Controller.touchBeginEvent.connect(touchBeginEvent); +Controller.touchUpdateEvent.connect(touchUpdateEvent); +Controller.touchEndEvent.connect(touchEndEvent); + +// disable the standard application for mouse events +Controller.captureTouchEvents(); + +function scriptEnding() { + // re-enabled the standard application for mouse events + Controller.releaseTouchEvents(); +} + +MyAvatar.bodyYaw = 0; +MyAvatar.bodyPitch = 0; +MyAvatar.bodyRoll = 0; + +// would be nice to change to update +Script.willSendVisualDataCallback.connect(update); +Script.scriptEnding.connect(scriptEnding); diff --git a/examples/spaceInvadersExample.js b/examples/spaceInvadersExample.js new file mode 100644 index 0000000000..c575cbcd76 --- /dev/null +++ b/examples/spaceInvadersExample.js @@ -0,0 +1,224 @@ +// +// spaceInvadersExample.js +// hifi +// +// Created by Brad Hefta-Gaub on 1/30/14. +// Copyright (c) 2014 HighFidelity, Inc. All rights reserved. +// +// This is an example script that demonstrates a simple space invaders style of game +// + +var iteration = 0; + +var gameAt = { x: 10, y: 0, z: 10 }; +var gameSize = { x: 10, y: 20, z: 1 }; +var middleX = gameAt.x + (gameSize.x/2); +var middleY = gameAt.y + (gameSize.y/2); + +var shipSize = 0.2; +var missileSize = 0.1; +var myShip; +var myShipProperties; + +// create the rows of space invaders +var invaders = new Array(); +var numberOfRows = 5; +var invadersPerRow = 8; +var emptyColumns = 2; // number of invader width columns not filled with invaders +var invadersBottomCorner = { x: gameAt.x, y: middleY , z: gameAt.z }; +var rowHeight = ((gameAt.y + gameSize.y) - invadersBottomCorner.y) / numberOfRows; +var columnWidth = gameSize.x / (invadersPerRow + emptyColumns); + +var missileFired = false; +var myMissile; + +function initializeMyShip() { + myShipProperties = { + position: { x: middleX , y: gameAt.y, z: gameAt.z }, + velocity: { x: 0, y: 0, z: 0 }, + gravity: { x: 0, y: 0, z: 0 }, + damping: 0, + radius: shipSize, + color: { red: 0, green: 255, blue: 0 }, + lifetime: 60 * 5 // 5 minutes + }; + myShip = Particles.addParticle(myShipProperties); +} + +function initializeInvaders() { + for (var row = 0; row < numberOfRows; row++) { + invaders[row] = new Array(); + for (var column = 0; column < invadersPerRow; column++) { + invaderPosition = { + x: invadersBottomCorner.x + (column * columnWidth), + y: invadersBottomCorner.y + (row * rowHeight), + z: invadersBottomCorner.z }; + + + invaders[row][column] = Particles.addParticle({ + position: invaderPosition, + velocity: { x: 0, y: 0, z: 0 }, + gravity: { x: 0, y: 0, z: 0 }, + damping: 0, + radius: shipSize, + color: { red: 255, green: 0, blue: 0 }, + lifetime: 60 * 5 // 5 minutes + }); + + print("invaders[row][column].creatorTokenID=" + invaders[row][column].creatorTokenID); + } + } +} + +var invadersMovingRight = true; +function moveInvaders() { + print("moveInvaders()..."); + if (invadersMovingRight) { + invaderMoveX = 0.05; + } else { + invaderMoveX = -0.05; + } + for (var row = 0; row < numberOfRows; row++) { + for (var column = 0; column < invadersPerRow; column++) { + props = Particles.getParticleProperties(invaders[row][column]); + if (props.isKnownID) { + newPosition = { x: props.position.x + invaderMoveX, + y: props.position.y, + z: props.position.z }; + Particles.editParticle(invaders[row][column], { position: newPosition }); + } + } + } +} + +function update() { + print("updating space invaders... iteration="+iteration); + iteration++; + if ((iteration % 60) == 0) { + invadersMovingRight = !invadersMovingRight; + } + moveInvaders(); +} + +// register the call back so it fires before each data send +Agent.willSendVisualDataCallback.connect(update); + +function endGame() { + print("ending game..."); + Particles.deleteParticle(myShip); + print("endGame() ... Particles.deleteParticle(myShip)... myShip.id="+myShip.id); + for (var row = 0; row < numberOfRows; row++) { + for (var column = 0; column < invadersPerRow; column++) { + Particles.deleteParticle(invaders[row][column]); + print("endGame() ... Particles.deleteParticle(invaders[row][column])... invaders[row][column].id="+invaders[row][column].id); + } + } + + // clean up our missile + if (missileFired) { + Particles.deleteParticle(myMissile); + } + Agent.stop(); +} + +function moveShipTo(position) { + myShip = Particles.identifyParticle(myShip); + Particles.editParticle(myShip, { position: position }); +} + +function fireMissile() { + // we only allow one missile at a time... + var canFire = false; + + // If we've fired a missile, then check to see if it's still alive + if (missileFired) { + var missileProperties = Particles.getParticleProperties(myMissile); + print("missileProperties.isKnownID=" + missileProperties.isKnownID); + + if (!missileProperties.isKnownID) { + print("canFire = true"); + canFire = true; + } + } else { + canFire = true; + } + + if (canFire) { + print("firing missile"); + var missilePosition = { x: myShipProperties.position.x, + y: myShipProperties.position.y + (2* shipSize), + z: myShipProperties.position.z }; + + myMissile = Particles.addParticle( + { + position: missilePosition, + velocity: { x: 0, y: 5, z: 0}, + gravity: { x: 0, y: 0, z: 0 }, + damping: 0, + radius: missileSize, + color: { red: 0, green: 0, blue: 255 }, + lifetime: 5 + }); + + missileFired = true; + } +} + +function keyPressEvent(key) { + //print("keyPressEvent key.text="+key.text); + if (key.text == ",") { + myShipProperties.position.x -= 0.1; + if (myShipProperties.position.x < gameAt.x) { + myShipProperties.position.x = gameAt.x; + } + moveShipTo(myShipProperties.position); + } else if (key.text == ".") { + myShipProperties.position.x += 0.1; + if (myShipProperties.position.x > gameAt.x + gameSize.x) { + myShipProperties.position.x = gameAt.x + gameSize.x; + } + moveShipTo(myShipProperties.position); + } else if (key.text == " ") { + fireMissile(); + } else if (key.text == "q") { + endGame(); + } +} + +// remap the keys... +Controller.keyPressEvent.connect(keyPressEvent); +Controller.captureKeyEvents({text: " "}); + +function deleteIfInvader(possibleInvaderParticle) { + for (var row = 0; row < numberOfRows; row++) { + for (var column = 0; column < invadersPerRow; column++) { + invaders[row][column] = Particles.identifyParticle(invaders[row][column]); + if (invaders[row][column].isKnownID) { + if (invaders[row][column].id == possibleInvaderParticle.id) { + Particles.deleteParticle(possibleInvaderParticle); + Particles.deleteParticle(myMissile); + } + } + } + } +} + +function particleCollisionWithParticle(particleA, particleB) { + print("particleCollisionWithParticle() a.id="+particleA.id + " b.id=" + particleB.id); + if (missileFired) { + myMissile = Particles.identifyParticle(myMissile); + if (myMissile.id == particleA.id) { + deleteIfInvader(particleB); + } else if (myMissile.id == particleB.id) { + deleteIfInvader(particleA); + } + } +} +Particles.particleCollisionWithParticle.connect(particleCollisionWithParticle); + + +// initialize the game... +initializeMyShip(); +initializeInvaders(); + +