-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSprite.js
More file actions
180 lines (147 loc) · 5.09 KB
/
Sprite.js
File metadata and controls
180 lines (147 loc) · 5.09 KB
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
function Sprite(canvas, file, width, height, startX, startY, name) {
this.canvas = canvas;
this.ctx = this.canvas.getContext('2d');
this.file = file;
this.image = new Image();
this.width = width;
this.height = height;
this.name = name;
this.shown = true;
this.x = startX;
this.y = startY;
this.dx = 0;
this.dy = 0;
this.isMoving = false;
//debugging
if (height === isNaN) {
console.log("Failure! Height was not set to a number. You wrote " + height);
} //endif
if (width === isNaN) {
console.log("Failure! Width was not set to a number. You wrote " + width);
} //endif
//find if sprite is moving
if (this.dx != 0 || this.dy != 0) {
this.isMoving = true;
}
if (this.dx === 0 || this.dy === 0) {
this.isMoving = false;
}
this.setSize = function(newWidth, newHeight) {
this.width = newWidth;
this.height = newHeight;
if (newHeight === isNaN) {
console.log("Failure! Height was not set to a number. You wrote " + newHeight);
} //endif
if (newWidth === isNaN) {
console.log("Failure! Width was not set to a number. You wrote " + newWidth);
} //endif
} //end setSize()
this.setImageFile = function(newFile) {
this.file = newFile;
} //end setImageFile()
this.getSpriteSpeed = function() {
spriteSpeed = Math.sqrt((this.dx * this.dx) + (this.dy * this.dy));
return spriteSpeed;
} //end getSpeed()
this.getSpriteDX = function() {
return this.dx;
} //end getSpriteDX()
this.getSpriteDY = function() {
return this.dy;
} //end getSpriteDY()
this.spriteReport = function(size) {
if (size === "full") {
console.log("Name: " + this.name);
console.log("X: " + this.x);
console.log("Y: " + this.y);
console.log("DX: " + this.dx);
console.log("DY: " + this.dy);
console.log("Shown: " + this.shown);
console.log("Image File: " + this.file);
console.log("Width: " + this.width);
console.log("Height: " + this.height);
} //endif
if (size === "partial") {
console.log("Name: " + this.name);
console.log("X: " + this.x);
console.log("Y: " + this.y);
console.log("Sprite is Moving: " + this.isMoving);
console.log("Shown: " + this.shown);
} //endif
} //end spriteReport()
this.getSpritePosition = function() {
spritePosition = Math.sqrt((this.x * this.x) + (this.y * this.y));
return spritePosition;
} //end getSpritePosition()
this.setPosition = function(newX, newY) {
this.x = newX;
this.y = newY;
if (newX === isNaN) {
console.log("Failure! X was not set to a number. You wrote " + newX);
} //endif
if (newY === isNaN) {
console.log("Failure! Y was not set to a number. You wrote " + newY);
} //endif
} //end setPosition()
this.setX = function(newX) {
this.x = newX;
} //end setX()
this.setY = function(newY) {
this.y = newY;
} //end setY()
this.setSpeed = function(newDX, newDY) {
this.dx = newDX;
this.dy = newDY;
if (newDX === isNaN) {
console.log("Failure! DX was not set to a number. You wrote " + newDX);
} //endif
if (newDY === isNaN) {
console.log("Failure! DY was not set to a number. You wrote " + newDY);
} //endif
} //end setSpeed()
this.setDX = function(newDX) {
this.dx = newDX;
} //end setDX()
this.setDY = function(newDY) {
this.dy = newDY;
} //end setDY()
this.hide = function() {
this.shown = false;
} //end hide()
this.show = function() {
this.shown = true;
} //end show()
this.draw = function(){
ctx.save();
ctx.translate(this.x, this.y);
ctx.rotate(this.imgAngle);
ctx.drawImage(this.image, this.x, this.y, this.width, this.height);
ctx.restore();
} // end draw()
this.update = function() {
this.x += this.dx;
this.y += this.dy;
if (this.visible){
this.draw();
} // end if
} // end update
this.collides = function(object) {
if (this.visible && object.visible) {
thisLeft = this.x - (this.width / 2);
thisRight = this.x + (this.width / 2);
thisTop = this.y - (this.height / 2);
thisBottom = this.y + (this.height / 2);
objLeft = sprite.x - (object.width / 2);
objRight = sprite.x + (object.width / 2);
objTop = sprite.y - (object.height / 2);
objBottom = sprite.y + (object.height / 2);
collision = true;
if ((thisBottom < objTop) ||
(thisTop > objBottom) ||
(thisRight < objLeft) ||
(thisLeft > objRight)) {
collision = false;
} //endif
} //endif
} //end collides()
} //end Sprite()