f99fd63a12b60efe9298a767e7f5373d4ee39720
1 var game
= new Phaser
.Game(800, 600, Phaser
.AUTO
, '', { preload
: preload
, create
: create
, update
: update
});
8 const WALL_THICKNESS
= 32;
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) {
23 class Entity
extends Phaser
.Sprite
{
24 constructor(x
, y
, sprite
) {
25 super(game
, x
, y
, sprite
);
26 this.anchor
.setTo(.5,.5);
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;
37 class Player
extends Entity
{
38 constructor(x
, y
, sprite
) {
40 this.lastmovetime
= 0;
44 class Gem
extends Entity
{
47 this.lastInteraction
= 0;
48 this.isActivated
= false;
52 this.isActivated
= true;
53 this.loadTexture('gem_active');
57 this.isActivated
= false;
58 this.loadTexture('gem');
63 if (n
>= 0) { return 1 } else { return -1 };
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');
77 game
.world
.setBounds(0, 0, 800, 600);
78 game
.stage
.backgroundColor
= 'black';
79 game
.physics
.startSystem(Phaser
.Physics
.ARCADE
);
81 walls
= game
.add
.group();
82 walls
.classType
= Phaser
.TileSprite
;
83 walls
.enableBody
= true;
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; });
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(); });
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();
102 keyboard_handler
= keyPress_default
;
104 newWorldStartX
= WALL_THICKNESS
;
105 newWorldEndX
= game
.world
.width
- WALL_THICKNESS
;
106 newWorldStartY
= WALL_THICKNESS
;
107 newWorldEndY
= game
.world
.height
- WALL_THICKNESS
;
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";
116 game
.physics
.arcade
.collide(player
, walls
);
117 game
.physics
.arcade
.overlap(player
, gems
, gem_overlap
, gem_isInteractable
);
119 var isAllActive
= true;
120 gems
.children
.forEach(function(gem
) { if (!gem
.isActivated
) { isAllActive
= false; }});
125 if ((game
.time
.now
- player
.lastmovetime
) > WAIT_KEY
) player
.body
.velocity
.x
= player
.body
.velocity
.y
= 0;
126 if (keyboard_handler
) keyboard_handler();
130 function keyPress_default() {
132 if (cursors
.left
.isDown
)
134 player
.body
.velocity
.x
= -150;
136 player
.lastmovetime
= game
.time
.now
;
138 else if (cursors
.right
.isDown
)
140 player
.body
.velocity
.x
= 150;
142 player
.lastmovetime
= game
.time
.now
;
144 if (cursors
.up
.isDown
)
146 player
.body
.velocity
.y
= -150;
148 player
.lastmovetime
= game
.time
.now
;
150 else if (cursors
.down
.isDown
)
152 player
.body
.velocity
.y
= 150;
154 player
.lastmovetime
= game
.time
.now
;
159 function gem_isInteractable(player
, gem
) {
160 return (game
.time
.now
- gem
.lastInteraction
) > WAIT_GEM
;
163 function gem_overlap(player
, gem
) {
165 gem
.lastInteraction
= game
.time
.now
;
170 function world_shrink() {
172 if (player
.x
< (newWorldStartX
+ (newWorldEndX
- newWorldStartX
) / 2))
173 newWorldEndX
-= (newWorldEndX
- newWorldStartX
) / 2;
175 newWorldStartX
+= (newWorldEndX
- newWorldStartX
) / 2;
176 if (player
.y
< (newWorldStartY
+ (newWorldEndY
- newWorldStartY
) / 2))
177 newWorldEndY
-= (newWorldEndY
- newWorldStartY
) / 2;
179 newWorldStartY
+= (newWorldEndY
- newWorldStartY
) / 2;
181 // Draw a wall around the new world bounds.
182 walls
.removeAll(true);
183 walls
.add(game
.add
.tileSprite(newWorldStartX
- WALL_THICKNESS
, newWorldStartY
- WALL_THICKNESS
, newWorldEndX
- newWorldStartX
+ WALL_THICKNESS
* 2, WALL_THICKNESS
, 'wall'));
184 walls
.add(game
.add
.tileSprite(newWorldStartX
- WALL_THICKNESS
, newWorldEndY
, newWorldEndX
- newWorldStartX
+ WALL_THICKNESS
* 2, WALL_THICKNESS
, 'wall'));
185 walls
.add(game
.add
.tileSprite(newWorldStartX
- WALL_THICKNESS
, newWorldStartY
- WALL_THICKNESS
, WALL_THICKNESS
, newWorldEndY
- newWorldStartY
+ WALL_THICKNESS
* 2, 'wall'));
186 walls
.add(game
.add
.tileSprite(newWorldEndX
, newWorldStartY
- WALL_THICKNESS
, WALL_THICKNESS
, newWorldEndY
- newWorldStartY
+ WALL_THICKNESS
* 2, 'wall'));
187 walls
.children
.forEach(function(wall
) { wall
.body
.immovable
= true; });
189 function relocateGem(gem
) {
190 if (gem
.x
< newWorldStartX
) gem
.x
= newWorldStartX
+ (newWorldStartX
- gem
.x
);
191 else if (gem
.x
> newWorldEndX
) gem
.x
= newWorldEndX
+ (newWorldEndX
- gem
.x
); // Adding negative value is like substraction.
192 if (gem
.y
< newWorldStartY
) gem
.y
= newWorldStartY
+ (newWorldStartY
- gem
.y
);
193 else if (gem
.y
> newWorldEndY
) gem
.y
= newWorldEndY
+ (newWorldEndY
- gem
.y
); // Same.
196 gems
.children
.forEach(relocateGem
);