Skip to content

Commit

Permalink
Merge pull request #13 from Patrick-W-McMahon/Dev
Browse files Browse the repository at this point in the history
Dev
  • Loading branch information
Patrick-W-McMahon authored Jun 28, 2017
2 parents d04326f + 6cd7cd6 commit bbe1e86
Show file tree
Hide file tree
Showing 53 changed files with 1,913 additions and 133 deletions.
311 changes: 295 additions & 16 deletions JinxEngine.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ function GameEngine(){
this.displayDomId;
this.requestID;
this.keys = new Array(400);
this.keysDown=[];//future depracation
this.keysUp=[];//future depracation
this.keysPressed=[];//future depracation
//this.keysDown=[];//future depracation
//this.keysUp=[];//future depracation
//this.keysPressed=[];//future depracation
this.engineMode="live";
this.frameCount=0;
this.eventStack=[];
Expand All @@ -32,7 +32,6 @@ function GameEngine(){
});

this.init = function(){

for(var x=0;x<this.objects.length;x++){
if(typeof(this.objects[x].init)==='function'){
this.objects[x].init(gameEngineThis);
Expand All @@ -55,11 +54,9 @@ function GameEngine(){
location.href = 'http://www.mosilla.org/en-US/firefox/new/';
}
}

gameEngineThis.displayDomId.addEventListener("mousedown",function(){
gameEngineThis.mousePos.left=true;
},false);

}

this.PlaySound = function(src){
Expand Down Expand Up @@ -116,7 +113,6 @@ function GameEngine(){
}else{
console.log("Object "+x+" is undefined");
}

}
for(var x=0;x<this.objects.length;x++){
if(typeof(this.objects[x])!==undefined){
Expand Down Expand Up @@ -234,18 +230,13 @@ function GameEngine(){
return false;
}
}
*/
/*
this.clearKeys = function(){
this.keysDown.clear();
this.keysPressed.clear();
this.keysUp.clear();
}
*/


/*
window.onkeydown = function(e){
if(gameEngineThis.loopState){
gameEngineThis.engineLog("Key Down "+e.keyCode);
Expand Down Expand Up @@ -326,8 +317,6 @@ function GameEngine(){
return dpr / bsr;
};



this.setDisplay = function(canvas){
this.displayDomId = document.getElementById(canvas);
var ratio = this.getPixelRatio();
Expand All @@ -339,7 +328,6 @@ function GameEngine(){
this.displayDomId.style.height = h+"px";
this.display = document.getElementById(canvas).getContext(this.context);
//this.getDisplayPixelDensity(canvas,this.display);

}

this.addObject = function(o){//pass object
Expand All @@ -353,6 +341,10 @@ function GameEngine(){
return this.objects[this.objects.length-1].id;//return the objects id
}

this.getObjectByIndex = function(i){
return this.objects[i];
}

this.purgeObjects = function(){
this.objects.clear();
}
Expand Down Expand Up @@ -434,6 +426,8 @@ function GameEngine(){
this.purgeObjects();
this.frameCount=0;
this.render(this.display);
window.cancelAnimationFrame(gameEngineThis.requestID);
gameEngineThis.requestID = undefined;
}

this.frame = function(){
Expand All @@ -448,8 +442,293 @@ function GameEngine(){
gameEngineThis.render(gameEngineThis.display);
}
if(gameEngineThis.loopState){
window.cancelAnimationFrame(gameEngineThis.requestID);
gameEngineThis.requestID = undefined;
gameEngineThis.requestID = window.requestAnimationFrame(gameEngineThis.frame);
}
}

};

function ArcObject(x,y,radius,startAngle,endAngle,anticlockwise){
this.x=x;
this.y=y;
this.radius=radius;
this.startAngle=startAngle;
this.endAngle=endAngle;
this.anticlockwise=anticlockwise;
this.path = new Path2D();
this.path.arc(x,y,radius,startAngle,endAngle,anticlockwise);
this.path.closePath();
this.g=false;

this.getObjType = function(){
return "Arc";
}

this.pointIntersects = function(x,y){
var ranges={
x1:this.x-this.radius,
x2:this.x+this.radius,
y1:this.y-this.radius,
y2:this.y+this.radius
}
if(point.x>=ranges.x1&&point.x<=ranges.x2&&point.y>=ranges.y1&&point.y<=ranges.y2){
//work on this
}
return false;
}

this.pointIn = function(x,y){
if(this.g){
return this.g.isPointInPath(this.path,x,y);
}
}

this.rectIntersects = function(rect){
//work on this
}

this.getPath = function(g){
this.g=g;
return this.path;
}
}

function Rectangle(x,y,h,w){
this.x=x;
this.y=y;
this.height=h;
this.width=w;
this.path = new Path2D();
this.path.rect(x,y,h,w);
this.path.closePath();
this.g=false;
this.roundedSides={tl:0,tr:0,bl:0,br:0};

this.getObjType = function(){
return "Rectangle";
}

this.rounding = function(side,radius){
switch(side){
case "tl":
this.roundedSides.tl=radius;
break;
case "tr":
this.roundedSides.tr=radius;
break;
case "bl":
this.roundedSides.bl=radius;
break;
case "br":
this.roundedSides.br=radius;
break;
case "all":
default:
this.roundedSides.tl=radius;
this.roundedSides.tr=radius;
this.roundedSides.bl=radius;
this.roundedSides.br=radius;
}
if(this.hasRoundedCorners()){
this.path.moveTo(x,y+radius);
this.path.lineTo(x,y+height-radius);
this.path.quadraticCurveTo(x,y+height,x+radius,y+height);
this.path.lineTo(x+width-radius,y+height);
this.path.quadraticCurveTo(x+width,y+height,x+width,y+height-radius);
this.path.lineTo(x+width,y+radius);
this.path.quadraticCurveTo(x+width,y,x+width-radius,y);
this.path.lineTo(x+radius,y);
this.path.quadraticCurveTo(x,y,x,y+radius);
this.path.closePath();
}else{
this.path.rect(this.x,this.y,this.h,this.w);
}
}

this.hasRoundedCorners = function(){
if(this.roundedSides.tl>0||this.roundedSides.tr>0||this.roundedSides.bl>0||this.roundedSides.br>0){
return true;
}
return false;
}

this.RectIntersects = function(rect){
if(typeof(rect)==="undefined"){
return false;
}else{
if(this.x < rect.x + rect.width && this.x + this.width > rect.x && this.y < rect.y + rect.height && this.height + this.y > rect.y){
return true;
}
}
return false;
}

this.pointIn = function(x,y){
if(this.g){
return this.g.isPointInPath(this.path,x,y);
}
}

this.getPath = function(g){
this.g=g;
return this.path;
}
}

/*
*Basic Texture object that is used to define a texture that will be drawn by a sprite
*/
function Texture(src,x,y,h,w){
var img = new Image();
img.src=src;
this.img = img;
this.x=x;
this.y=y;
this.h=h;
this.w=w;
}

/*
*Sprite sheet that is a more complex texture that holds many small textures that are used by a sprite for animation
*/
function SpriteSheet(src){
var img = new Image();
img.src=src;
this.texture=img;
this.spriteGroups=[];
this.sheetItems=[];

this.addAnimation = function(obj){//{name:,matrix:[{x,y,h,w}...,],loop,delay}
this.spriteGroups.push(obj);
}

this.getAnimation = function(name){
for(x=0;x<this.spriteGroups.length;x++){
if(this.spriteGroups[x].name==name){
return this.spriteGroups[x];
}
}
}

this.addItem = function(obj){//{name,x,y,h,w}
this.sheetItems.push(obj);
}

this.getItem = function(name){
for(x=0;x<this.sheetItems.length;x++){
if(this.sheetItems[x].name==name){
return this.sheetItems[x];
}
}
}

}

/*
*The Sprite object defines the most basic drawing element. It uses textures to draw an object onto the screen.
*/
function Sprite(eId){
this.gameEngine;
this.texture;
this.curTexPos;
this.canvasPos;
this.isSpriteSheet;
this.spriteSheet;
this.timelaps;
this.frameCount=0;
this.currentAnimation;
this.firstRun=1;
this.animation;
this.clickDelay=30;
this.delayCounter=0;
this.eventId=eId;
this.cursorHover=false;

this.init = function(e){
this.gameEngine = e;
}

this.update = function(){
var mousePos = this.gameEngine.getEventInStack("cursor",false);
var cursorBox={
x:mousePos.x,
y:mousePos.y,
height:2,
width:2
}
if(this.gameEngine.collitionDetection(cursorBox,this.canvasPos)){
this.cursorHover=true;
if(this.mousePos.left){
if(this.delayCounter==0){
this.gameEngine.addEvent({name:this.eventId,clicked:true});
this.delayCounter=this.clickDelay;
}else{
this.delayCounter--;
}
}
}else{
this.buttonHover=false;
}
var currentAnimation = this.spriteSheet.getAnimation(this.animation);
this.timelaps++;
if(this.timelaps>currentAnimation.delay||this.firstRun==1){
this.firstRun=0;
this.timelaps=0;
var curMa = currentAnimation.matrix;
var curAnSize = curMa.length-1;
if(this.frameCount>curAnSize){
if(currentAnimation.loop==true){
this.frameCount=0;
}else{
this.frameCount=curAnSize;
}
}
var crm = curMa[this.frameCount];
this.curTexPos={
x:crm.x,
y:crm.y,
h:crm.h,
w:crm.w
}
this.frameCount++;
}
}

this.setAnimation = function(animation){
this.animation = animation;
}

this.setPos = function(x,y,h,w){
this.canvasPos={x:x,y:y,h:h,w:w}
}

this.getPos = function(){
return this.canvasPos;
}

//this.isHover

this.draw = function(g){
var texture,tx,ty,tw,th,cx,cy,cw,ch;
cx = this.canvasPos.x;
cy = this.canvasPos.y
cw = this.canvasPos.w;
ch = this.canvasPos.h;
if(this.isSpriteSheet){
tx = this.curTexPos.x;
ty = this.curTexPos.y;
tw = this.curTexPos.w;
th = this.curTexPos.h;
texture = this.spriteSheet.texture;
}else{
tx = this.texture.x;
ty = this.texture.y;
tw = this.texture.w;
th = this.texture.h;
texture = this.texture.img;
}
g.drawImage(texture,tx,ty,tw,th,cx,cy,cw,ch);
}
}
Loading

0 comments on commit bbe1e86

Please sign in to comment.