Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
246 changes: 149 additions & 97 deletions Killian.js
Original file line number Diff line number Diff line change
@@ -1,98 +1,150 @@
//
// Killian
//

Killian = function( game, x, y ){
Phaser.Sprite.call(this, game, x, y, 'killian');

//
// Animations
//

this.animations.add('walking', [1,2,3,4,5,6,7,8,9,10,11,12], 30, true);
this.animations.add('walking_to_sitting', [13,14,15,16,17,18,19], 40, false);

this.animations.add('sitting', [21,22,23,24,25,26], 35, true);
this.animations.add('sitting_to_walking', [19,18,17,16,15,14,13], 40, false);
this.animations.add('sitting_to_laying', [88,89,90,91,92,93,94,95,97,97,97,97], 40, false);

this.animations.add('laying', [97,98,99,98], 10, true);
this.animations.add('laying_to_sitting', [95,94,93,92,91,90,89,88,87,87,87,87,87,87,87], 40, false);
this.animations.add('laying_to_sleeping', [102,103,104,105,106,107,108,109,110,110,110,110,110,110,110,110,110,110,110,110,110,110,110,110,110,110,110,110,110,110,110,110,110,110,110,110,110,110,110,110,110,110,110,110,110,110,110,110], 40, false);

this.animations.add('sleeping', [113,114,115,116,117,118,119,120,121,121,121,121,121,121,121,121,121,121,121,120,119,118,117,116,115,114,113,113,113,113,113,113,113,113,113,113,113,113,113,113,113,113,113,113], 10, true );
this.animations.add('sleeping_to_laying', [113,110,109,108,107,106,105,104,103,102,100,99,98,97,97,97,97,97,97,97], 40, false);

//
// State Machine
//

this.sm = new StateMachine( this, { debug: false } );
var self = this;

this.sm.state('sitting', {
enter: function(){ },
update: function(){ },
exit: function(){ }
});

this.sm.state('walking', {
enter: function(){ },
update: function(){ },
exit: function(){ }
});

this.sm.state('laying', {
enter: function(){ },
update: function(){ },
exit: function(){ }
});

this.sm.state('sleeping', {
enter: function(){ },
update: function(){ },
exit: function(){ }
});

//
// state machine transitions
//

// walking
this.sm.transition('walking_to_sitting', 'walking', 'sitting', function(){
return ( !game.input.keyboard.isDown(Phaser.Keyboard.RIGHT) );
});

this.sm.transition('sitting_to_walking', 'sitting', 'walking', function(){
return ( game.input.keyboard.isDown(Phaser.Keyboard.RIGHT) );
});

// sitting
this.sm.transition('sitting_to_laying', 'sitting', 'laying', function(){
return ( new Date() - self.sm.timer > 1000 );
});

// laying
this.sm.transition('laying_to_sitting', 'laying', 'sitting', function(){
return ( game.input.keyboard.isDown(Phaser.Keyboard.RIGHT) );
});

this.sm.transition('laying_to_sleeping', 'laying', 'sleeping', function(){
return ( new Date() - self.sm.timer > 1000 );
});

// sleeping
this.sm.transition('sleeping_to_laying', 'sleeping', 'laying', function(){
return ( game.input.keyboard.isDown(Phaser.Keyboard.RIGHT) );
});

this.animations.play( this.sm.initialState );

game.add.existing(this);
}
class Killian extends Phaser.GameObjects.Sprite {
constructor(scene, x, y) {
super(scene, x, y, 'killian');

//
// Animations
//

this.anims.create({
key: 'walking',
frames: this.anims.generateFrameNumbers('killian', { start: 1, end: 12 }),
frameRate: 30,
repeat: -1
});

this.anims.create({
key: 'walking_to_sitting',
frames: this.anims.generateFrameNumbers('killian', { start: 13, end: 19 }),
frameRate: 40,
repeat: 0
});

this.anims.create({
key: 'sitting',
frames: this.anims.generateFrameNumbers('killian', { start: 21, end: 26 }),
frameRate: 35,
repeat: -1
});

this.anims.create({
key: 'sitting_to_walking',
frames: this.anims.generateFrameNumbers('killian', { start: 19, end: 13 }),
frameRate: 40,
repeat: 0
});

this.anims.create({
key: 'sitting_to_laying',
frames: this.anims.generateFrameNumbers('killian', { start: 88, end: 97 }),
frameRate: 40,
repeat: 0
});

this.anims.create({
key: 'laying',
frames: this.anims.generateFrameNumbers('killian', { start: 97, end: 98 }),
frameRate: 10,
repeat: -1
});

this.anims.create({
key: 'laying_to_sitting',
frames: this.anims.generateFrameNumbers('killian', { start: 95, end: 87 }),
frameRate: 40,
repeat: 0
});

this.anims.create({
key: 'laying_to_sleeping',
frames: this.anims.generateFrameNumbers('killian', { start: 102, end: 110 }),
frameRate: 40,
repeat: 0
});

this.anims.create({
key: 'sleeping',
frames: this.anims.generateFrameNumbers('killian', { start: 113, end: 121 }),
frameRate: 10,
repeat: -1
});

this.anims.create({
key: 'sleeping_to_laying',
frames: this.anims.generateFrameNumbers('killian', { start: 113, end: 97 }),
frameRate: 40,
repeat: 0
});

//
// State Machine
//

this.sm = new StateMachine(this, { debug: false });
var self = this;

Killian.prototype = Object.create(Phaser.Sprite.prototype);
Killian.prototype.constructor = Killian;
Killian.prototype.update = function(){
this.sm.update();
}
this.sm.state('sitting', {
enter: function () { },
update: function () { },
exit: function () { }
});

this.sm.state('walking', {
enter: function () { },
update: function () { },
exit: function () { }
});

this.sm.state('laying', {
enter: function () { },
update: function () { },
exit: function () { }
});

this.sm.state('sleeping', {
enter: function () { },
update: function () { },
exit: function () { }
});

//
// state machine transitions
//

// walking
this.sm.transition('walking_to_sitting', 'walking', 'sitting', function () {
return (!self.scene.input.keyboard.isDown(Phaser.Input.Keyboard.KeyCodes.RIGHT));
});

this.sm.transition('sitting_to_walking', 'sitting', 'walking', function () {
return (self.scene.input.keyboard.isDown(Phaser.Input.Keyboard.KeyCodes.RIGHT));
});

// sitting
this.sm.transition('sitting_to_laying', 'sitting', 'laying', function () {
return (new Date() - self.sm.timer > 1000);
});

// laying
this.sm.transition('laying_to_sitting', 'laying', 'sitting', function () {
return (self.scene.input.keyboard.isDown(Phaser.Input.Keyboard.KeyCodes.RIGHT));
});

this.sm.transition('laying_to_sleeping', 'laying', 'sleeping', function () {
return (new Date() - self.sm.timer > 1000);
});

// sleeping
this.sm.transition('sleeping_to_laying', 'sleeping', 'laying', function () {
return (self.scene.input.keyboard.isDown(Phaser.Input.Keyboard.KeyCodes.RIGHT));
});

this.anims.play(this.sm.initialState);

this.scene.add.existing(this);
}

update() {
this.sm.update();
}
}
Loading