-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcanvas2.html
190 lines (176 loc) · 5.34 KB
/
canvas2.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
189
190
<!DOCTYPE html>
<html>
<head>
<title>Second Canvas Engine</title>
<style type="text/css">
*{
font-family: Arial;
}
</style>
<script type="text/javascript">
function debug(stuff){
document.getElementById("debug").value += stuff+"\n"
document.getElementById("debug").scrollTop = document.getElementById("debug").scrollHeight
}
var keyState = {};
window.addEventListener('keydown',function(e){keyState[e.keyCode || e.which] = true;},true);
window.addEventListener('keyup',function(e){keyState[e.keyCode || e.which] = false;},true);
function World(canvas){
var self = this;
this.init = function(){
this.objects = [];
this.canvas = document.getElementById(canvas);
this.context = this.canvas.getContext("2d");
}
this.start = function(){
self.drawObjects = setInterval(this.drawObjects,10);
self.moveObj = setInterval(this.moveObjects,10);
}
this.clear = function(){
self.context.fillStyle = "#34495e"
self.context.fillRect(0,0,self.canvas.width,self.canvas.height);
}
this.drawObjects = function(){
self.clear()
self.objects.forEach(function(obj){
obj.draw();
});
}
this.moveObjects = function(){
self.objects.forEach(function(obj){
if (typeof(obj.velX) !== 'undefined'){
obj.move(obj);
}
});
}
this.appendObject = function(newObject){
this.objects.push(newObject);
return this.objects[this.objects.length-1]
}
this.init();
}
function Object(x,y,width,height,color){
self = this
this.init = function() {
this.x = parseFloat(x);
this.y = parseFloat(y);
this.height = height;
this.width = width;
this.color = color;
}
this.draw = function(ctx){
ctx = world.context;
ctx.fillStyle=this.color
ctx.strokeStyle="#FFFFFF"
ctx.lineWidth = 2;
ctx.fillRect(this.x,this.y,this.width,this.height);
ctx.strokeRect(this.x,this.y,this.width,this.height);
}
this.isTouching = function(self,o){
if ((((o.x < self.x && self.x < (o.x + o.width)) || (o.x < (self.x + self.width) && (self.x + self.width) < (o.x + o.width)))) && (((o.y < self.y && self.y < (o.y + o.height)) || (o.y < (self.y + self.height) && (self.y + self.height) < (o.y + o.height))))) {
return true
}
}
this.init();
}
function Obstacle(x,y,width,height,color){
this.init = function() {
this.x = parseFloat(x);
this.y = parseFloat(y);
this.height = height;
this.width = width;
this.color = color;
}
this.init();
}
Obstacle.prototype = new Object();
function Movable(x,y,width,height,color){
var self = this;
this.init = function() {
this.x = parseFloat(x);
this.y = parseFloat(y);
this.velX = 0.0;
this.velY = 0.0;
this.height = height;
this.width = width;
this.color = color;
}
this.move = function(s) {
world.objects.forEach(function(obj){
s.x = s.x + s.velX
s.y = s.y + s.velY
if (!(obj instanceof Player)) {
if(s.isTouching(s,obj)){
var x2 = s.x + s.width;
var y2 = s.y + s.height;
var ox2 = obj.x + obj.width;
var oy2 = obj.y + obj.height;
if (Math.abs(s.x - ox2) < Math.abs(s.y - oy2)){
if(s.x > obj.x){s.x -= s.x - ox2;}
}
else{
if(s.y > obj.y){s.y -= s.y - oy2;}
}
if (Math.abs(x2 - obj.x) < Math.abs(y2 - obj.y))
{
if (x2 < ox2){s.x += obj.x - x2;}
}
else{
if (y2 < oy2){s.y += obj.y - y2;}
}
}
}
});
}
this.init();
}
Movable.prototype = new Object();
function Player(x,y,width,height,color){
var self = this
this.init = function() {
this.x = parseFloat(x);
this.y = parseFloat(y);
this.height = height;
this.width = width;
this.color = color;
}
this.testKeys = function(){
if (keyState[87]){
self.velY = -2;
}
if (keyState[83]){
self.velY = 2;
}
if (keyState[65]){
self.velX = -2;
}
if (keyState[68]){
self.velX = 2;
}
if (self.velX != 0){
self.velX = ((self.velX - .1*(Math.abs(self.velX)/self.velX)).toFixed(1))/1
}
if (self.velY != 0){
self.velY = ((self.velY - .1*(Math.abs(self.velY)/self.velY)).toFixed(1))/1
}
}
this.init();
}
Player.prototype = new Movable();
</script>
</head>
<body>
<canvas id="game" width="600" height="300" style="border:1px solid;"></canvas>
<br><br>
Debug:<br/>
<textarea id="debug" style="width: 600px; height: 150px;"></textarea>
<script type="text/javascript">
var asdf = "ayy";
var world = new World("game");
world.start();
var player = world.appendObject(new Player(10,10,30,30,"#ABCDEF"))
world.appendObject(new Obstacle(270,90,30,120,"#EEEEEE"))
var movement = setInterval(player.testKeys,10);
</script>
</body>
</html>