-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdot.js
More file actions
73 lines (62 loc) · 1.69 KB
/
dot.js
File metadata and controls
73 lines (62 loc) · 1.69 KB
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
'use strict'
class Dot {
//Constructs shape
constructor(canvas, size, enzyme, reacted, active) {
this.pos = startLocation(canvas.x, canvas.y)
this.speed = new Coordinate(1,1)
this.direction = randCoordinate()
//Checks if dot is enzyme, if so changes color
this.colour = enzyme ? color(255, 0, 0) : color(255, 255, 255)
this.canvas = canvas
this.size = size
this.enzyme = enzyme
this.reacted = reacted
this.active = active
}
move() {
this.checkBoundaries()
this.pos = new Coordinate(this.pos.x + this.speed.x * this.direction.x, this.pos.y + this.speed.y * this.direction.y)
}
//If the shape collides with a wall, the direction is reverted
checkBoundaries() {
if (hitBoundary(this, 'x')) {
this.direction.x *= -1
}
if (hitBoundary(this, 'y')) {
this.direction.y *= -1
}
}
//Renders a circle
render() {
if(this.enzyme) {
rect(this.pos.x, this.pos.y, this.size * 1.5, this.size * 0.75)
} else {
ellipse(this.pos.x, this.pos.y, this.size)
}
}
}
function hitBoundary(dot, axis) {
if(dot.enzyme) {
if(axis == "x") {
return dot.pos[axis] > dot.canvas[axis] - (dot.size) || dot.pos[axis] < (dot.size)
} else {
return dot.pos[axis] > dot.canvas[axis] - (dot.size) || dot.pos[axis] < (dot.size)
}
} else {
return dot.pos[axis] > dot.canvas[axis] - dot.size || dot.pos[axis] < dot.size
}
}
function startLocation(x, y) {
console.log(x)
console.log(y)
return new Coordinate(randRange(x, 30), randRange(y, 50))
}
function randRange(max, min) {
return Math.ceil(Math.random() * (max - min) + min)
}
function rand(constraint) {
return Math.ceil(Math.random() * constraint)
}
function randCoordinate() {
return new Coordinate(rand(2), rand(2))
}