-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathborder.js
97 lines (86 loc) · 2.76 KB
/
border.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
var Border = function(collisionFunc,drawScore,heightFirst) {
if (heightFirst) {
this.heightFirst = heightFirst;
this.indent = 120;
this.heightSecond = 500 - (this.heightFirst + this.indent);
}
else this.setRandomHeight();
this.stepPx = 2;
this.poleWidth = parseInt($('.game').width()) + 50;
this.color = Helper.randowColor();
this.collisionFunc = collisionFunc;
this.score = 0;
this.drawScore = drawScore;
}
Border.prototype.setRandomHeight = function() {
this.heightFirst = Helper.randomInt(50,310);
this.indent = 120;
this.heightSecond = 500 - (this.heightFirst + this.indent);
}
Border.prototype.draw = function() {
var borders = document.createElement('div');
$(borders).addClass('borders');
var borderFirst = document.createElement('div');
$(borderFirst).addClass('border-top');
$(borderFirst).addClass('border');
$(borderFirst).css({
height: this.heightFirst,
backgroundColor: this.color,
});
$(borders).append(borderFirst);
var borderSecond = document.createElement('div');
$(borderSecond).addClass('border-bottom');
$(borderSecond).addClass('border');
$(borderSecond).css({
height: this.heightSecond,
marginTop: this.indent,
backgroundColor: this.color,
});
$(borders).append(borderSecond);
this.borders = borders;
$('.game').append(borders);
}
Border.prototype.move = function() {
if (!this.borders)
return;
$(this.borders).css({
marginRight: '+=' + this.stepPx + 'px',
});
this.chackCollision();
this.remove();
}
Border.prototype.remove = function() {
var currentMargin = parseInt($(this.borders).css('marginRight'));
if (currentMargin >= this.poleWidth) {
$(this.borders).remove();
this.setRandomHeight();
this.color = Helper.randowColor();
this.draw();
this.score++;
this.drawScore(this.score);
}
}
Border.prototype.chackCollision = function() {
var bird = {
x: $('.bird').offset().left,
y: $('.bird').offset().top,
width: $('.bird').width(),
height: $('.bird').height()
};
var borderTop = {
x: $('.border-top').offset().left,
y: $('.border-top').offset().top,
width: $('.border-top').width() - 10,
height: $('.border-top').height() - 10
};
var borderBottom = {
x: $('.border-bottom').offset().left,
y: $('.border-bottom').offset().top,
width: $('.border-bottom').width() - 10,
height: $('.border-bottom').height() - 10
};
if (Helper.MacroCollision(bird,borderTop))
this.collisionFunc();
else if (Helper.MacroCollision(bird,borderBottom))
this.collisionFunc();
}