-
Notifications
You must be signed in to change notification settings - Fork 0
/
drunk.js
129 lines (113 loc) · 3.59 KB
/
drunk.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
function Drunk(x, y, img) {
this.x = x;
this.y = y;
this.img = img;
this.speed = scl;
this.show = function() {
image(this.img, this.x, this.y);
}
this.move = function() {
var randomizedx = round(random(-1, 1)) * scl;
var randomizedy = round(random(-1, 1)) * scl;
var resultx;
var resulty;
// X RANDOMIZED --------------------------
for (var i = 0; i < tiles.length; i++) {
if (this.x + scl === tiles[i].x && this.y === tiles[i].y) { // if bump into wall on the right
this.speed = -scl;
this.x = this.x + this.speed;
} else
if (this.x - scl === tiles[i].x && this.y === tiles[i].y) { // if bump into wall on the left
this.speed = scl;
this.x = this.x + this.speed;
}
}
if (randomizedx < -scl / 2 && randomizedx >= -scl ) {
var resultx = -scl;
this.x = this.x + resultx;
} else
if (randomizedx < scl / 2 && randomizedx >= -scl / 2) {
var resultx = 0;
this.x = this.x + resultx;
} else
if (randomizedx <= scl && randomizedx >= scl / 2 ) {
var resultx = scl;
this.x = this.x + resultx;
}
// RANDOMIZED Y
for (var j = 0; j < tiles.length; j++) {
if (this.y + scl === tiles[j].y && this.x === tiles[j].x) { // bump into up wall
this.speed = -scl;
this.y = this.y + this.speed;
}else
if (this.y - scl === tiles[j].y && this.x === tiles[j].x) {
this.speed = scl;
this.y = this.y + this.speed;
}
}
if (randomizedy < -scl / 2 && randomizedy >= -scl) {
var resulty = -scl;
this.y = this.y + resulty;
}
if (randomizedy < scl / 2 && randomizedy >= -scl / 2) {
var resulty = 0;
this.y = this.y + resulty;
}
if (randomizedy <= scl && randomizedy >= scl / 2) {
var resulty = scl;
this.y = this.y + resulty;
}
}
}
// this was working but going through walls
// for (var i = 0; i < tiles.length; i++) {
// if (this.x + scl === tiles[i].x && this.y === tiles[i].y) { // if bump into wall on the right
// this.speed = -scl;
// this.x = this.x + this.speed;
// }
// }
// for (var i = 0; i < tiles.length; i++) {
// if (this.x - scl === tiles[i].x && this.y === tiles[i].y) { // if bump into wall on the left
// this.speed = scl;
// this.x = this.x + this.speed;
// }
// }
// if (randomizedx < -scl / 2 && randomizedx >= -scl) {
// var resultx = -scl;
// this.x = this.x + resultx;
// }
// if (randomizedx < scl / 2 && randomizedx >= -scl / 2) {
// var resultx = 0;
// this.x = this.x + resultx;
// }
// if (randomizedx <= scl && randomizedx >= scl / 2) {
// var resultx = scl;
// this.x = this.x + resultx;
// }
// // RANDOMIZED Y
// for (var i = 0; i < tiles.length; i++) {
// if (this.y + scl === tiles[i].y && this.x === tiles[i].x) { // bump into up wall
// this.speed = -scl;
// this.y = this.y + this.speed;
// }
// }
// for (var i = 0; i < tiles.length; i++) {
// if (this.y - scl === tiles[i].y && this.x === tiles[i].x) {
// this.speed = scl;
// this.y = this.y + this.speed;
// }
// }
// if (randomizedy < -scl / 2 && randomizedy >= -scl) {
// var resulty = -scl;
// this.y = this.y + resulty;
// }
// if (randomizedy < scl / 2 && randomizedy >= -scl / 2) {
// var resulty = 0;
// this.y = this.y + resulty;
// }
// if (randomizedy <= scl && randomizedy >= scl / 2) {
// var resulty = scl;
// this.y = this.y + resulty;
// }
// }
// }