-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
156 lines (123 loc) · 4.88 KB
/
index.js
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
var wedge = {
ctx : '', //canvas context
canvas : '',
objects : [], //number of wedge objects
setup : function()
{
var canvas = document.getElementById('gameCanvas')
this.canvas = canvas
this.ctx = canvas.getContext("2d")
},
update : function(inLoop = function inLoop(){})
{
loop() //run for the first time
function loop()
{
wedge.rect(0, 0, wedge.canvas.width, wedge.canvas.height, '#2e2e2e') //background
function updateObject(i, name = 'wedgeObject', transform, sprite = 'rectangle', color = 'white', physics) {
//PHYSICS
if(physics.isFriction)
{
if(physics.velocity.x != 0) physics.velocity.x -= physics.friction;
if(physics.velocity.y != 0) physics.velocity.y -= physics.friction;
if(physics.velocity.x < physics.friction && physics.velocity.x > -physics.friction) physics.velocity.x = 0;
if(physics.velocity.x < physics.friction && physics.velocity.x > -physics.friction) physics.velocity.y = 0;
}
//ADDING VELOCITY TO POSITION
transform.position.x += physics.velocity.x
transform.position.y += physics.velocity.y
//UPDATING TRANSFORM AND PHYSICS
wedge.objects[i].physics = physics
wedge.objects[i].transform = transform
// RENDER
switch (sprite){
case 'rectangle':
wedge.rect(transform.position.x, transform.position.y, transform.size.x, transform.size.y, color)
break
default:
console.log('there is not a sprite named: ' + this.sprite);
}
}
function collUpdate(j, collider, tran)
{
collider.collides = false;
collider.collidesWith = [];
for (var i = 0; i < wedge.objects.length; i++) {
if(i != j)
{
var obj = wedge.objects[i];
if(obj.physics.collider.works)
{
if(tran.position.x < obj.transform.position.x + obj.transform.size.x && tran.position.x + tran.size.x > obj.transform.position.x && tran.position.y < obj.transform.position.y + obj.transform.size.y && tran.position.y + tran.size.y > obj.transform.position.y)
{
collider.collides = true;
collider.collidesWith[collider.collidesWith.length] = wedge.objects[i]
//console.log(wedge.objects[j].name + ' is colliding with ' + wedge.objects[i].name + ' on bouth other axis')
//console.log('\n\n\n ' + wedge.objects[j].name)
//console.log(collider.collidesWith)
}
}
//console.log('\n\n\n ' + wedge.objects[j].name)
//console.log(collider.collides)
wedge.objects[j].physics.collider = collider
}
}
//wedge.objects[j].physics = physics
}
//GO THROUGH OBJECTS AND UPDATE THEM
for (i = 0; i < wedge.objects.length; i++) {
var obj = wedge.objects[i];
updateObject(i, obj.name, obj.transform, obj.sprite, obj.color, obj.physics);
if(obj.physics.collider.works)
{
collUpdate(i, obj.physics.collider, obj.transform);
}
}
inLoop(); //user input
requestAnimationFrame(loop); //repeat next frame
}
},
log : function(message = "I'm working.")
{
var logMessage = "WedGE: " + message
console.log(logMessage);
document.getElementById('wedgeDebug').innerHTML = logMessage
},
rect : function(posX = 0, posY = 0, width = 100, height = 100, color = 'white')
{
this.ctx.fillStyle = color
this.ctx.fillRect(posX, posY, width, height)
},
object : class{
constructor(name = 'wedgeObject', posX = 0, posY = 0, width = 100, height = 100, sprite = 'rectangle', color = 'white') {
this.name = name;
this.sprite = sprite;
this.color = color;
//transform
var pos = {x:posX, y:posY};
var sz = {x:width, y:height}
this.transform = {position:pos, size:sz}
//physics
var velocity = {x:0, y:0};
var isFriction = false;
var friction = 0.001;
//colider
var collWorks = false;
var collType = 'rectangle';
var collWidth = this.transform.size.x;
var collHeight = this.transform.size.y;
var collides = 'false';
var collidesWith = [];
var collider = {works:collWorks, type:collType, width:collWidth, height:collHeight, collides:collides, collidesWith:collidesWith};
this.physics = {velocity, isFriction, friction, collider};
switch (this.sprite){
case 'rectangle':
wedge.rect(this.transform.position.x, this.transform.position.y, this.transform.size.x = width, this.transform.size.y = height, this.color)
break
default:
console.log('there is not a sprite named: ' + this.sprite);
}
wedge.objects[wedge.objects.length] = this;
}
},
};