Skip to content

Commit 18ac312

Browse files
committed
Got collision detection to work
1 parent 87e4d40 commit 18ac312

File tree

3 files changed

+53
-22
lines changed

3 files changed

+53
-22
lines changed

index.html

+3-3
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,10 @@
55
Learnathon Platformer
66
</title>
77
<link rel='stylesheet' type='text/css' href='css/style.css' />
8-
</head>
9-
<body>
10-
<script type='text/javascript' src='lib/p5.min.js'></script>
8+
<script type='text/javascript' src='lib/p5.js'></script>
119
<script type='text/javascript' src='lib/p5.play.js'></script>
1210
<script type='text/javascript' src='src/platformer.js'></script>
11+
</head>
12+
<body>
1313
</body>
1414
</html>

setup.sh

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#!/usr/bin/env bash
22

3+
rm -rf lib
34
mkdir -p lib
4-
wget -P lib 'https://github.com/processing/p5.js/releases/download/0.4.22/p5.min.js'
5-
wget -P lib 'https://raw.githubusercontent.com/molleindustria/p5.play/master/lib/p5.play.js'
5+
wget -P lib 'https://raw.githubusercontent.com/molleindustria/p5.play/ed41d31c0ab6eaba89669de07ceaa29c888ed120/examples/lib/p5.js'
6+
wget -P lib 'https://raw.githubusercontent.com/molleindustria/p5.play/ed41d31c0ab6eaba89669de07ceaa29c888ed120/examples/lib/p5.play.js'

src/platformer.js

+47-17
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,63 @@
11
var game = {
22
blockSize: 30,
3+
ground: null,
4+
solids: null,
5+
physics: null,
36
objects: []
47
};
58

69
function makeClasses() {
10+
game.solids = new Group();
11+
game.physics = new Group();
12+
game.ground = new Group();
13+
714
game.Block = function(x, y, w, h) {
815
this.sprite = createSprite(x, y, w, h);
916
};
1017
game.Block.prototype = {
11-
tick: function() {},
12-
collideWith: function(target) {}
18+
tick: function() { },
19+
interact: function() { },
20+
collideWith: function(target) { }
1321
};
1422

1523
game.Ground = function(x, y, w, h) {
1624
game.Block.call(this, x, y, w, h);
1725
this.sprite.shapeColor = color(80, 180, 100);
18-
//this.sprite.immovable = true;
26+
this.sprite.immovable = true;
27+
game.solids.add(this.sprite);
28+
game.ground.add(this.sprite);
1929
};
2030
game.Ground.prototype = {
2131
tick: function() {
2232
game.Block.prototype.tick.call(this);
2333
},
34+
interact: function() {
35+
game.Block.prototype.interact.call(this);
36+
},
2437
collideWith: function(target) {
25-
console.log(target);
2638
game.Block.prototype.collideWith.call(this, target);
27-
target.sprite.velocity.y = 0;
2839
}
2940
};
3041

3142
game.PhysicsObject = function(x, y, w, h) {
3243
game.Block.call(this, x, y, w, h);
33-
this.gravity = createVector(0, 0.2);
44+
game.solids.add(this.sprite);
45+
game.physics.add(this.sprite);
46+
this.gravity = createVector(0, 1);
3447
};
3548
game.PhysicsObject.prototype = {
3649
tick: function() {
3750
game.Block.prototype.tick.call(this);
3851
this.sprite.velocity.add(this.gravity);
3952
},
53+
interact: function() {
54+
game.Block.prototype.interact.call(this);
55+
},
4056
collideWith: function(target) {
4157
game.Block.prototype.collideWith.call(this, target);
58+
if (this.sprite.touching.bottom) {
59+
this.sprite.velocity.y = this.gravity.y;
60+
}
4261
}
4362
};
4463

@@ -50,6 +69,10 @@ function makeClasses() {
5069
game.Player.prototype = {
5170
tick: function() {
5271
game.PhysicsObject.prototype.tick.call(this);
72+
},
73+
interact: function() {
74+
game.PhysicsObject.prototype.interact.call(this);
75+
console.log(this.sprite.velocity.y);
5376
if (keyDown('RIGHT_ARROW')) {
5477
this.sprite.velocity.x = 5;
5578
} else if (keyDown('LEFT_ARROW')) {
@@ -58,8 +81,12 @@ function makeClasses() {
5881
this.sprite.velocity.x = 0;
5982
}
6083

61-
if (keyWentDown('UP_ARROW') && this.sprite.velocity.y == 0) {
62-
this.sprite.velocity.y = this.jump.y;
84+
if (keyWentDown('UP_ARROW')) {
85+
this.sprite.position.y += 2;
86+
if (this.sprite.touching.bottom) {
87+
this.sprite.velocity.y = this.jump.y;
88+
}
89+
this.sprite.position.y -= 2;
6390
}
6491
},
6592
collideWith: function(target) {
@@ -69,24 +96,27 @@ function makeClasses() {
6996
}
7097

7198
function setup() {
99+
createCanvas(800, 500);
72100
makeClasses();
73-
createCanvas(800, 600);
74-
objects = [new game.Player(400, 300), new game.Ground(200, 400, 300, 30)];
101+
game.objects = [new game.Player(400, 300), new game.Ground(400, 400, 300, 80)];
75102
}
76103

104+
var n=0;
77105
function draw() {
78106
background(255, 220, 180);
79-
objects.forEach(function(obj) {
107+
game.objects.forEach(function(obj) {
80108
obj.tick();
81109
});
82-
for (var i=0; i<objects.length; i++) {
83-
for (var j=i+1; j<objects.length; j++) {
84-
if (objects[i].sprite.overlap(objects[j].sprite)) {
85-
console.log("touching");
86-
objects[i].collideWith(objects[j]);
87-
objects[j].collideWith(objects[i]);
110+
for (var i=0; i<game.objects.length; i++) {
111+
for (var j=i+1; j<game.objects.length; j++) {
112+
if (game.objects[i].sprite.collide(game.objects[j].sprite)) {
113+
game.objects[i].collideWith(game.objects[j]);
114+
game.objects[j].collideWith(game.objects[i]);
88115
};
89116
}
90117
}
118+
game.objects.forEach(function(obj) {
119+
obj.interact();
120+
});
91121
drawSprites();
92122
}

0 commit comments

Comments
 (0)