Skip to content

Commit 5e33085

Browse files
committed
Add objects, object movement
1 parent 82d8bc8 commit 5e33085

File tree

6 files changed

+112
-7
lines changed

6 files changed

+112
-7
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
1+
.vscode
12
node_modules
23
todo.txt

src/index.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,11 @@ var app = express();
1010
app.use(express.static(path.join(__dirname, publicDir)));
1111

1212
app.use((req, res, next) => {
13-
res.status(404).sendFile(path.join(__dirname, errorDir, '404.html'));
13+
res.status(404).sendFile(path.join(errorDir, '404.html'));
1414
});
1515

1616
app.use((err, req, res, next) => {
17-
res.status(500).sendFile(path.join(__dirname, errorDir, '500.html'));
17+
res.status(500).sendFile(path.join(errorDir, '500.html'));
1818
});
1919

2020
app.listen(port, () => {

src/public/bin.js

+11-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,14 @@
11
class Bin {
2-
constructor() {
3-
2+
constructor(binType, x, y, width, height) {
3+
this.type = binType;
4+
this.x = x;
5+
this.y = y;
6+
this.width = width;
7+
this.height = height;
8+
this.items = [];
9+
}
10+
11+
draw() {
12+
rect(this.x, this.y, this.width, this.height);
413
}
514
}

src/public/index.html

+13-1
Original file line numberDiff line numberDiff line change
@@ -1 +1,13 @@
1-
<!-- TODO: create webpage structure -->
1+
<!DOCTYPE html>
2+
<html>
3+
<head>
4+
<title>Go Recycle!</title>
5+
<link rel="stylesheet" type="text/css" href="main.css">
6+
<script type="text/javascript" src="trash.js"></script>
7+
<script type="text/javascript" src="bin.js"></script>
8+
<script type="text/javascript" src="main.js"></script>
9+
<script src="https://cdnjs.cloudflare.com/ajax/libs/p5.js/0.9.0/p5.min.js" integrity="sha256-WVsM3xrcqyuFNF3W1qtIKbHFsD0977nDQA8DCMp1zCw=" crossorigin="anonymous"></script>
10+
</head>
11+
<body>
12+
</body>
13+
</html>

src/public/main.js

+19-1
Original file line numberDiff line numberDiff line change
@@ -1 +1,19 @@
1-
// TODO: build sketch
1+
var trash;
2+
var bin;
3+
4+
function setup() {
5+
createCanvas(800, 600);
6+
trash = new Trash();
7+
bin = new Bin(trashNames[0], width - 50, 10, 40, 40);
8+
}
9+
10+
function draw() {
11+
background(220);
12+
if (trash.moveToward(bin)) {
13+
bin.draw();
14+
trash.draw();
15+
} else {
16+
// now trash is in the bin
17+
console.log('reached the bin!');
18+
}
19+
}

src/public/trash.js

+66-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,70 @@
1+
const movementConstant = 20;
2+
3+
const trashNames = {
4+
'plastic/metal': [
5+
6+
],
7+
'cardboard': [
8+
9+
],
10+
'paper': [
11+
12+
],
13+
'landfill': [
14+
15+
],
16+
'compost': [
17+
18+
]
19+
};
20+
21+
function randomTrashType() {
22+
var trashTypes = Object.keys(trashNames);
23+
return trashTypes[Math.floor(Math.random() * trashTypes.length)];
24+
}
25+
26+
function randomTrashName(trashType) {
27+
var trashTypeNames = trashNames[trashType];
28+
return trashTypeNames[Math.floor(Math.random() * trashTypeNames.length)];
29+
}
30+
131
class Trash {
232
constructor() {
3-
33+
this.type = randomTrashType();
34+
this.name = randomTrashName(this.type);
35+
this.x = width / 2;
36+
this.y = height - 10;
37+
this.scale = 1;
38+
}
39+
40+
moveToward(bin) {
41+
if (this.x === bin.x && this.y === bin.y) return false;
42+
var angle = Math.atan((this.y - bin.y) / (bin.x - this.x));
43+
if (angle < 0) angle += Math.PI;
44+
var newX = this.x + movementConstant * Math.cos(angle);
45+
var newY = this.y - movementConstant * Math.sin(angle);
46+
var distFromPrev = Math.sqrt((newX - this.x) ** 2 + (newY - this.y) ** 2);
47+
var distFromBin = Math.sqrt((newX - bin.x) ** 2 + (newY - bin.y) ** 2);
48+
if (distFromBin >= distFromPrev) {
49+
this.x = newX;
50+
this.y = newY;
51+
return true;
52+
} else {
53+
this.x = bin.x;
54+
this.y = bin.y;
55+
return false;
56+
}
57+
}
58+
59+
draw() {
60+
rect(this.x, this.y, 10, 10);
61+
}
62+
63+
inCorrectBin(bin) {
64+
return this.type === bin.type;
65+
}
66+
67+
putInBin(bin) {
68+
bin.items.push(this);
469
}
570
}

0 commit comments

Comments
 (0)