|
| 1 | +var game = { |
| 2 | + blockSize: 30, |
| 3 | + objects: [] |
| 4 | +}; |
| 5 | + |
| 6 | +function makeClasses() { |
| 7 | + game.Block = function(x, y, w, h) { |
| 8 | + this.sprite = createSprite(x, y, w, h); |
| 9 | + }; |
| 10 | + game.Block.prototype = { |
| 11 | + tick: function() {}, |
| 12 | + collideWith: function(target) {} |
| 13 | + }; |
| 14 | + |
| 15 | + game.Ground = function(x, y, w, h) { |
| 16 | + game.Block.call(this, x, y, w, h); |
| 17 | + this.sprite.shapeColor = color(80, 180, 100); |
| 18 | + //this.sprite.immovable = true; |
| 19 | + }; |
| 20 | + game.Ground.prototype = { |
| 21 | + tick: function() { |
| 22 | + game.Block.prototype.tick.call(this); |
| 23 | + }, |
| 24 | + collideWith: function(target) { |
| 25 | + console.log(target); |
| 26 | + game.Block.prototype.collideWith.call(this, target); |
| 27 | + target.sprite.velocity.y = 0; |
| 28 | + } |
| 29 | + }; |
| 30 | + |
| 31 | + game.PhysicsObject = function(x, y, w, h) { |
| 32 | + game.Block.call(this, x, y, w, h); |
| 33 | + this.gravity = createVector(0, 0.2); |
| 34 | + }; |
| 35 | + game.PhysicsObject.prototype = { |
| 36 | + tick: function() { |
| 37 | + game.Block.prototype.tick.call(this); |
| 38 | + this.sprite.velocity.add(this.gravity); |
| 39 | + }, |
| 40 | + collideWith: function(target) { |
| 41 | + game.Block.prototype.collideWith.call(this, target); |
| 42 | + } |
| 43 | + }; |
| 44 | + |
| 45 | + game.Player = function(x, y) { |
| 46 | + game.PhysicsObject.call(this, x, y, game.blockSize, game.blockSize); |
| 47 | + this.sprite.shapeColor = color(255, 0, 0); |
| 48 | + this.jump = createVector(0, -15); |
| 49 | + }; |
| 50 | + game.Player.prototype = { |
| 51 | + tick: function() { |
| 52 | + game.PhysicsObject.prototype.tick.call(this); |
| 53 | + if (keyDown('RIGHT_ARROW')) { |
| 54 | + this.sprite.velocity.x = 5; |
| 55 | + } else if (keyDown('LEFT_ARROW')) { |
| 56 | + this.sprite.velocity.x = -5; |
| 57 | + } else { |
| 58 | + this.sprite.velocity.x = 0; |
| 59 | + } |
| 60 | + |
| 61 | + if (keyWentDown('UP_ARROW') && this.sprite.velocity.y == 0) { |
| 62 | + this.sprite.velocity.y = this.jump.y; |
| 63 | + } |
| 64 | + }, |
| 65 | + collideWith: function(target) { |
| 66 | + game.PhysicsObject.prototype.collideWith.call(this, target); |
| 67 | + } |
| 68 | + }; |
| 69 | +} |
| 70 | + |
| 71 | +function setup() { |
| 72 | + makeClasses(); |
| 73 | + createCanvas(800, 600); |
| 74 | + objects = [new game.Player(400, 300), new game.Ground(200, 400, 300, 30)]; |
| 75 | +} |
| 76 | + |
| 77 | +function draw() { |
| 78 | + background(255, 220, 180); |
| 79 | + objects.forEach(function(obj) { |
| 80 | + obj.tick(); |
| 81 | + }); |
| 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]); |
| 88 | + }; |
| 89 | + } |
| 90 | + } |
| 91 | + drawSprites(); |
| 92 | +} |
0 commit comments