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
ArcticFqx committed Nov 19, 2023
2 parents ff9283a + cd2967c commit 0ea864a
Show file tree
Hide file tree
Showing 3 changed files with 107 additions and 46 deletions.
75 changes: 36 additions & 39 deletions src/assets/assets.ts
Original file line number Diff line number Diff line change
@@ -1,61 +1,58 @@
import { Image, SpriteSheet, Audio } from './util';
import { image, sound, music, loadFont, spritesheet } from './util';
import { Image, SpriteSheet, Audio } from "./util";
import { image, sound, music, loadFont, spritesheet } from "./util";

/* Images */
const images: Image[] = [
// Backgrounds
image('backgrounds/overworld', 'overworld'),
image('backgrounds/shop', 'shop'),
image('backgrounds/trampoline', 'trampoline'),
image("backgrounds/overworld", "overworld"),
image("backgrounds/shop", "shop"),
image("backgrounds/trampoline", "trampoline"),

// Characters
image('characters/turtle_jumping', 'turtle_jumping'),
image('characters/turtle_scared', 'turtle_scared'),
image('characters/turtle_stuck', 'turtle_stuck'),
image('characters/turtle_waiting', 'turtle_waiting'),
image('characters/turtle_waiting', 'turtle_waiting_angry'),
image('characters/turtle_waiting', 'turtle_walking'),
image('characters/turtle_walking1', 'turtle_walking1'),
image('characters/turtle_walking2', 'turtle_walking2'), // For walking animation, do this: turtle_walking - turtle_walking1 - turtle_walking - turtle_walking2 -loop
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'),
image("characters/turtle_jumping", "turtle_jumping"),
image("characters/turtle_scared", "turtle_scared"),
image("characters/turtle_stuck", "turtle_stuck"),
image("characters/turtle_waiting", "turtle_waiting"),
image("characters/turtle_waiting_angry", "turtle_waiting_angry"),
// For walking animation, do this: turtle_walking - turtle_walking1 - turtle_walking - turtle_walking2 -loop
image("characters/turtle_walking", "turtle_walking"),
image("characters/turtle_walking1", "turtle_walking1"),
image("characters/turtle_walking2", "turtle_walking2"),
// For walking animation, do this: turtle_walking - turtle_walking1 - turtle_walking - turtle_walking2 -loop
image("characters/turtle_walking_angry", "turtle_walking_angry"),
image("characters/turtle_walking_angry1", "turtle_walking_angry1"),
image("characters/turtle_walking_angry2", "turtle_walking_angry2"),

image("characters/shopper", "shopper"),
image("characters/raccoon", "raccoon"),

// Items
image('items/coin', 'coin'),
image('items/spring', 'spring'),
image('items/screw', 'screw'),
image('items/nail', 'nail'),
image("items/coin", "coin"),
image("items/spring", "spring"),
image("items/screw", "screw"),
image("items/nail", "nail"),

// UI
image('ui/hud', 'hud'),
image("ui/hud", "hud"),

// Titlescreen
image('titlescreen/sky', 'title_sky'),
image('titlescreen/background', 'title_background'),
image('titlescreen/foreground', 'title_foreground'),
image('titlescreen/character', 'title_character'),
image("titlescreen/sky", "title_sky"),
image("titlescreen/background", "title_background"),
image("titlescreen/foreground", "title_foreground"),
image("titlescreen/character", "title_character"),
];

/* Spritesheets */
const spritesheets: SpriteSheet[] = [

];
const spritesheets: SpriteSheet[] = [];

/* Audios */
const audios: Audio[] = [
music('title', 'm_main_menu'),
music('first', 'm_first'),
sound('tree/rustle', 't_rustle', 0.5),
music("title", "m_main_menu"),
music("first", "m_first"),
sound("tree/rustle", "t_rustle", 0.5),
];

/* Fonts */
await loadFont('NerkoOne-Regular', 'Game Font');
await loadFont("NerkoOne-Regular", "Game Font");

