Skip to content

Commit d1d94bf

Browse files
Jeff CooperJeff Cooper
authored andcommitted
initial commit of engine stuffs
0 parents  commit d1d94bf

File tree

4 files changed

+261
-0
lines changed

4 files changed

+261
-0
lines changed

ImageDrawable.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
function ImageDrawable(src) //a non-solid image
2+
{
3+
this.solid=false;
4+
this.myImage = new Image();
5+
this.myImage.src=src;
6+
7+
this.z = -10; //default to lower z
8+
9+
this.setSize(this.myImage.width, this.myImage.height);
10+
11+
this.draw = function()
12+
{
13+
try
14+
{
15+
c2d.drawImage(this.myImage,0,0);
16+
}
17+
catch(err){}
18+
}
19+
}
20+
21+
ImageDrawable.prototype = new Drawable();
22+

SpriteDrawable.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
function SprteDrawable(src) //a solid image
2+
{
3+
this.solid=true;
4+
this.myImage = new Image();
5+
this.myImage.src=src;
6+
7+
this.z = -10; //default to lower z
8+
9+
this.setSize(this.myImage.width, this.myImage.height);
10+
11+
this.draw = function()
12+
{
13+
c2d.drawImage(this.myImage,0,0);
14+
}
15+
}
16+
17+
ImageDrawable.prototype = new Drawable();

drawable.js

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
/******
2+
* 1) prestep
3+
* 2) collisions
4+
* 3) step
5+
* 4) posttep
6+
*/
7+
8+
function Drawable()
9+
{
10+
this.x=0; //x and y are top-left corner
11+
this.y=0;
12+
this.width=0;
13+
this.height=0;
14+
this.z=0;
15+
this.solid=true;
16+
this.name="";
17+
}
18+
Drawable.prototype.draw=function(){};
19+
20+
Drawable.prototype.prestep=function(){};
21+
Drawable.prototype.step=function(){};
22+
Drawable.prototype.poststep=function(){};
23+
24+
Drawable.prototype.handleKeyDown=function(key){};
25+
Drawable.prototype.handleKeyUp=function(key){};
26+
Drawable.prototype.collide=function(other,dir){};
27+
Drawable.prototype.setPos=function(a,b)
28+
{
29+
this.x=a;
30+
this.y=b;
31+
}
32+
33+
Drawable.prototype.setSize=function(a,b)
34+
{
35+
this.width=a;
36+
this.height=b;
37+
}
38+
39+
Drawable.prototype.collides=function(other) //true if I collide with other, false otherwise
40+
{
41+
/*
42+
lr=false;
43+
ud=false;
44+
//horizontal collisions
45+
if(this.x <= other.x && this.x+this.width >= other.x) //on left
46+
lr=true;
47+
if(this.x >= other.x && other.x+other.width >= this.x) //on right
48+
lr=true;
49+
50+
//vertical collisions
51+
if(this.y <= other.y && this.y+this.height >= other.y) //on bottom
52+
ud=true;
53+
if(this.y >= other.y && other.y >= this.y+this.height) //on top
54+
ud=true;
55+
56+
return lr&&ud;*/
57+
58+
left1=this.x;
59+
left2=other.x;
60+
right1=this.x+this.width;
61+
right2=other.x+other.width;
62+
63+
top1=this.y;
64+
top2=other.y;
65+
bottom1=this.y+this.height;
66+
bottom2=other.y+other.height;
67+
68+
if(bottom1 < top2) {return false;}
69+
if(bottom2 < top1) {return false;}
70+
71+
if(right1 < left2) {return false;}
72+
if(right2 < left1) {return false;}
73+
74+
return true;
75+
}
76+
77+
Drawable.prototype.destroy = function()
78+
{
79+
for(i=0;i<elements.length;i++)
80+
{
81+
if(elements[i] == this)
82+
delete elements.splice(i,1);
83+
}
84+
}

engine.js

Lines changed: 138 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,138 @@
1+
var canvas = null;
2+
var c2d = null;
3+
var game = null;
4+
var elements = new Array();
5+
6+
7+
function startGame()
8+
{
9+
try
10+
{
11+
GameMain();
12+
}
13+
catch(err)
14+
{
15+
elements = new Array();
16+
setTimeout(startGame, 10);
17+
}
18+
}
19+
20+
21+
function init(el, FPS)
22+
{
23+
GameInit();
24+
canvas = document.getElementById(el);
25+
c2d = canvas.getContext('2d');
26+
game = new Engine();
27+
setInterval(game.draw, 1000 / FPS);
28+
document.onkeydown = function(event){game.keyDown(event)};
29+
document.onkeyup = function(event){game.keyUp(event)};
30+
started=false;
31+
startGame();
32+
}
33+
34+
function Engine()
35+
{
36+
37+
this.draw = function()
38+
{
39+
c2d.save();
40+
c2d.clearRect(0,0,canvas.width, canvas.height);
41+
c2d.fillStyle = "rgb(0,0,0)";
42+
c2d.fillRect(0,0,canvas.width, canvas.height);
43+
44+
45+
var ii=0;
46+
for(var i=0;ii<elements.length;ii++)
47+
{
48+
c2d.save();
49+
c2d.translate((elements[ii]).x, (elements[ii]).y);
50+
(elements[ii]).draw();
51+
c2d.restore();
52+
}
53+
54+
55+
for(i=0;i<elements.length;i++)
56+
elements[i].prestep();
57+
58+
59+
for(i=0;i<elements.length;i++)
60+
{
61+
for(j=0;j<elements.length;j++)
62+
{
63+
if(elements[i] != elements[j])
64+
{
65+
x=elements[i].collides(elements[j]);
66+
if(x != 0)
67+
{
68+
elements[i].collide(elements[j],x);
69+
}
70+
}
71+
}
72+
}
73+
74+
75+
for(i=0;i<elements.length;i++)
76+
elements[i].step();
77+
78+
79+
for(i=0;i<elements.length;i++)
80+
elements[i].poststep();
81+
82+
c2d.restore();
83+
}
84+
85+
this.keyDown = function(key)
86+
{
87+
for(i=0;i<elements.length;i++)
88+
{
89+
elements[i].handleKeyDown(key.keyCode);
90+
}
91+
}
92+
93+
this.keyUp = function(key)
94+
{
95+
for(i=0;i<elements.length;i++)
96+
{
97+
elements[i].handleKeyUp(key.keyCode);
98+
}
99+
}
100+
101+
this.add=function(x) //can take a single element or [nested] arrays
102+
{
103+
if(x[0])
104+
{
105+
var u=0;
106+
for(var u=0;u<x.length;u++)
107+
{
108+
this.add(x[u]);
109+
}
110+
}
111+
else
112+
{
113+
elements.push(x);
114+
elements.sort(function(a,b){ return (a.z > b.z)? 1 : (a.z < b.z)? -1: 0; });
115+
}
116+
}
117+
}
118+
119+
120+
function includeFile(file)
121+
{
122+
// you can add more files if you want
123+
var scripts = [file];
124+
125+
var scriptElement = new Array(scripts.length);
126+
var headTag = document.getElementsByTagName("head")[0];
127+
var fragment = document.createDocumentFragment();
128+
129+
for(var i=0, count = scripts.length; i < count; i++)
130+
{
131+
scriptElement[i] = document.createElement("script");
132+
scriptElement[i].setAttribute("src", scripts[i]);
133+
fragment.appendChild(scriptElement[i]);
134+
}
135+
136+
headTag.appendChild(fragment);
137+
}
138+

0 commit comments

Comments
 (0)