-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathscene.js
99 lines (85 loc) · 2.25 KB
/
scene.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
function Scene() {
'use strict';
var items = [];
var self = this;
this.add = function(item) {
items.push(item);
}
// TODO: put all items in a matrix 12 by 5 so searching for an item is faster
this.isItemAt = function(x,y) {
for(var item in items) {
if(items[item].getX() == x && items[item].getY() == y) {
return true;
}
}
return false;
}
this.getItemAt = function(x,y) {
for(var item in items) {
if(items[item].getX() == x && items[item].getY() == y) {
return items[item];
}
}
return false;
}
this.draw = function(canvas) {
addStaticTextures(canvas);
applyGravity();
for(var item in items) {
items[item].draw(canvas, self);
}
}
function setAnimateDropDown(item) {
if(!(!(item.interval))) return;
// This executes the animations of falling in all items IF it is implemented
if(typeof item.fall == "function") {
item.fall();
}
var speed = 1/8; // means a blok per 50 milliseconds
item.interval = setInterval(function(){
item.setY(item.getY() - speed);
if(Math.floor(item.getY()) == item.getY()) {
clearInterval(item.interval);
item.interval = null;
if(typeof item.land == "function") {
item.land();
}
}
},50);
}
function applyGravity() {
for(var item in items) {
var x = items[item].getX();
var y = items[item].getY();
if(y > 0 && !self.isItemAt(x,y-1) && Math.floor(y) == y) {
setAnimateDropDown(items[item]);
}
}
}
function addStaticTextures(canvas) {
function addFloor() {
for(var i=4; i < 99; i+=4) {
canvas.drawTextureRealXY(i, 60, window.textures.floorTexture);
}
}
function addWall() {
for(var i=0; i < 63; i+=4) {
canvas.drawTextureRealXY(0, i, window.textures.wallTexture);
}
}
function addBackground() {
for(var i=4; i < 99; i+=8) {
canvas.drawTextureRealXY(i, 4, window.textures.backgroundTexture);
}
}
function addRailing() {
for(var i=0; i < 99; i+=4) {
canvas.drawTextureRealXY(i, 0, window.textures.railingTexture);
}
}
addFloor();
addWall();
addRailing();
addBackground();
}
}