export {
images,
spritesheets,
audios
};
export { images, spritesheets, audios };
67 changes: 61 additions & 6 deletions src/components/Turtle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,16 +30,28 @@ export class Turtle extends Button {
private border: { [key: string]: number };
private dragOffset: Phaser.Math.Vector2;

// Walking
private ground: Phaser.Geom.Rectangle;
private walkTarget: Phaser.Geom.Point;
private walkTimer: number;

// Jumping
private trampoline: Trampoline;
private feetOffset: number;
private lostBalance: boolean;
private hasCrashed: boolean;
private jumpTarget: Phaser.Geom.Point;
private jumpTargetTween: Phaser.Tweens.Tween;
private maxJumpSpeed: number;
public bounceCount: number;

constructor(scene: GameScene, x: number, y: number, trampoline: Trampoline) {
constructor(
scene: GameScene,
x: number,
y: number,
trampoline: Trampoline,
ground: Phaser.Geom.Rectangle
) {
super(scene, x, y);
scene.add.existing(this);
this.scene = scene;
Expand All @@ -66,13 +78,21 @@ export class Turtle extends Button {
bottom: scene.H - 200,
};

/* Walking */
this.walkTarget = new Phaser.Geom.Point();
this.ground = ground;
this.newWalkTarget();
this.border.bottom = this.walkTarget.y;
this.walkTimer = 0;

/* Trampoline */
this.trampoline = trampoline;
this.feetOffset = 0;
this.lostBalance = false;
this.hasCrashed = false;
this.jumpTarget = new Phaser.Geom.Point();
this.newJumpTarget();
this.maxJumpSpeed = Phaser.Math.RND.between(27, 31);
this.bounceCount = 0;

/* Input */
Expand All @@ -95,7 +115,7 @@ export class Turtle extends Button {
this.physicsPosition.y + this.feetOffset >=
this.trampoline.zone.bottom
) {
let maxSpeed = 30;
let maxSpeed = this.maxJumpSpeed;
let landSpeed = Math.max(this.physicsVelocity.y, 2);
let jumpSpeed =
Phaser.Math.Easing.Sine.Out(landSpeed / maxSpeed) * maxSpeed;
Expand All @@ -108,7 +128,7 @@ export class Turtle extends Button {
}
// In air above trampoline
else {
// Driction
// Friction
this.physicsVelocity.x = 0.97 * this.physicsVelocity.x;

// Drift towards desired location on trampoline
Expand All @@ -132,6 +152,15 @@ export class Turtle extends Button {

// Grounded
else if (this.isGrounded) {
// Walking
if (!this.lostBalance) {
const distance = this.walkTarget.x - this.physicsPosition.x;
if (Math.abs(distance) > 5) {
const walkingSpeed = 1.0;
this.physicsVelocity.x += walkingSpeed * Math.sign(distance);
}
}

// Friction
this.physicsVelocity.x = 0.5 * this.physicsVelocity.x;
// Stop fall
Expand Down Expand Up @@ -164,7 +193,7 @@ export class Turtle extends Button {
this.x += 0.5 * (this.physicsPosition.x - this.x);
this.y += 0.5 * (this.physicsPosition.y - this.y);
this.debug.setPosition(this.x, this.physicsPosition.y + this.feetOffset);
this.debugLand.setPosition(this.jumpTarget.x, this.jumpTarget.y);
this.debugLand.setPosition(this.walkTarget.x, this.walkTarget.y);

if (this.hold) {
this.dragVelocity.set(
Expand All @@ -179,17 +208,34 @@ export class Turtle extends Button {
const squish = 0.02 * Math.sin((6 * time) / 1000);
this.setScale(1.0 + squish, 1.0 - squish);

const scale = this.spriteSize / this.sprite.width;
const facing = -Math.sign(this.physicsVelocity.x) || 1;
this.sprite.setScale(scale * facing, scale);
this.sprite.angle = this.dragVelocity.x;

if (this.isOnTrampoline) {
if (this.hold || this.isOnTrampoline) {
this.sprite.setTexture("turtle_jumping");
} else if (this.isGrounded) {
if (this.lostBalance) {
this.sprite.setTexture("turtle_stuck");
this.setSpriteOrigin(0.5, 0.8);
this.sprite.angle = 10 * Math.sin((7 * time) / 1000);
} else if (Math.abs(this.physicsVelocity.x) > 0.1) {
const walkSprites = [
"turtle_walking",
"turtle_walking1",
"turtle_walking",
"turtle_walking2",
];
this.walkTimer += 1;
this.sprite.setTexture(
walkSprites[Math.floor(this.walkTimer / 20) % walkSprites.length]
);
} else {
this.sprite.setTexture("turtle_waiting");
if (Math.random() < 0.01) {
this.newWalkTarget();
}
}
} else {
if (this.lostBalance) {
Expand All @@ -201,7 +247,7 @@ export class Turtle extends Button {

// Depth sorting
this.sprite.setPosition(this.x, this.y);
let depth = this.jumpTarget.y;
let depth = this.jumpTarget.y + this.walkTarget.y;
if (this.hold) depth += 100;
this.sprite.setDepth(depth);
}
Expand Down Expand Up @@ -247,6 +293,15 @@ export class Turtle extends Button {
this.sprite.setOrigin(ox, oy);
}

newWalkTarget() {
let y = this.walkTarget.y;
Phaser.Geom.Rectangle.Random(this.ground, this.walkTarget);
if (y != 0) {
// Hack because it's hard to make them move up or down
this.walkTarget.y = y;
}
}

newJumpTarget() {
let target = Phaser.Geom.Ellipse.Random(this.trampoline.surface);

Expand Down
11 changes: 10 additions & 1 deletion src/scenes/OverworldState.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ export class OverworldState extends Phaser.GameObjects.Container {
private turtles: Turtle[];
private someButton: Button;

private ground: Phaser.Geom.Rectangle;

constructor(scene: GameScene) {
super(scene, 0, 0);
this.scene = scene;
Expand All @@ -21,6 +23,13 @@ export class OverworldState extends Phaser.GameObjects.Container {
scene.fitToScreen(this.background);
this.add(this.background);

this.ground = new Phaser.Geom.Rectangle(
0.5 * this.scene.W,
0.8 * this.scene.H,
0.45 * this.scene.W,
0.1 * this.scene.H
);

this.trampoline = new Trampoline(scene, 0.25 * scene.W, 0.85 * scene.H);
this.add(this.trampoline);

Expand Down Expand Up @@ -56,7 +65,7 @@ export class OverworldState extends Phaser.GameObjects.Container {
addTurtle() {
let x = this.scene.W * (0.5 + 0.4 * Math.random());
let y = this.scene.H * (0.6 + 0.2 * Math.random());
let turtle = new Turtle(this.scene, x, y, this.trampoline);
let turtle = new Turtle(this.scene, x, y, this.trampoline, this.ground);
this.add(turtle);
this.turtles.push(turtle);

Expand Down

0 comments on commit 0ea864a

Please sign in to comment.