From c77df712f5e058d101b57c09ac67178d9641cefe Mon Sep 17 00:00:00 2001 From: Chris Weed Date: Wed, 10 Jul 2024 14:35:04 -0500 Subject: [PATCH] Animations somewhat there, adding an extension --- images.g.jres | 14 ----- images.g.ts | 157 -------------------------------------------------- island.ts | 6 +- pirate.ts | 154 ++++++++++++++++++++++++++++++++++++++----------- utils.ts | 8 +++ 5 files changed, 131 insertions(+), 208 deletions(-) diff --git a/images.g.jres b/images.g.jres index b704f38..b37ec32 100644 --- a/images.g.jres +++ b/images.g.jres @@ -78,20 +78,6 @@ "data": "YzgwMDEwMDAxMDAwMDQwMDAwMDBlZTAwZWUwMDAwMDAwMGUwY2VlZWVlMDAwMDAwMDAwMGVjZWUwZTAwMDAwMDAwZWVlZWRkZmQwMDAwMDAwMGYwZGZkZmRmMDAwMDAwMDBiZGRkYmRkZDAwMDAwMDAwZGRkYmRkMGQwMDAwMDAwMDAwZDBkZDAwMDAwMDAwMDBlMGVlZWUwZWQwMDAwMDAwZWVlY2NlZWVkZTBkMDBlMGNlZWVlZWVjZGUwZDAwZGQwZGVlZWUwMDAwMDAwMGRkMDBhYWNhMDAwMDAwMDAwMGYwYTBjYTAwMDAwMDAwMDBhZmFhMGMwMDAwMDAwMGYwMDBmMGZmMDAwMDAwMDAwMDAwZWUwMGVlMDAwMDAwMDBlMGNlZWVlZTAwMDAwMDAwMDBlY2VlMGUwMDAwMDAwMGVlZWVkZGZkMDAwMDAwMDBmMGRmZGZkZjAwMDAwMDAwYmRkZGJkZGQwMDAwMDAwMGRkZGJkZDBkMDAwMDAwMDAwMGQwZGQwMDAwMDAwMDAwZTBlZWVlMGMwMDAwMDAwMGVlZWNjZTBjMDAwMDAwMDBjZWVlZWUwMDAwMDAwMDAwZWVlY2VlMGQwMDAwMDAwMGUwZGRhYTBkMDAwMDAwZjBkYzBkYWEwYTBmMDAwMDAwY2YwMGEwZmEwMDAwMDAwMGYwMDAwMDBmMDAwMDAwMDAwMGVlMDBlZTAwMDAwMDAwZTB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QwZDAwYmJiMDAwMGVlZWVlZWUwZWJiYjAwZWUwY2VlZWNlZWViYmIwZTBlZWVjZWVlZWVjYmIwMDBiZWVlMGVlZGViYmJiMDEwZGRkY2NhY2FhZGRiMDFiZDFkZGNjMDBhYTBhMDBiYjFiZmVmZmYwZmYwZjAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDBlZTAwMDAwMDAwMDBlZWVlZWMwZTAwMDAwMDAwZWVlZWNlMDAwMDAwMDAwMGRmZGRlZWVlMDAwMDAwMDBmZGZkZmQwZjAwMDAwMDAwZGRkYmRkZGIwMDAwMDAwMGQwZGRiZGRkMDAwMDAwMDAwMGRkMGQwMDAwMDAwMDAwZWVlZWVlMGUwMDBiMDBlMGNlZWVjZWVlMDAwYjAwZTBlY2VlZWVlYzAwYjAwMGRkZWRlZWRlZGQwMDAwYmJkZWNkYWNhYWRkMDAwMGIwYmJjYzAwYWEwYTAwMDAwMGYwZmZmMGZmMGYwMDAwMDAwMDAwMDBlZTAwMDAwMDAwMDBlZWVlZWMwZTAwMDAwMDAwZWVlZWNlMDAwMDAwMDAwMGRmZGRlZWVlMDAwMDAwMDBmZGZkZmQwZjAwMDAwMDAwZGRkYmRkZGIwMDAwMDAwMGQwZGRiZGRkMDAwMDAwMDAwMGRkMGQwMDAwMDAwMDAwZWVlZWVlMGUwMDAwMDBlMGNlZWVjZWVlMDAwMDAwZTBlY2VlZWVlYzAwMDAwMGRkZWRlZWRlZGQwMDAwMDBkMGNkYWNhYWRkMDAwMDAwMDBjYzAwYWEwYTAwMDAwMDAwY2MwMGFhMGEwMDAwMDBmMGZmZjBmZjBm", - "displayName": "Pirate Swing w Sword0" - }, "anim9": { "namespace": "myAnimations", "id": "anim9", diff --git a/images.g.ts b/images.g.ts index cc38603..4419a6a 100644 --- a/images.g.ts +++ b/images.g.ts @@ -704,163 +704,6 @@ d d . . a a a c . . . . . . . . . f a d d . c c c . f . . . . . . . f a . . . c c f . . . . . . . . . f . . . . f . . . . . . . -`]; - case "Pirate Walk0": - case "anim11":return [img` -. . . . . . e e . . e e . . . . -. . . . . . e e e e c e e . . . -. . . . . . . e e e e c . . . . -. . . . . . f d d d e e e e . . -. . . . . . d f d f d f f . . . -. . . . . . d d b d d d b d . . -. . . . . . . d d d d b d d . . -. . . . . . . . d d d . . . . . -. . . . d . . e e e e e e . . . -. . . d d e e e c e e c e e . . -. . . d d e e c e e e e c e e . -. . . . . . . . e e e e . d d d -. . . . . . . . c a a a . . d d -. . . . . . . . c a a . f . . . -. . . . . . . . . c a a a f . . -. . . . . . . . f f f . . . f . -`, img` -. . . . . . e e . . e e . . . . -. . . . . . e e e e c e e . . . -. . . . . . . e e e e c . . . . -. . . . . . f d d d e e e e . . -. . . . . . d f d f d f f . . . -. . . . . . d d b d d d b d . . -. . . . . . . d d d d b d d . . -. . . . . . . . d d d . . . . . -. . . . . . . c e e e e e . . . -. . . . . . . c c e e c e e . . -. . . . . . . . e e e e c e . . -. . . . . . . d e e e c e e . . -. . . . . . . d a a d d e . . . -. . . . . f . a a a . d d c f . -. . . . . . f a a . . . c f . . -. . . . . . . f . . . . f . . . -`, img` -. . . . . . e e . . e e . . . . -. . . . . . e e e e c e e . . . -. . . . . . . e e e e c . . . . -. . . . . . . d d d e e e e . . -. . . . . . f d f d d f f . . . -. . . . . . d b d d d b d . . . -. . . . . . . d d d b d d . . . -. . . . . . . . d d d . . . . . -. . . . . . . . e e e e . . . . -. . . . . . . d c e e c e . . . -. . . . . . d d c c c e e d . . -. . . . . . d d e e e e e d . . -. . . . . . . . e e e c . . . . -. . . . . . . . a a c . f . . . -. . . . . . . . . a a c c f . . -. . . . . . . . f f f . . . f . -`, img` -. . . . . . e e . . e e . . . . -. . . . . . e e e e c e e . . . -. . . . . . . e e e e c . . . . -. . . . . . . d d d e e e e . . -. . . . . . f d f d d f f . . . -. . . . . . d b d d d b d . . . -. . . . . . . d d d b d d . . . -. . . . . . . . d d d . . . . . -. . . . . . . c e e e e e . . . -. . . . . . . c c e e c e e . . -. . . . . . . . e e e e c e . . -. . . . . . . d e e e c e e . . -. . . . . . . d d c d d e . . . -. . . . . f . c c c . d d a f . -. . . . . . f c c . . . a f . . -. . . . . . . f . . . . f . . . -`]; - case "Pirate Swing w Sword0": - case "anim12":return [img` -. . . . b 1 1 . e e . . e e . . -. . . b b b b 1 e e e e c e e . -. . b b b b b b 1 e e e e c . . -. . b 1 . . . b b d d d e e e e -. . b e . b . . d f d f d f f . -. . b . e d d e d d b d d d b d -. . . . d d e . e d d d d b d d -. . . . . e e e . e d d d . . . -. . . . . . e e e e e e e e e . -. . . . . . . e c c e e e c e e -. . . . . . . . . e e e e e c e -. . . . . . . . . e e e e d d d -. . . . . . . . . c c a a a d d -. . . . . . . . c c . . a a a . -. . . . . . . . c c . . a a a . -. . . . . . . f f f . f f f f . -`, img` -. . . . . . . . e e . . e e . . -. . b 1 1 1 . . e e e e c e e . -. b b b b b 1 . . e e e e c . . -b b 1 . . . . . f d d d e e e e -b b . . b . . . d f d f d f f . -b b . b . . e . d d b d d d b d -b b . . d e . . e d d d d b d d -b b . d . e . e . . d d d . . . -b b e d d e e e e e e e e e e . -. b . . d e e e e c e e e c e e -. . . . . . . . . e e e e e c e -. . . . . . . . . e e e e d d d -. . . . . . . . . c c a a a d d -. . . . . . . . c c . . a a a . -. . . . . . . . c c . . a a a . -. . . . . . . f f f . f f f f . -`, img` -. . . . . . . . . . . . . . . . -. . . b . . . . e e . . e e . . -. . b . . . . . e e e e c e e . -. b . . b . . . . e e e e c . . -. b . b . . . . f d d d e e e e -1 b . b . b . . d f d f d f f . -b b . b . b . . d d b d d d b d -b b . b . b e . . d d d d b d d -b b . b . b . . e . d d d . . . -b b . b . . e . e e e e e e e . -b b . b e . . e e c e e e c e e -b b . b . e e e c e e e e e c e -b b . . b . e e . e e e e d b b -b b 1 . d . d d c c c a a a d d -. b b 1 1 d d d c c . . a a a . -. . b b b 1 e f f f . f f f f . -`, img` -. . . . . . . . . . . . . . . . -. . . . . . . . . . . . e e . . -. . . . . . . . e e e e c e e . -. . . . . . . . e e e e e c . . -. . . . . . . . f d d d e e e e -. . . . . . . . d f d f d f f . -. . . . . . . . d d b d d d b d -. . . . . . . . . d d d d b d d -. . . . . . . . . . d d d . . . -. . . . . . . . e e e e e e e . -. . b . . . . e e c e e e c e e -. . b . . . . e c e e e e e c e -. . . b . . d d d e e e e d d d -. . . . b b e d d c c a a a d d -. . . . . b b b c c . . a a a . -. . . . . . . f f f . f f f f . -`, img` -. . . . . . . . . . . . e e . . -. . . . . . . . e e e e c e e . -. . . . . . . . e e e e e c . . -. . . . . . . . f d d d e e e e -. . . . . . . . d f d f d f f . -. . . . . . . . d d b d d d b d -. . . . . . . . . d d d d b d d -. . . . . . . . . . d d d . . . -. . . . . . . . e e e e e e e . -. . . . . . . e e c e e e c e e -. . . . . . . e c e e e e e c e -. . . . . . d d d e e e e d d d -. . . . . . . d d c c a a a d d -. . . . . . . . c c . . a a a . -. . . . . . . . c c . . a a a . -. . . . . . . f f f . f f f f . `]; case "Pirate Swing w Sword": case "anim9":return [img` diff --git a/island.ts b/island.ts index 45b71f0..02045d6 100644 --- a/island.ts +++ b/island.ts @@ -43,10 +43,8 @@ namespace Island { // } export function init(island: Map.Island) { - player1 = new Pirate({ controller: controller.player1 }) - player2 = new Pirate({ controller: controller.player2 }) - - Pirate.registerEvents(player1) + player1 = new Pirate({ control: controller.player1, playerNumber: 0 }) + player2 = new Pirate({ control: controller.player2, playerNumber: 1 }) player1.place(10, 90) diff --git a/pirate.ts b/pirate.ts index b8a4a7b..32a0adc 100644 --- a/pirate.ts +++ b/pirate.ts @@ -1,37 +1,62 @@ +type ActionObject = { + attack: () => void + parry: () => void + faceLeft: () => void + faceRight: () => void + goIdle: () => void +} + class Pirate { - static idleRightSprite: Image = assets.image`Pirate` - static idleLeftSprite: Image = assets.image`Pirate` - static attackLeftAnimation: Image[] = assets.animation`Pirate Swing w Sword`.map((frame: Image) => { - frame.flipX() - return frame - }) + static idleRightAnimation: Image[] = assets.animation`Pirate Stand` + static idleLeftAnimation: Image[] = Utils.flipAnimation(assets.animation`Pirate Stand`) + static attackLeftAnimation: Image[] = Utils.flipAnimation(assets.animation`Pirate Swing w Sword`) static attackRightAnimation: Image[] = assets.animation`Pirate Swing w Sword` static parryLeftSprite: Image = assets.image`Pirate` static parryRightSprite: Image = assets.image`Pirate` - static walkRightSprite: Image = assets.image`Pirate` - static walkLeftSprite: Image = assets.image`Pirate` - - static registerEvents = function(pirate: Pirate) { - - } + static walkRightAnimation: Image[] = assets.animation`Pirate Walk` + static walkLeftAnimation: Image[] = Utils.flipAnimation(assets.animation`Pirate Walk`) currentSprite: Sprite facing: 'left' | 'right' - _controller: controller.Controller + public controller: controller.Controller + // This action object is for registering event listeners + // It keeps all functions stable per this class so we can removeEventListners + action: ActionObject = { + attack: () => undefined, + parry: () => undefined, + faceLeft: () => undefined, + faceRight: () => undefined, + goIdle: () => undefined + } public health: number - constructor({ controller }: { controller: controller.Controller }) { + constructor({ control, playerNumber }: { control: controller.Controller, playerNumber: 0 | 1 }) { // Setup event listeners this.health = 100 this.facing = 'right' - this._controller = controller - this.currentSprite = sprites.create(Pirate.idleRightSprite) - animation.runImageAnimation(this.currentSprite, assets.animation`Pirate Stand`, 300, true) + this.currentSprite = sprites.create(assets.image`Pirate`) + this.idle() + // Setup multiplayer + mp.setPlayerSprite(mp.getPlayerByNumber(playerNumber), this.currentSprite) // Setup the controller handlers - // this._controller.A.addEventListener(ControllerButtonEvent.Pressed, Pirate.player1Attack) + this.controller = control + // We can't simply do `this.attack` as the event listeners can't handle a "lambda" + // And we need to keep a reference to the callback function so we can removeEventListner + this.action.attack = () => this.attack() + this.action.parry = () => this.parry() + this.action.faceLeft = () => this.face('left') + this.action.faceRight = () => this.face('right') + this.action.goIdle = () => this.idle() + + this.controller.A.addEventListener(ControllerButtonEvent.Pressed, this.action.attack) + // this.controller.B.addEventListener(ControllerButtonEvent.Pressed, this.action.parry) + this.controller.left.addEventListener(ControllerButtonEvent.Pressed, this.action.faceLeft) + this.controller.right.addEventListener(ControllerButtonEvent.Pressed, this.action.faceRight) + this.controller.left.addEventListener(ControllerButtonEvent.Released, this.action.goIdle) + this.controller.right.addEventListener(ControllerButtonEvent.Released, this.action.goIdle) } public place(x: number, y: number) { @@ -41,25 +66,88 @@ class Pirate { } public destroy() { + this.currentSprite.destroy() // Remove all event listeners - // this._controller.A.removeEventListener(ControllerButtonEvent.Pressed, Pirate.punch) + this.controller.A.removeEventListener(ControllerButtonEvent.Pressed, this.action.attack) + this.controller.B.removeEventListener(ControllerButtonEvent.Pressed, this.action.parry) + this.controller.left.removeEventListener(ControllerButtonEvent.Pressed, this.action.faceLeft) + this.controller.right.removeEventListener(ControllerButtonEvent.Pressed, this.action.faceRight) + this.controller.left.removeEventListener(ControllerButtonEvent.Released, this.action.goIdle) + this.controller.right.removeEventListener(ControllerButtonEvent.Released, this.action.goIdle) } - public render() { } + public render() { + this.currentSprite.x += this.controller.dx(50) + this.currentSprite.y += this.controller.dy(50) + this.currentSprite.z = this.currentSprite.y + + if (this.controller.dx(50) > 0) { + animation.runImageAnimation( + this.currentSprite, + Pirate.walkRightAnimation, + 200, + true + ) + } else if (this.controller.dx(50) < 0) { + animation.runImageAnimation( + this.currentSprite, + Pirate.walkLeftAnimation, + 200, + true + ) + } + } - public attack(me: Pirate) { - console.log('Attack!') + parry() { + console.log('parry ' + this.controller.playerIndex) + } + + attack() { const attackAnimationSpeed = 50 - const anim = me.facing === 'right' ? - Pirate.attackRightAnimation : - Pirate.attackLeftAnimation - - animation.runImageAnimation( - me.currentSprite, - anim, - attackAnimationSpeed, - false - ) - pause(anim.length * attackAnimationSpeed) + if (this.facing === 'right') { + animation.runImageAnimation( + this.currentSprite, + Pirate.attackRightAnimation, + attackAnimationSpeed, + false + ) + } else { + animation.runImageAnimation( + this.currentSprite, + Pirate.attackLeftAnimation, + attackAnimationSpeed, + false + ) + } + + pause(Pirate.attackLeftAnimation.length * attackAnimationSpeed) + } + + face(direction: 'left' | 'right') { + console.log('Face ' + direction) + if (direction === 'left' && this.facing === 'right') { + this.facing = 'left' + } else if (direction === 'right' && this.facing === 'left') { + this.facing = 'right' + } + } + + idle() { + console.log('Going idle ' + this.facing) + if (this.facing === 'left') { + animation.runImageAnimation( + this.currentSprite, + Pirate.idleLeftAnimation, + 200, + true + ) + } else { + animation.runImageAnimation( + this.currentSprite, + Pirate.idleRightAnimation, + 200, + true + ) + } } } diff --git a/utils.ts b/utils.ts index 216a3a6..f971f2a 100644 --- a/utils.ts +++ b/utils.ts @@ -6,4 +6,12 @@ namespace Utils { } return result } + + // Used to re-use an animation but flip it horizontally (x) + export function flipAnimation(frames: Image[]) { + return frames.map((frame: Image) => { + frame.flipX() + return frame + }) + } }