-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsketch.js
More file actions
141 lines (129 loc) · 3.37 KB
/
sketch.js
File metadata and controls
141 lines (129 loc) · 3.37 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
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
'use strict'
const width = window.innerWidth * (10/16)
const height = window.innerHeight * 0.75
const substratePopulation = 50
const enzymePopulation = 2
const dots = []
let reactions = 0
const enzymes = []
const substrates = []
const inactiveSubstrates = []
function setup() {
var canvas = createCanvas(width, height)
canvas.parent('sketch-holder');
createPopulation()
}
function draw() {
background(51)
moveDots()
updateText()
}
function updateText() {
select('#reactions').html(`Reaktionen: ${reactions}`)
}
function checkComplete() {
if (substratePopulation - inactiveSubstrates.length == 0) {
noLoop()
}
}
function moveDots() {
for (const dot of dots) {
if(dot.active) {
dot.move()
fill(dot.colour)
dot.render()
for (const dot2 of dots) {
checkCollision(dot, dot2)
}
}
}
}
//Checks if entitys collide
/*function checkCollision(dot, dot2) {
//Calculates distance between dot and dot2, checks if the distance between is smaller than the size of the dots -> collision happened
//If so checks if one of the dots is infected, then infects the other.
if (dist(dot.pos.x, dot.pos.y, dot2.pos.x, dot2.pos.y) < dot.size
&& isOneInfected(dot, dot2)) {
dot.infect()
dot2.infect()
infected.push(dot.infected)
}
}*/
function checkCollision(dot, dot2) {
//Calculates distance between dot and dot2, checks if the distance between is smaller than the size of the dots -> collision happened
//If so checks if one of the dots is infected, then infects the other.
if (dist(dot.pos.x, dot.pos.y, dot2.pos.x, dot2.pos.y) < dot.size
&& isOneEnzyme(dot, dot2) && dot.active && dot2.active) {
console.log("Collision!")
if(dot.enzyme) {
//Do nothing to the enzyme
deleteEntity(dot2)
if(dot.reacted) {
//Enzyme completed reaction
console.log("Reaction complete")
reactions += 1
dot.reacted = false
dot.colour = color(255, 0, 0)
} else {
console.log("Reacted = true")
dot.reacted = true
dot.colour = color(255, 193, 7)
}
} else {
deleteEntity(dot)
if(dot2.reacted) {
//Enzyme completed reaction
reactions += 1
dot2.reacted = false
dot2.colour = color(255, 0, 0)
} else {
dot2.reacted = true
dot2.colour = color(255, 193, 7)
}
}
}
}
function isOneEnzyme(dot, dot2) {
//Returns if one of the dots is an and enzyme, or if both are enzymes
return (dot.enzyme || dot2.enzyme) && !areBothEnzymes(dot, dot2)
}
function areBothEnzymes(dot, dot2) {
//Returns if both entitys are enzymes
return dot.enzyme && dot2.enzyme
}
function deleteEntity(dot) {
dot.active = false;
inactiveSubstrates.push(dot)
console.log("Entity deleted")
}
/*
function isOneInfected(dot, dot2) {
return (dot.infected || dot2.infected) && !areBothInfected(dot, dot2)
}
function areBothInfected(dot, dot2) {
return dot.infected && dot2.infected
}
*/
function createPopulation() {
for (let i = 0; i < substratePopulation; i++) {
createSubstrate()
}
for(let i = 0; i < enzymePopulation; i++) {
createEnzyme()
}
}
function createEnzyme() {
const enzyme = new Dot(new Coordinate(width, height), 32, true, false, true)
dots.push(enzyme)
enzymes.push(enzyme)
}
function createSubstrate() {
const substrate = new Dot(new Coordinate(width, height), 32, false, false, true)
dots.push(substrate)
substrates.push(substrate)
}
function changeSpeed(speed) {
for (const dot of dots) {
dot.speed = new Coordinate(speed, speed)
}
}