-
Notifications
You must be signed in to change notification settings - Fork 0
/
FrameRateDemo.html
188 lines (152 loc) · 5.82 KB
/
FrameRateDemo.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
<html>
<head>
<title>Game Scheleton</title>
<style></style>
</head>
<body>
<canvas
id="gameDrawingPort"
width="600"
height="400"
style="background-color: black;"
>
</canvas>
<div>
<fieldset>
<legend>Visual Ojbects</legend>
<button>Add visual object</button>
</fieldset>
</div>
</body>
<script lang="JavaScript">
(function() {
var view = {
drawBoundaryBox : function(position, size) {
var ctx = gameModel.gameCanvas.getContext("2d");
// select a random color
ctx.strokeStyle = "rgb(255," + Math.ceil(Math.random() * 255) + "," + Math.ceil(Math.random() * 255) + ")" ;
ctx.strokeRect(position.x, position.y, size.width, size.height);
},
drawVisualObjects : function() {
for(var i=0; i < gameModel.visualObjects.length; i++) {
var objectToDraw = gameModel.visualObjects[i];
if(objectToDraw.animations.length > 0) {
// drawAnimations
console.log("drawing animations");
}
else {
// just draw default sprite
if(objectToDraw.defaultSprite != "") {
// draw default sprite
console.log("drawing default sprite");
}
else {
// draw boundary box to signal object's postion
// console.log("drawing boundary box");
this.drawBoundaryBox(objectToDraw.position, objectToDraw.size);
}
}
}
}
};
var gameModel = {
gameCanvas: document.getElementById("gameDrawingPort"),
gameOn: true,
animationFrameID: "",
elapsedTime : 0,
frameCount: 0,
visualObjects: [],
newVisualObject : function() {
return {
position : {x:0, y:0},
size: {width : 0, height: 0},
defaultSprite: "",
visible : true,
animations :[],
phisicsBody:
};
}
};
var phisics = {
transformations : {
rotatePoint : function(centerPoint, pointToRotate, rotationAngle) {
var angle = (rotationAngle) * (Math.PI/180); // Convert to radians
var rotatedX = Math.cos(angle) * (pointToRotate.x - centerPoint.x) - Math.sin(angle) * (pointToRotate.y-centerPoint.y) + centerPoint.x;
var rotatedY = Math.sin(angle) * (pointToRotate.x - centerPoint.x) + Math.cos(angle) * (pointToRotate.y - centerPoint.y) + centerPoint.y;
return {x: rotatedX, y: rotatedY};
} ,
newPhisicsBody : function() {
return {
position : {x : 0, y: 0},
forceVectors : [],
}
},
newForceVector: function(){
return {
xforce: 0,
yforce: 0,
rotattion: 0
};
}
}
};
var gameController = {
handleUserInput: function(e) {
switch (e.key) {
case "Escape":
gameModel.gameOn = false;
break;
default:
console.log(e);
}
},
loop: function(timestamp) {
gameModel.frameCount++;
if(gameModel.elapsedTime == 0) {
gameModel.elapsedTime = timestamp;
}
else if((timestamp - gameModel.elapsedTime) >= 1000) {
console.log("Frame count after a second :" + gameModel.frameCount);
gameModel.elapsedTime = 0;
gameModel.frameCount = 0;
view.drawVisualObjects();
}
gameModel.animationFrameID = window.requestAnimationFrame(gameController.loop);
try {
if (!gameModel.gameOn) {
window.cancelAnimationFrame(gameModel.animationFrameID);
gameController.endGame();
return;
}
} catch (err) {
console.log("something went wrong!", err);
return;
}
},
init: function() {
console.log("here is DISH:", this);
console.log("initializing game..");
document.addEventListener("keydown", this.handleUserInput);
},
endGame: function() {
console.log("game is ending.. wrapping up...");
}
};
// create and configure visual objects -------------------------------------------------------------
var myObject = gameModel.newVisualObject();
var object2 = gameModel.newVisualObject();
object2.position.x = 200;
object2.position.y = 100;
object2.size.height = 50;
object2.size.width = 60;
myObject.position.x = 100;
myObject.position.y = 200;
myObject.size.width = 20;
myObject.size.height = 30;
gameModel.visualObjects.push(myObject);
gameModel.visualObjects.push(object2);
gameController.init();
gameModel.animationFrameID = window.requestAnimationFrame(gameController.loop);
})();
</script>
</html>