Skip to content

Instantly share code, notes, and snippets.

@thinkong
Created February 4, 2014 12:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save thinkong/8802753 to your computer and use it in GitHub Desktop.
Save thinkong/8802753 to your computer and use it in GitHub Desktop.
///<reference path="phaser.d.ts" />
class RPGTownGame {
// variables
game: Phaser.Game;
//spritemap: { [chartype: string]: Phaser.Sprite } = {};
spritegroup: Phaser.Group;
bulletgroup: Phaser.Group;
PlayerSprite: Phaser.Sprite;
// the constructor
iNextLaunch: number;
constructor() {
this.game = new Phaser.Game(800, 450, Phaser.AUTO, 'content', { preload: this.preload, create: this.create, update: this.update, render:this.render });
//console.log('constructor : ' + this.spritemap);
}
preload() {
// load all the needed images here..
this.game.stage.backgroundColor = '#FF00FF';
this.game.load.spritesheet('char1', './assets/vx_chara01_a.png', 32, 48, 96);
this.game.load.image('bullet', './assets/bullet.png');
this.game.stage.scaleMode = Phaser.StageScaleMode.SHOW_ALL;
//this.game.stage.backgroundColor = 'rgba(0,0,0,0.3)';
}
create() {
this.bulletgroup = this.game.add.group();
this.bulletgroup.createMultiple(100, 'bullet');
this.bulletgroup.setAll('anchor.x', 0.5);
this.bulletgroup.setAll('anchor.y', 0.5);
this.bulletgroup.setAll('outOfBoundsKill', true);
this.PlayerSprite = this.game.add.sprite(this.game.world.centerX, this.game.world.centerY, 'char1');
this.PlayerSprite.anchor.setTo(0.5, 0.5);
var iModifier = 0;
this.PlayerSprite.animations.add('down', [0 + iModifier, 1 + iModifier, 2 + iModifier, 1 + iModifier], 4);
this.PlayerSprite.animations.add('left', [12 + iModifier, 13 + iModifier, 14 + iModifier, 13 + iModifier], 4);
this.PlayerSprite.animations.add('right', [24 + iModifier, 25 + iModifier, 26 + iModifier, 25 + iModifier], 4);
this.PlayerSprite.animations.add('up', [36 + iModifier, 37 + iModifier, 38 + iModifier, 37 + iModifier], 4);
//this.PlayerSprite.animations.play('down', 4, true);
this.iNextLaunch = 0;
}
//changeChar() {
// this.iSelected = (this.iSelected + 1) % 8;
//}
collisionHandler(player, bullets:Phaser.Sprite) {
if (!bullets.alive) return;
bullets.kill();
player.reset(this.game.world.centerX, this.game.world.centerY);
}
fireBullet(game:Phaser.Game, bul: Phaser.Sprite, target:Phaser.Sprite) {
//bul.reset(0, game.rnd.integerInRange(0, game.world.width));
var iRnd = game.rnd.integer() % 4;
var xPos = 0;
var yPos = 0
switch (iRnd) {
case 0:// from top
xPos = game.rnd.integerInRange(0, game.world.width);
break;
case 1:// from bot
xPos = game.rnd.integerInRange(0, game.world.width);
yPos = game.world.height;
break;
case 2: // from left
yPos = game.rnd.integerInRange(0, game.world.height);
break;
case 3:
xPos = game.world.width;
yPos = game.rnd.integerInRange(0, game.world.height);
break;
}
bul.reset(xPos, yPos);
game.physics.moveToObject(bul, target, game.rnd.integerInRange(200,500));
}
render() {
//this.game.debug.renderText('active bullets: ' + this.bulletgroup.countLiving() + '/' + this.bulletgroup.countDead(), 32, 32);
//this.game.debug.renderSpriteInfo(this.PlayerSprite, 32, 450);
}
update() {
if (this.game.input.mousePointer.isDown || this.game.input.pointer1.isDown) {
this.game.physics.moveToPointer(this.PlayerSprite, 400);
if (Math.abs(this.PlayerSprite.body.velocity.x) > Math.abs(this.PlayerSprite.body.velocity.y)) {
if (this.PlayerSprite.body.velocity.x > 0) {
this.PlayerSprite.animations.play('right', 4, true);
}
else if (this.PlayerSprite.body.velocity.x < 0) {
this.PlayerSprite.animations.play('left', 4, true);
}
}
else {
if (this.PlayerSprite.body.velocity.y > 0) {
this.PlayerSprite.animations.play('down', 4, true);
}
else if (this.PlayerSprite.body.velocity.y < 0) {
this.PlayerSprite.animations.play('up', 4, true);
}
}
if (Phaser.Rectangle.contains(this.PlayerSprite.bounds, this.game.input.x, this.game.input.y)) {
this.PlayerSprite.body.velocity.setTo(0, 0);
}
}
else {
this.PlayerSprite.body.velocity.setTo(0, 0);
}
if (this.game.time.now > this.iNextLaunch) {
var deadbullet = this.bulletgroup.getFirstExists(false);
if (deadbullet) {
RPGTownGame.prototype.fireBullet(this.game, deadbullet, this.PlayerSprite);
this.iNextLaunch = this.game.time.now + this.game.rnd.integerInRange(100, 500);
}
else {
console.log('no dead bullets');
}
}
this.game.physics.collide(this.bulletgroup, this.PlayerSprite, RPGTownGame.prototype.collisionHandler, null, this);
}
}
window.onload = () => {
var game = new RPGTownGame();
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment