-
Notifications
You must be signed in to change notification settings - Fork 0
/
ImageHandler.js
82 lines (69 loc) · 2.3 KB
/
ImageHandler.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
// global, so images can raise this value.
var NumLoadedImages = 0;
var MapSize = 0;
// An object loading and organizing all images. Please create one only.
function ImageHandler() {
// Has the drawing been started jet?
this.gameStarted = false;
this.data = {};
this.numImages = 0;
// Returns the image.
this.GetImage = function (name) {
//if(!(typeof this.data[name] === Image)) alert("This image does not exist: "+name);
if(!this.gameStarted) alert("Game has not started jet - cannot Draw!");
return this.data[name];
}
// Add an image to be loaded. If the game has started already, this will fail.
this.AddImage = function (name, source) {
if (this.gameStarted) alert("Game started! Cannot load!");
this.data[name] = new Image();
this.data[name].src = source;
// Assert that loading is done before an image is shown.
this.numImages++;
this.data[name].addEventListener("load", function () {
NumLoadedImages++;
if (this.src == "map.bmp") {
MapSize = new Object();
MapSize.x = this.width;
MapSize.y = this.height;
alert("Loaded map");
}
}, false);
}
function waitUntilImageIsValid() {
if (this.numImages != NumLoadedImages) {
setTimeout(waitUntilImageIsValid, 500);
}
}
this.WaitForLoad = function () {
waitUntilImageIsValid();
this.gameStarted = true;
}
}
function Box(x, y, w, h) {
this.x = x;
this.y = y;
this.w = w;
this.h = h;
}
function ClickableBox(box, boxFunction) {
this.box = box;
this.boxFunction = boxFunction;
}
// Handles a number of clickable boxes and reacts to mouse click.
function ClickHandler() {
this.data = [];
this.AddButton = function(clickableBox) {
this.data.push(clickableBox);
}
// Calls the boxFunction of all boxes being clicked on.
this.Update = function (clickX, clickY) {
for (var key in this.data) {
var coordsBox = GetCoordsBox(this.data[key].box);
if (pos.x < clickX && pos.w + pos.x > clickX
&& pos.y < clickY && pos.y + pos.h > clickY) {
this.data[key].boxFunction();
}
}
}
}