Skip to content

Commit

Permalink
Added extra functionality to Mouse.js that allows for continuous call…
Browse files Browse the repository at this point in the history
…ing of a listener if the mouse button is pressed and hold
  • Loading branch information
Mathias committed Dec 5, 2013
1 parent 4d78e7e commit 3431e79
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 17 deletions.
8 changes: 8 additions & 0 deletions ModelRender.js
Original file line number Diff line number Diff line change
Expand Up @@ -532,4 +532,12 @@ cs.ModelRender = function(gl, player) {
fps = fps || player.sequences[sequenceIndex].fps;
animationQueue.push({index: sequenceIndex, fps: fps});
};

this.forceAnimation = function(newSequenceIndex, fps) {
fps = fps || player.sequences[sequenceIndex].fps;
sequenceIndex = newSequenceIndex;
customFPS = fps;

frame = 0;
};
};
11 changes: 6 additions & 5 deletions Player.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,13 +75,14 @@ cs.Player = function(gl, x, y, z, data) {
return playerRender.render();
}

MouseJS.on("left", function(e) {
//Player is shooting. Set shooting animation
playerRender.queueAnimation(3, 125);
}, function(e) {
MouseJS.on("left", function() {
//Player is shooting. Set shooting animation and play sound
document.getElementById("sound").cloneNode(true).play();
playerRender.forceAnimation(3, 125);
}, function() {
//No longer shooting. Set idle animation once the current animation has finished
playerRender.queueAnimation(0);
});
}, 100);

KeyboardJS.on("r", function(event, keys, combo) {
//Play reload animation
Expand Down
2 changes: 2 additions & 0 deletions game.html
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
<script type="text/javascript" src="Player.js"></script>
<script type="text/javascript" src="lib/keyboard.js"></script>

<audio id="sound" src="data/sounds/ak47-1.mp3" preload="auto" autobuffer></audio>

<script type="text/javascript">
//Define namespace
window.cs = window.cs || { };
Expand Down
1 change: 0 additions & 1 deletion index.html
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
<head>
<link rel="stylesheet" type="text/css" href="stylesmenu.css">
<script type="text/javascript" src="lib/keyboard.js"></script>

<script>
function openNewGameMenu() {
var menu = document.getElementById("newGameMenu");
Expand Down
46 changes: 35 additions & 11 deletions util/Mouse.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,36 @@

var MouseJS = (function() {

//Index 1 is left, 2 is middle and 3 is right
//Listeners for mouse down and mouse up
//Index 0 is left, 1 is right and 2 is middle
var downListeners = [[], [], []];
var upListeners = [[], [], []];
//Interval IDs used for clearInterval
var intervalIds = [];

var upListener = function(e) {
var downListener = function(e) {
//If we're not currently locked we should ignore the event
if(!PointerLock.pointerLockElement()) return;
upListeners[e.which-1].forEach(function(callback) {
callback(e);
//Call listeners waiting on the down event on this mouse button
downListeners[e.which-1].forEach(function(element) {
//If an interval is specified we should call the listener continuously
if(element.interval !== "undefined") {
var intervalID = setInterval(element.callback, element.interval);
intervalIds[element.id] = intervalID;
}
element.callback();
});
};

var downListener = function(e) {
var upListener = function(e) {
if(!PointerLock.pointerLockElement()) return;
downListeners[e.which-1].forEach(function(callback) {
callback(e);
upListeners[e.which-1].forEach(function(element) {
//If we're doing a continuous call from a setInterval
if(intervalIds[element.id] !== "undefined") {
clearInterval(intervalIds[element.id]);
delete intervalIds[element.id];
}
element.callback();
});
};

Expand All @@ -32,11 +47,20 @@ var MouseJS = (function() {
right: 2
};

var id = 0;
return {
//"left", "right" or "middle"
on: function(which, down, up) {
downListeners[stringToWhich[which]].push(down);
upListeners[stringToWhich[which]].push(up);
on: function(which, down, up, cont) {
downListeners[stringToWhich[which]].push({
callback: down,
interval: cont,
id: id
});

upListeners[stringToWhich[which]].push({
callback: up,
id: id
});
++id;
}
};
})();

0 comments on commit 3431e79

Please sign in to comment.