Skip to content

Commit

Permalink
Merge branch 'main' of github.com:Chocobois/TurtleTrampoline
Browse files Browse the repository at this point in the history
  • Loading branch information
Golen87 committed Nov 19, 2023
2 parents 6bfb3a5 + cec664d commit b998f0c
Show file tree
Hide file tree
Showing 7 changed files with 144 additions and 1 deletion.
2 changes: 2 additions & 0 deletions src/assets/assets.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ const images: Image[] = [
image('characters/turtle_waiting', 'turtle_walking_angry'),
image('characters/turtle_walking1', 'turtle_walking_angry1'),
image('characters/turtle_walking2', 'turtle_walking_angry2'), // For walking animation, do this: turtle_walking - turtle_walking1 - turtle_walking - turtle_walking2 -loop
image('characters/shopper', 'shopper'),
image('characters/raccoon', 'raccoon'),

// Items
image('items/coin', 'coin'),
Expand Down
Binary file added src/assets/images/characters/raccoon.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added src/assets/images/characters/shopper.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
45 changes: 45 additions & 0 deletions src/components/ShopOwner.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import { GameScene } from "@/scenes/GameScene";
import { Button } from "./Button";
import { Trampoline } from "./Trampoline";

export class ShopOwner extends Button {
public scene: GameScene;

// Sprites
private spriteSize: number;
public sprite: Phaser.GameObjects.Sprite;
private tween: Phaser.Tweens.Tween;

constructor(scene: GameScene, x: number, y: number) {
super(scene, x, y);
scene.add.existing(this);
this.scene = scene;

/* Sprite */
this.spriteSize = 150;
this.sprite = this.scene.add.sprite(this.scene.W*0.85, this.scene.H*0.95, "raccoon");
this.sprite.setOrigin(0.5, 1);

this.sprite.setScale(this.spriteSize / this.sprite.width);
this.add(this.sprite);

/* Input */
this.bindInteractive(this.sprite, true);
}

update(time: number, delta: number) {
const squish = 0.02 * Math.sin((6 * time) / 1000);
this.sprite.setScale(
(1.0 + squish) + this.spriteSize / this.sprite.width,
(1.0 - squish) + this.spriteSize / this.sprite.height
);
}


setSpriteOrigin(ox: number, oy: number) {
this.sprite.x += (ox - this.sprite.originX) * this.sprite.displayWidth;
this.sprite.y += (oy - this.sprite.originY) * this.sprite.displayHeight;
this.sprite.setOrigin(ox, oy);
}

}
44 changes: 44 additions & 0 deletions src/components/Shopper.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import { GameScene } from "@/scenes/GameScene";
import { Button } from "./Button";
import { Trampoline } from "./Trampoline";

export class Shopper extends Button {
public scene: GameScene;

// Sprites
private spriteSize: number;
public sprite: Phaser.GameObjects.Sprite;
private tween: Phaser.Tweens.Tween;

constructor(scene: GameScene, x: number, y: number) {
super(scene, x, y);
scene.add.existing(this);
this.scene = scene;

/* Sprite */
this.spriteSize = 1;
this.sprite = this.scene.add.sprite(-60, this.scene.H, "shopper");
this.sprite.setOrigin(0, 1);

this.sprite.setScale(this.spriteSize / this.sprite.width);
this.add(this.sprite);

this.bindInteractive(this.sprite, true);
}

update(time: number, delta: number) {
const squish = Math.sin((2.5 * (time+0.5)) / 1000);
this.sprite.setScale(
(1.0 + 0.005 * squish) + this.spriteSize / this.sprite.width,
(1.0 - 0.01 * squish) + this.spriteSize / this.sprite.height
);
}


setSpriteOrigin(ox: number, oy: number) {
this.sprite.x += (ox - this.sprite.originX) * this.sprite.displayWidth;
this.sprite.y += (oy - this.sprite.originY) * this.sprite.displayHeight;
this.sprite.setOrigin(ox, oy);
}

}
3 changes: 2 additions & 1 deletion src/scenes/GameScene.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ export class GameScene extends BaseScene {
this.shop = new ShopState(this);
this.ui = new UI(this);

this.setState(State.Overworld);
this.setState(this.state);
}

update(time: number, delta: number) {
Expand All @@ -41,6 +41,7 @@ export class GameScene extends BaseScene {

setState(state: State) {
this.state = state;
this.events.emit(`state_${state}`);
this.overworld.setVisible(state == State.Overworld);
this.shop.setVisible(state == State.Shop);
}
Expand Down
51 changes: 51 additions & 0 deletions src/scenes/ShopState.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,45 @@
import { GameScene, State } from "@/scenes/GameScene";
import { Button } from "@/components/Button";
import { Shopper } from "@/components/Shopper";
import { ShopOwner } from "@/components/ShopOwner";

type Item = {
title: string[];
description: string[];
image: string[];
prices: number[],
onBuy: (() => null | null)[]
}

const shopItems: Item[] = [
/*
{
name: 'Duct Tape',
description: 'Will fix your trampoline 100% of the time!',
image: 'tape',
prices: [500],
onBuy: []
},
*/
{
title: ['Spring'],
description: ['Enhances the bounce'],
image: ['spring'],
prices: [100],
onBuy: []
}
];

export class ShopState extends Phaser.GameObjects.Container {
public scene: GameScene;

private background: Phaser.GameObjects.Image;
private someButton: Button;

private itemsForSale: Item[];
private shopper: Shopper;
private owner: ShopOwner;

constructor(scene: GameScene) {
super(scene, 0, 0);
this.scene = scene;
Expand All @@ -31,9 +64,27 @@ export class ShopState extends Phaser.GameObjects.Container {
this.someButton.on("click", () => {
this.scene.setState(State.Overworld);
});

this.itemsForSale = [];
this.scene.events.addListener('state_Shop', () => {
this.populateShop();
});

this.shopper = new Shopper(scene, 0.5, 0.5);
this.add(this.shopper);

this.owner = new ShopOwner(scene, 0.5, 0.5);
this.add(this.owner);
}

populateShop() {
this.itemsForSale = shopItems;

}

update(time: number, delta: number) {
if (!this.visible) return;
this.shopper.update(time, delta);
this.owner.update(time, delta);
}
}

0 comments on commit b998f0c

Please sign in to comment.