This will be my first Ludum Dare. I have entered three 48 hour game jams recently (the weekly Fight Magic Run competition), and won one of them,  so I’m pretty wamed up. I’ve been using createjs. The only base code that I will use that I wrote myself will probably be my key input class. I wrote it some time ago and it’s  pretty messy but it’s worked pretty well. So I thought I had better declare it. Also my loading bar class which was written last minute and rubbish, but again it does the job.  I don’t have a github, so I’m pasting the code below. You can use it if you like, but I doubt you will want to! haha

njc.KeyInput = function() {
this.keyActions = [];
this.keyEvents = [];
this.loggingInput = false;

 

njc.KeyInput.prototype.register = function(elementId) {
var element = document.getElementById(elementId);
var referenceName = this;
element.addEventListener(“keydown”, function(event) {referenceName.onEvent(event)}, false);
element.addEventListener(“keyup”, function(event) {referenceName.onEvent(event)}, false);
element.addEventListener(“keypress”, function(event) { referenceName.iHateOpera(event)}, false);
};

// opera doesn’t support pevent default on keydown! edit: or it didn’t I have no idea anymore what this is for
njc.KeyInput.prototype.iHateOpera = function(event) {

if (!this.loggingInput) {
return;
}

if (this.getActionByCode(event.keyCode)) {
event.preventDefault();
}
};

njc.KeyInput.prototype.onEvent = function(event) {

if (!this.loggingInput) {
return;

}
var keyAction = this.getActionByCode(event.keyCode);

if (keyAction) {
event.preventDefault();
}
this.keyEvents.push(event);
};

njc.KeyInput.prototype.poll = function() {
this.resetPress();

for (var i = 0; i < this.keyEvents.length; i++) {
this.processEvent(this.keyEvents[i]);
}

this.keyEvents.length = 0;
};

njc.KeyInput.prototype.processEvent = function(e) {
keyAction = this.getActionByCode(e.keyCode);

if (keyAction != undefined) {

if (e.type == “keydown”) {
keyAction.press();
} else if (e.type == “keyup”) {
keyAction.release();
}
};
};

njc.KeyInput.prototype.addKeyAction = function(name, code, rapidFireTime, initialDelay) {
var keyAction = new njc.KeyAction(name, code);

if (rapidFireTime) {
keyAction.rapidFire = true;
keyAction.rapidFireTime = rapidFireTime;
}

if (initialDelay) {
keyAction.initialDelay = initialDelay;

}

this.keyActions.push(keyAction);

return this;
};

njc.KeyInput.prototype.getActionByCode = function(keyCode) {

for (var i = 0; i < this.keyActions.length; i++) {
if (this.keyActions[i].code == keyCode) {
return this.keyActions[i];
}
};
};

njc.KeyInput.prototype.key = function(keyName) {

for (var i = 0; i < this.keyActions.length; i++) {
if (this.keyActions[i].name == keyName) {
return this.keyActions[i];
}
};
return undefined;
};

njc.KeyInput.prototype.resetPress = function() {
for (var i = 0; i < this.keyActions.length; i++) {
var keyAction = this.keyActions[i];
keyAction.pressed = false;
keyAction.released = false;
}
};

njc.KeyInput.prototype.logInput = function(log) {
this.loggingInput = log;
this.keyEvents.length = 0;
// reset all keys when log input is enabled or disabled to make sure
// game has no unwanted actions.
for (var i = 0; i < this.keyActions.length; i++) {
this.keyActions[i].fullReset();
}
};
};

njc.KeyAction = function(name, code) {
this.name = name;
this.code = code;

this.rapidFire = false;
this.rapidFireTime;
this.initialDelay = 0;
this.firstPress = false;
this.timeStamp;

this.isDown = false;
this.pressed = false;
this.released = false;

njc.KeyAction.prototype.fullReset = function() {
this.isDown = false;
this.pressed = false;
this.released = false;
};

njc.KeyAction.prototype.press = function() {

if (!this.isDown) {
this.isDown = true;
this.pressed = true;
this.firstPress = true;

if (this.rapidFire) {
this.timeStamp = new Date().getTime();
}
}
};

njc.KeyAction.prototype.release = function() {
this.isDown = false;
this.released = true;
};

njc.KeyAction.prototype.wasReleased = function() {
return this.released;
};

njc.KeyAction.prototype.wasPressed = function() {

if (this.rapidFire && this.isDown && this.enoughTimePassed()) {
this.timeStamp = new Date().getTime();
this.firstPress = false;
this.pressed = true;
}

return this.pressed;
};

njc.KeyAction.prototype.enoughTimePassed = function() {
var time = this.rapidFireTime;

if (this.firstPress && this.initialDelay != 0) {
time = this.initialDelay;
}

if (new Date().getTime() – this.timeStamp >= time) {
return true;
}

return false;
};
};

 

 

 

// loading scene
njc.LoadingScene = function(stage, numberToLoad) {
this.barColor = “white”;
this.fontColor = “white”;
this.fontStyle = “30px arial, sans-serif”;
this.barWidth = 400;
this.barHeight = 40;
this.text = “Loading…”;
this.backgroundColor = “rgb(30, 30, 30)”;

this.stage = stage;
this.numberToLoad = numberToLoad;
this.assetsLoaded = 0;

this.background = new createjs.Shape();

this.background.graphics.beginFill(this.backgroundColor).drawRect(0, 0, this.stage.canvas.width, this.stage.canvas.height);

this.barText = new createjs.Text(this.text, this.fontStyle, this.fontColor)
this.barText.x = (this.stage.canvas.width – this.barText.getMeasuredWidth() ) / 2;
this.barText.y = 330;

this.bar = new createjs.Shape();
this.bar.x = (this.stage.canvas.width – this.barWidth) / 2;
this.bar.y = (this.stage.canvas.height – this.barHeight) / 2;

this.barOutLine = new createjs.Shape();
this.barOutLine.x = this.bar.x;
this.barOutLine.y = this.bar.y;
this.barOutLine.graphics.beginStroke(this.barColor).drawRect(0, 0, this.barWidth, this.barHeight);

this.stage.addChild(this.background);
this.stage.addChild(this.bar);
this.stage.addChild(this.barOutLine);
this.stage.addChild(this.barText);
this.stage.update();
};

njc.LoadingScene.prototype.onComplete = function() {
this.stage.removeChild(this.background);
this.stage.removeChild(this.bar);
this.stage.removeChild(this.barOutLine);
this.stage.removeChild(this.barText);
};

njc.LoadingScene.prototype.render = function() {
this.bar.graphics.clear();

var percentageLoaded =  this.assetsLoaded / this.numberToLoad * 100;
var width = (percentageLoaded / 100) * this.barWidth;
this.bar.graphics.beginFill(this.barColor).drawRect(0, 0, width, this.barHeight);
this.stage.update();
};

njc.LoadingScene.prototype.onAssetLoaded = function() {

this.assetsLoaded++;

if (this.assetsLoaded > this.numberToLoad) {
console.log(“Error”);
return;
}

this.render();
};


3 Responses to “Just declaring my rubbish base code”

  1. JPatrick says:

    Please use pastebin or gist next time.

  2. Dream Of Sleeping says:

    Ok sorry, I will do in future.

Leave a Reply

You must be logged in to post a comment.

[cache: storing page]