Skip to content

Commit

Permalink
Bounce
Browse files Browse the repository at this point in the history
  • Loading branch information
ArcticFqx committed Nov 18, 2023
1 parent 477c0d7 commit fd2a96b
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 22 deletions.
7 changes: 5 additions & 2 deletions src/components/Trampoline.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,22 @@
import { GameScene } from "@/scenes/GameScene";
import { Button } from "./Button";
import constants from '@/constants.json';

export class Trampoline extends Button {
public scene: GameScene;

// Sprites
private sprite: Phaser.Types.Physics.Arcade.SpriteWithStaticBody;
public sprite: Phaser.Types.Physics.Arcade.SpriteWithDynamicBody;

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

/* Sprite */
this.sprite = this.scene.physics.add.staticSprite(0, 0, "trampoline");
this.sprite = this.scene.physics.add.sprite(0, 0, "trampoline");
this.sprite.setImmovable(true);

//this.sprite.static;
this.sprite.setOrigin(0.5, 1.0);
this.add(this.sprite);
Expand Down
28 changes: 8 additions & 20 deletions src/components/Turtle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ export class Turtle extends Button {

// Sprites
private spriteSize: number;
private sprite: Phaser.Types.Physics.Arcade.SpriteWithDynamicBody;
public sprite: Phaser.Types.Physics.Arcade.SpriteWithDynamicBody;
private tween: Phaser.Tweens.Tween;

// Controls
Expand All @@ -35,6 +35,7 @@ export class Turtle extends Button {
this.sprite = this.scene.physics.add.sprite(0, 0, "turtle_waiting");
this.sprite.setGravityY(constants.physics.gravity);
this.sprite.setCollideWorldBounds(true)
this.sprite.setBounceY(0.4);
this.sprite.setOrigin(0.5, 1.0);
this.sprite.y += this.spriteSize / 2;
this.sprite.setScale(this.spriteSize / this.sprite.width);
Expand All @@ -53,35 +54,22 @@ export class Turtle extends Button {
this.isDragged = false;
this.dragOffset = new Phaser.Math.Vector2();
this.bindInteractive(this.sprite, true);

this.sprite.on('collide', () => {
console.log("Collide")
});
}

update(time: number, delta: number) {
// this.x += (this.velocity.x * delta) / 1000;
// this.y += (this.velocity.y * delta) / 1000;

// Border collision
// if (this.x < this.border.left) {
// this.x = this.border.left;
// }
// if (this.x > this.border.right) {
// this.x = this.border.right;
// }
// if (this.y < this.border.top) {
// this.y = this.border.top;
// }
// if (this.y > this.border.bottom) {
// this.y = this.border.bottom;
// }

// Animation (Change to this.sprite.setScale if needed)
const squish = 0.02 * Math.sin((6 * time) / 1000);
this.setScale(1.0 + squish, 1.0 - squish);
}

onDown(pointer: Phaser.Input.Pointer, localX: number, localY: number, event: Phaser.Types.Input.EventData): void {
console.log("ondown")

this.sprite.setGravityY(0);
this.sprite.setVelocity(0,0);
this.sprite.setVelocity(0, 0);
}

onDragStart(pointer: Phaser.Input.Pointer, dragX: number, dragY: number) {
Expand Down
4 changes: 4 additions & 0 deletions src/scenes/OverworldState.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,10 @@ export class OverworldState extends Phaser.GameObjects.Container {
this.add(turtle);
this.turtles.push(turtle);

this.scene.physics.add.collider(turtle.sprite, this.trampoline.sprite, () => {
turtle.sprite.setVelocityY(-800 + (Math.random()*100-50));
})

turtle.on("action", () => {
turtle.doABarrelRoll();
});
Expand Down

0 comments on commit fd2a96b

Please sign in to comment.