|
| 1 | + |
| 2 | +var game = new Phaser.Game(800, 600, Phaser.AUTO, 'phaser-example', { preload: preload, create: create, update: update }); |
| 3 | + |
| 4 | +function preload() { |
| 5 | + |
| 6 | + game.load.image('maggot', 'assets/sprites/maggot.png'); |
| 7 | + |
| 8 | +} |
| 9 | + |
| 10 | +var batch; |
| 11 | +var dudeBoundsPadding = 100; |
| 12 | +var dudeBounds = new Phaser.Rectangle(-dudeBoundsPadding, -dudeBoundsPadding, 800 + dudeBoundsPadding * 2, 600 + dudeBoundsPadding * 2); |
| 13 | +var tick = 0; |
| 14 | + |
| 15 | +function create() { |
| 16 | + |
| 17 | + batch = game.add.spriteBatch(); |
| 18 | + |
| 19 | + var total = (game.renderType === Phaser.WEBGL) ? 10000 : 100; |
| 20 | + |
| 21 | + for (var i = 0; i < total; i++) |
| 22 | + { |
| 23 | + var dude = batch.create(game.world.randomX, game.world.randomY, 'maggot'); |
| 24 | + |
| 25 | + dude.anchor.set(0.5); |
| 26 | + dude.scale.set(0.8 + Math.random() * 0.3); |
| 27 | + dude.direction = Math.random() * Math.PI * 2; |
| 28 | + dude.turningSpeed = Math.random() - 0.8; |
| 29 | + dude.speed = (2 + Math.random() * 2) * 0.2; |
| 30 | + dude.offset = Math.random() * 100; |
| 31 | + } |
| 32 | + |
| 33 | +} |
| 34 | + |
| 35 | +function update() { |
| 36 | + |
| 37 | + batch.forEach(updateMaggot, this, false); |
| 38 | + |
| 39 | + tick += 0.1; |
| 40 | + |
| 41 | +} |
| 42 | + |
| 43 | +function updateMaggot(dude) { |
| 44 | + |
| 45 | + dude.scale.y = 0.95 + Math.sin(tick + dude.offset) * 0.05 |
| 46 | + dude.direction += dude.turningSpeed * 0.01; |
| 47 | + dude.position.x += Math.sin(dude.direction) * (dude.speed * dude.scale.y); |
| 48 | + dude.position.y += Math.cos(dude.direction) * (dude.speed * dude.scale.y); |
| 49 | + dude.rotation = -dude.direction + Math.PI; |
| 50 | + |
| 51 | + // wrap the dudes by testing their bounds.. |
| 52 | + if (dude.position.x < dudeBounds.x) |
| 53 | + dude.position.x += dudeBounds.width; |
| 54 | + else if (dude.position.x > dudeBounds.x + dudeBounds.width) |
| 55 | + dude.position.x -= dudeBounds.width; |
| 56 | + |
| 57 | + if (dude.position.y < dudeBounds.y) |
| 58 | + dude.position.y += dudeBounds.height; |
| 59 | + else if (dude.position.y > dudeBounds.y + dudeBounds.height) |
| 60 | + dude.position.y -= dudeBounds.height; |
| 61 | + |
| 62 | +} |
0 commit comments