Changed player starting position
[ld38.git] / ld38.js
1 var game = new Phaser.Game(800, 600, Phaser.AUTO, '', { preload: preload, create: create, update: update });
2 var walls;
3 var gems;
4 var shrinklevel = 0;
5
6 const WAIT_KEY = 200;
7 const WAIT_GEM = 2000;
8 const WALL_THICKNESS = 32;
9 const MAX_GEMS = 16;
10
11 var newWorldStartX;
12 var newWorldEndX;
13 var newWorldStartY;
14 var newWorldEndY;
15
16 window.addEventListener("keydown", function(e) {
17 // Prevent default browser action for arrows and spacebar
18 if([32, 37, 38, 39, 40].indexOf(e.keyCode) > -1) {
19 e.preventDefault();
20 }
21 }, false);
22
23 class Entity extends Phaser.Sprite {
24 constructor(x, y, sprite) {
25 super(game, x, y, sprite);
26 this.anchor.setTo(.5,.5);
27 }
28
29 enablePhysics() {
30 game.physics.arcade.enable(this);
31 this.body.bounce.y = 0.2;
32 this.body.bounce.x = 0.2;
33 this.body.collideWorldBounds = true;
34 }
35 }
36
37 class Player extends Entity {
38 constructor(x, y, sprite) {
39 super(x, y, sprite);
40 this.lastmovetime = 0;
41 }
42 }
43
44 class Gem extends Entity {
45 constructor(x, y) {
46 super(x, y, 'gem');
47 this.lastInteraction = 0;
48 this.isActivated = false;
49 }
50
51 activate() {
52 this.isActivated = true;
53 this.loadTexture('gem_active');
54 }
55
56 deactivate() {
57 this.isActivated = false;
58 this.loadTexture('gem');
59 }
60 }
61
62 function sign(n) {
63 if (n >= 0) { return 1 } else { return -1 };
64 }
65
66 function preload () {
67
68 game.load.image('wall', 'wall.png');
69 game.load.image('player', 'player.png');
70 game.load.image('gem', 'gem.png');
71 game.load.image('gem_active', 'gem_active.png');
72
73 }
74
75 function create () {
76
77 game.world.setBounds(0, 0, 800, 600);
78 game.stage.backgroundColor = 'black';
79 game.physics.startSystem(Phaser.Physics.ARCADE);
80
81 walls = game.add.group();
82 walls.classType = Phaser.TileSprite;
83 walls.enableBody = true;
84
85 walls.add(game.add.tileSprite(0, 0, game.world.width, WALL_THICKNESS, 'wall'));
86 walls.add(game.add.tileSprite(0, game.world.height - WALL_THICKNESS, game.world.width, WALL_THICKNESS, 'wall'));
87 walls.add(game.add.tileSprite(0, 0, WALL_THICKNESS, game.world.height, 'wall'));
88 walls.add(game.add.tileSprite(game.world.width - WALL_THICKNESS, 0, WALL_THICKNESS, game.world.height, 'wall'));
89 walls.children.forEach(function(wall) { wall.body.immovable = true; });
90
91 gems = game.add.group();
92 for (var i = 0; i <= MAX_GEMS; i++)
93 gems.add(new Gem((Math.random() * (game.world.width - (WALL_THICKNESS * 4))) + (WALL_THICKNESS * 2), (Math.random() * (game.world.height - (WALL_THICKNESS * 4))) + (WALL_THICKNESS * 2)));
94 gems.children.forEach(function(gem) { gem.enablePhysics(); });
95
96 player = new Player(128, game.world.height - 128, 'player');
97 player.enablePhysics();
98 game.add.existing(player);
99 game.camera.follow(player);
100 cursors = game.input.keyboard.createCursorKeys();
101
102 keyboard_handler = keyPress_default;
103
104 newWorldStartX = WALL_THICKNESS;
105 newWorldEndX = game.world.width - WALL_THICKNESS;
106 newWorldStartY = WALL_THICKNESS;
107 newWorldEndY = game.world.height - WALL_THICKNESS;
108
109 helptext = game.add.text(10, 10, "Move with arrows, activate dimension crystals to shrink the world.", { align: 'left', fill: '#000000', fontSize: 14 });
110 helptext.font = "Ubuntu Mono";
111
112 }
113
114 function update () {
115
116 game.physics.arcade.collide(player, walls);
117 game.physics.arcade.overlap(player, gems, gem_overlap, gem_isInteractable);
118
119 var isAllActive = true;
120 gems.children.forEach(function(gem) { if (!gem.isActivated) { isAllActive = false; }});
121 if (isAllActive) {
122 world_shrink();
123 };
124
125 if ((game.time.now - player.lastmovetime) > WAIT_KEY) player.body.velocity.x = player.body.velocity.y = 0;
126 if (keyboard_handler) keyboard_handler();
127
128 }
129
130 function keyPress_default() {
131
132 if (cursors.left.isDown)
133 {
134 player.body.velocity.x = -150;
135 player.angle = -90;
136 player.lastmovetime = game.time.now;
137 }
138 else if (cursors.right.isDown)
139 {
140 player.body.velocity.x = 150;
141 player.angle = 90;
142 player.lastmovetime = game.time.now;
143 }
144 if (cursors.up.isDown)
145 {
146 player.body.velocity.y = -150;
147 player.angle = 0;
148 player.lastmovetime = game.time.now;
149 }
150 else if (cursors.down.isDown)
151 {
152 player.body.velocity.y = 150;
153 player.angle = 180;
154 player.lastmovetime = game.time.now;
155 }
156
157 }
158
159 function gem_isInteractable(player, gem) {
160 return (game.time.now - gem.lastInteraction) > WAIT_GEM;
161 }
162
163 function gem_overlap(player, gem) {
164
165 gem.lastInteraction = game.time.now;
166 gem.activate();
167
168 }
169
170 function world_shrink() {
171
172 if (player.x < (newWorldStartX + (newWorldEndX - newWorldStartX) / 2))
173 newWorldEndX -= (newWorldEndX - newWorldStartX) / 2;
174 else
175 newWorldStartX += (newWorldEndX - newWorldStartX) / 2;
176 if (player.y < (newWorldStartY + (newWorldEndY - newWorldStartY) / 2))
177 newWorldEndY -= (newWorldEndY - newWorldStartY) / 2;
178 else
179 newWorldStartY += (newWorldEndY - newWorldStartY) / 2;
180
181 // Draw a wall around the new world bounds.
182 walls.add(game.add.tileSprite(newWorldStartX - WALL_THICKNESS, newWorldStartY - WALL_THICKNESS, newWorldEndX - newWorldStartX + WALL_THICKNESS * 2, WALL_THICKNESS, 'wall'));
183 walls.add(game.add.tileSprite(newWorldStartX - WALL_THICKNESS, newWorldEndY, newWorldEndX - newWorldStartX + WALL_THICKNESS * 2, WALL_THICKNESS, 'wall'));
184 walls.add(game.add.tileSprite(newWorldStartX - WALL_THICKNESS, newWorldStartY - WALL_THICKNESS, WALL_THICKNESS, newWorldEndY - newWorldStartY + WALL_THICKNESS * 2, 'wall'));
185 walls.add(game.add.tileSprite(newWorldEndX, newWorldStartY - WALL_THICKNESS, WALL_THICKNESS, newWorldEndY - newWorldStartY + WALL_THICKNESS * 2, 'wall'));
186 walls.children.forEach(function(wall) { wall.body.immovable = true; });
187
188 function relocateGem(gem) {
189 if (gem.x < newWorldStartX) gem.x = newWorldStartX + (newWorldStartX - gem.x);
190 else if (gem.x > newWorldEndX) gem.x = newWorldEndX + (newWorldEndX - gem.x); // Adding negative value is like substraction.
191 if (gem.y < newWorldStartY) gem.y = newWorldStartY + (newWorldStartY - gem.y);
192 else if (gem.y > newWorldEndY) gem.y = newWorldEndY + (newWorldEndY - gem.y); // Same.
193 gem.deactivate();
194 }
195 gems.children.forEach(relocateGem);
196
197 }