Skip to content

Commit

Permalink
Add item Light
Browse files Browse the repository at this point in the history
  • Loading branch information
diguifi committed Feb 21, 2019
1 parent 5df8cdd commit c406296
Show file tree
Hide file tree
Showing 8 changed files with 181 additions and 23 deletions.
129 changes: 113 additions & 16 deletions assets/levels/jungle3.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@
"name":"items",
"objects":[
{
"gid":762,
"gid":768,
"height":17,
"id":57,
"name":"shield",
Expand All @@ -52,6 +52,90 @@
"width":16,
"x":1641,
"y":108
},
{
"gid":774,
"height":16,
"id":59,
"name":"light",
"rotation":0,
"type":"",
"visible":true,
"width":16,
"x":775,
"y":126
},
{
"gid":774,
"height":16,
"id":61,
"name":"light",
"rotation":0,
"type":"",
"visible":true,
"width":16,
"x":1413,
"y":176
},
{
"gid":774,
"height":16,
"id":62,
"name":"light",
"rotation":0,
"type":"",
"visible":true,
"width":16,
"x":1937,
"y":175
},
{
"gid":774,
"height":16,
"id":63,
"name":"light",
"rotation":0,
"type":"",
"visible":true,
"width":16,
"x":2580,
"y":173
},
{
"gid":774,
"height":16,
"id":64,
"name":"light",
"rotation":0,
"type":"",
"visible":true,
"width":16,
"x":3263,
"y":93
},
{
"gid":774,
"height":16,
"id":65,
"name":"light",
"rotation":0,
"type":"",
"visible":true,
"width":16,
"x":1048,
"y":122
},
{
"gid":774,
"height":16,
"id":66,
"name":"light",
"rotation":0,
"type":"",
"visible":true,
"width":16,
"x":2578,
"y":70
}],
"opacity":1,
"type":"objectgroup",
Expand Down Expand Up @@ -256,7 +340,7 @@
"name":"enemies",
"objects":[
{
"gid":751,
"gid":754,
"height":22,
"id":6,
"name":"inimigo1",
Expand All @@ -268,7 +352,7 @@
"y":174.333333333333
},
{
"gid":751,
"gid":754,
"height":22,
"id":7,
"name":"inimigo1",
Expand Down Expand Up @@ -611,8 +695,8 @@
"type":"",
"visible":true,
"width":16,
"x":3944,
"y":171.333333333333
"x":2678,
"y":169.333333333333
}],
"opacity":1,
"type":"objectgroup",
Expand Down Expand Up @@ -720,10 +804,10 @@
"y":0
}],
"nextlayerid":9,
"nextobjectid":58,
"nextobjectid":67,
"orientation":"orthogonal",
"renderorder":"right-down",
"tiledversion":"1.2.1",
"tiledversion":"1.2.2",
"tileheight":16,
"tilesets":[
{
Expand Down Expand Up @@ -766,34 +850,34 @@
"tilewidth":16
},
{
"columns":1,
"columns":4,
"firstgid":750,
"image":"..\/sprites\/enemy.png",
"imageheight":22,
"imagewidth":23,
"imagewidth":105,
"margin":0,
"name":"enemies",
"spacing":0,
"tilecount":1,
"tilecount":4,
"tileheight":22,
"tilewidth":23
},
{
"columns":1,
"firstgid":751,
"columns":4,
"firstgid":754,
"image":"..\/sprites\/enemy.png",
"imageheight":22,
"imagewidth":23,
"imagewidth":105,
"margin":0,
"name":"enemies",
"spacing":0,
"tilecount":1,
"tilecount":4,
"tileheight":22,
"tilewidth":23
},
{
"columns":5,
"firstgid":752,
"firstgid":758,
"image":"..\/sprites\/bat_spritesheet.png",
"imageheight":32,
"imagewidth":80,
Expand All @@ -806,7 +890,7 @@
},
{
"columns":6,
"firstgid":762,
"firstgid":768,
"image":"..\/sprites\/shield.png",
"imageheight":17,
"imagewidth":96,
Expand All @@ -816,6 +900,19 @@
"tilecount":6,
"tileheight":17,
"tilewidth":16
},
{
"columns":4,
"firstgid":774,
"image":"..\/sprites\/light.png",
"imageheight":16,
"imagewidth":64,
"margin":0,
"name":"light",
"spacing":0,
"tilecount":4,
"tileheight":16,
"tilewidth":16
}],
"tilewidth":16,
"type":"map",
Expand Down
Binary file added assets/sprites/light.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion dist/game.js

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions scripts/Preloader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ export class Preloader extends Phaser.State {
this.game.load.spritesheet('redgem', 'assets/sprites/itens/spr_coin_ver.png?v=1', 16, 16, 4);
this.game.load.spritesheet('torch', 'assets/sprites/animated_torch.png?v=1', 8, 26, 9);
this.game.load.spritesheet('shield', 'assets/sprites/shield.png?v=1', 16, 17, 6);
this.game.load.spritesheet('light', 'assets/sprites/light.png?v=1', 16, 16, 4);
this.game.load.spritesheet('platform', 'assets/sprites/platform.png?v=1', 32, 8, 2);
this.game.load.spritesheet('lever', 'assets/sprites/lever.png', 16, 16, 2);
this.game.load.image('heart', 'assets/sprites/itens/heart.png');
Expand Down
34 changes: 34 additions & 0 deletions scripts/elements/items/Light.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
export class Light extends Phaser.Sprite {
game: Phaser.Game;
size: number;
name: string = 'light';

constructor(game: Phaser.Game, x: number, y: number, gravity: number) {
super(game, x, y, 'light', 0);
this.game = game;

// sprite size
this.size = 2;
this.scale.setTo(this.size, this.size);

// animation
this.frame = 0;
this.animations.add('shine', [0, 1, 2, 3, 2, 1], 8, true);
this.animations.play('shine');

// sprite anchor
this.anchor.setTo(0.5, 0);

// physics
this.game.physics.arcade.enableBody(this);
this.body.setSize(12, 9, 0, 0);
this.body.collideWorldBounds = true;
this.body.gravity.y = gravity;

game.add.existing(this);
}

update() {

}
}
10 changes: 10 additions & 0 deletions scripts/elements/player/Player.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ export class Player extends Phaser.Sprite {
this.pressingUp = false;
this.dead = false;
this.fadeComplete = false;
this.defaultLightRadius = 250;
this.lightRadius = this.defaultLightRadius;

// shield attributes
this.shieldMaxTime = 10;
Expand Down Expand Up @@ -69,6 +71,8 @@ export class Player extends Phaser.Sprite {
shieldSeconds: number;
lastShieldSeconds: number;
shieldSprite: Phaser.Sprite;
lightRadius: number;
defaultLightRadius: number;
spawnX: number;
spawnY: number;
animSpeeds;
Expand Down Expand Up @@ -125,6 +129,11 @@ export class Player extends Phaser.Sprite {
}
}

public updateLightRadius() {
if (this.lightRadius > 60)
this.lightRadius -= 0.2;
}

public removeShield() {
if (this.shieldSeconds === 0) {
this.hasShield = false;
Expand Down Expand Up @@ -176,6 +185,7 @@ export class Player extends Phaser.Sprite {
this.position.x = this.spawnX;
this.position.y = this.spawnY;
this.dead = false;
this.lightRadius = this.defaultLightRadius;

if (this.lives < 0){
this.soundManager.music.stop();
Expand Down
16 changes: 10 additions & 6 deletions scripts/levels/Level3.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,10 @@ export class Level3 extends Phaser.State {

this.lightSprite.blendMode = Phaser.blendModes.MULTIPLY;

// ---- hud and game
// light item (after shadow to make it bright)
this.levelManager.createLights();

// ---- hud and game
this.hud = new Hud(this.game, this.player);
this.game.world.bringToTop(this.hud);
}
Expand All @@ -79,21 +81,23 @@ export class Level3 extends Phaser.State {
this.levelManager.updateEnemiesInteraction(this.player);
this.levelManager.updateItemsInteraction(this.player);
this.levelManager.updateBatsInteraction(this.player);

this.player.updateLightRadius();
}

updateShadowTexture() {
this.lightSprite.reset(this.game.camera.x, this.game.camera.y);

this.shadowTexture.clear();
this.shadowTexture.context.fillStyle = 'rgb(10, 10, 10, 0.75)';
this.shadowTexture.context.fillStyle = 'rgb(10, 10, 10, 0.98)';
this.shadowTexture.context.fillRect(-25, -25, this.game.width + 100, this.game.height + 100);

var radius = 150 + this.game.rnd.integerInRange(1, 20),
let radius = this.player.lightRadius + this.game.rnd.integerInRange(1, (this.player.lightRadius * 0.13)),
torchX = this.player.position.x - this.game.camera.x,
torchY = this.player.position.y - this.game.camera.y;
torchY = this.player.position.y + 15 - this.game.camera.y;

var gradient = this.shadowTexture.context.createRadialGradient(
torchX, torchY, 100 * 0.75,
let gradient = this.shadowTexture.context.createRadialGradient(
torchX, torchY, (this.player.lightRadius * 0.66) * 0.75,
torchX, torchY, radius);
gradient.addColorStop(0, 'rgba(255, 255, 255, 1.0)');
gradient.addColorStop(1, 'rgba(255, 255, 255, 0.0)');
Expand Down
12 changes: 12 additions & 0 deletions scripts/levels/LevelManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { Shield } from "../elements/items/Shield";
import { Platform } from "../elements/objects/Platform";
import { Lever } from "../elements/objects/Lever";
import { Gate } from "../elements/objects/Gate";
import { Light } from "../elements/items/Light";

export class LevelManager {
public level: LevelBase;
Expand Down Expand Up @@ -108,6 +109,14 @@ export class LevelManager {
}.bind(this));
}

public createLights() {
this.level.map.objects.items.forEach(function (data) {
if(data.name == 'light') {
this.level.items.push(new Light(this.game, data.x * 2, data.y * 1.7, this.game.physics.arcade.gravity.y));
}
}.bind(this));
}

public createBats(player: Player) {
this.playerRef = player;
this.level.map.objects.bats.forEach(function (data) {
Expand Down Expand Up @@ -314,6 +323,9 @@ export class LevelManager {
if(item.name == 'shield') {
player.hasShield = true;
}
if(item.name == 'light') {
player.lightRadius = player.defaultLightRadius;
}
item.destroy();
}

Expand Down

0 comments on commit c406296

Please sign in to comment.