forked from Ezharjan/Web-basedRacerGame
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscenario.js
145 lines (118 loc) · 3.04 KB
/
scenario.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
function Scenario() {
var roadImage = new Image();
var roadImageWidth = 500;
var roadImageHeight = 600;
this.trees = [];
this.initialize = function (canvas) {
this.canvas = canvas;
this.canvas.style.backgroundColor = "green";
this.x = canvas.width / 2 - roadImageWidth / 2;
this.y = 0;
this.y2 = -roadImageHeight;
roadImage.src = "sprites/scenario/road.jpg";
this.createTrees();
};
this.drawRoad = function (context) {
context.drawImage(
roadImage,
this.x,
this.y,
roadImageWidth,
roadImageHeight
);
context.drawImage(
roadImage,
this.x,
this.y2,
roadImageWidth,
roadImageHeight
);
if (this.isThereOil()) {
this.oil.draw(context);
}
if (this.isTherePothole()) {
this.pothole.draw(context);
}
for (var i = 0; i < this.trees.length; i++) {
this.trees[i].draw(context);
}
};
this.updateRoad = function (emptyLane) {
this.y += 1;
if (this.y >= roadImageHeight) {
this.y = 0;
}
this.y2 += 1;
if (this.y2 >= 0) {
this.y2 = -roadImageHeight;
}
if (this.hasObstaclesOnRoad()) {
this.updateObstacles();
} else {
this.tryPutAnObstacleOnRoad(emptyLane);
}
this.updateTrees();
};
this.updateObstacles = function () {
if (this.isThereOil()) {
this.oil.update(this.canvas.height);
}
if (this.isTherePothole()) {
this.pothole.update(this.canvas.height);
}
};
this.updateTrees = function () {
for (var i = 0; i < this.trees.length; i++) {
this.trees[i].update(this.canvas.height);
}
};
this.tryPutAnObstacleOnRoad = function (emptyLane) {
var newObstacleProbability = Math.random();
if (newObstacleProbability < 0.002) {
this.putAnObstacleOnRoad(emptyLane);
}
};
this.putAnObstacleOnRoad = function (emptyLane) {
var typeObstacleProbability = Math.random();
if (typeObstacleProbability < 0.7) {
this.createPothole(emptyLane);
} else {
this.createOil(emptyLane);
}
};
this.createPothole = function (emptyLane) {
if (!this.pothole) {
this.pothole = new Obstacle();
}
this.pothole.initialize(emptyLane, GameConfig.obstacle.pothole);
};
this.createOil = function (emptyLane) {
if (!this.oil) {
this.oil = new Obstacle();
}
this.oil.initialize(emptyLane, GameConfig.obstacle.oil);
};
this.hasObstaclesOnRoad = function () {
return this.isThereOil() || this.isTherePothole();
};
this.isThereOil = function () {
return this.oil && this.oil.isOnRoad;
};
this.isTherePothole = function () {
return this.pothole && this.pothole.isOnRoad;
};
this.createTrees = function () {
var treePositionIndex = 0;
var treeSide = 0;
for (var i = 0; i < 10; i++) {
this.trees[i] = new Tree();
this.trees[i].initialize(treePositionIndex, treeSide);
if (treePositionIndex > 3) {
treePositionIndex = 0;
treeSide = 1;
} else {
treePositionIndex++;
}
}
};
}