forked from phaserjs/phaser
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathquadtree.php
More file actions
110 lines (82 loc) · 2.33 KB
/
Copy pathquadtree.php
File metadata and controls
110 lines (82 loc) · 2.33 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
<!DOCTYPE HTML>
<html>
<head>
<title>phaser.js - a new beginning</title>
<?php
require('js.php');
?>
</head>
<body>
<script type="text/javascript">
(function () {
var game = new Phaser.Game(800, 600, Phaser.CANVAS, '', { preload: preload, create: create, update: update, render: render });
function preload() {
game.load.image('ship', 'assets/sprites/xenon2_ship.png');
game.load.image('baddie', 'assets/sprites/space-baddie.png');
}
var ship;
var total;
var aliens;
function create() {
game.world.setSize(2000, 2000);
aliens = [];
for (var i = 0; i < 1000; i++)
{
var s = game.add.sprite(game.world.randomX, game.world.randomY, 'baddie');
s.name = 'alien' + s;
s.body.collideWorldBounds = true;
s.body.bounce.setTo(1, 1);
s.body.velocity.setTo(10 + Math.random() * 10, 10 + Math.random() * 10);
aliens.push(s);
}
ship = game.add.sprite(400, 400, 'ship');
ship.body.collideWorldBounds = true;
ship.body.bounce.setTo(0.5, 0.5);
}
function update() {
for (var i = 0; i < aliens.length; i++)
{
aliens[i].alpha = 0.3;
}
total = game.physics.quadTree.retrieve(ship);
// Get the ships top-most ID. If the length of that ID is 1 then we can ignore every other result,
// it's simply not colliding with anything :)
for (var i = 0; i < total.length; i++)
{
total[i].sprite.alpha = 1;
}
if (game.input.keyboard.isDown(Phaser.Keyboard.LEFT))
{
ship.body.velocity.x -= 2;
}
else if (game.input.keyboard.isDown(Phaser.Keyboard.RIGHT))
{
ship.body.velocity.x += 2;
}
if (game.input.keyboard.isDown(Phaser.Keyboard.UP))
{
ship.body.velocity.y -= 2;
}
else if (game.input.keyboard.isDown(Phaser.Keyboard.DOWN))
{
ship.body.velocity.y += 2;
}
}
function render() {
for (var i = 0; i < aliens.length; i++)
{
// game.debug.renderRectangle(aliens[i].bounds);
}
game.debug.renderText(total.length, 32, 32);
game.debug.renderQuadTree(game.physics.quadTree);
// game.debug.renderRectangle(ship);
game.debug.renderText('Index: ' + ship.body.quadTreeIndex, 32, 80);
for (var i = 0; i < ship.body.quadTreeIDs.length; i++)
{
game.debug.renderText('ID: ' + ship.body.quadTreeIDs[i], 32, 100 + (i * 20));
}
}
})();
</script>
</body>
</html>