Skip to content

Commit

Permalink
feat: sounds, enemies, rotation, better movements and others
Browse files Browse the repository at this point in the history
  • Loading branch information
oulahoop committed Sep 19, 2024
1 parent 5e874b0 commit b5a14c0
Show file tree
Hide file tree
Showing 22 changed files with 667 additions and 156 deletions.
442 changes: 313 additions & 129 deletions package-lock.json

Large diffs are not rendered by default.

8 changes: 4 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@
"typecheck": "tsc --noEmit"
},
"devDependencies": {
"@fibbojs/2d": "^0.0.5",
"@fibbojs/core": "^0.0.5",
"@fibbojs/devtools": "^0.0.5",
"@fibbojs/event": "^0.0.5",
"@fibbojs/2d": "^0.0.8",
"@fibbojs/core": "^0.0.8",
"@fibbojs/devtools": "^0.0.8",
"@fibbojs/event": "^0.0.8",
"typescript": "^5.2.2",
"vite": "^5.2.0",
"vite-plugin-top-level-await": "^1.4.4",
Expand Down
Binary file added public/assets/coin.mp3
Binary file not shown.
Binary file added public/assets/enemy.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 public/assets/goomba.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 public/assets/kill.mp3
Binary file not shown.
Binary file added public/assets/oof.mp3
Binary file not shown.
Binary file added public/assets/sound_effect.mp3
Binary file not shown.
Binary file added public/assets/tube.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
3 changes: 1 addition & 2 deletions src/classes/Character.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { FSprite } from '@fibbojs/2d'
import type { FScene } from '@fibbojs/2d'
import { FScene } from '@fibbojs/2d'
import {CharacterController} from "./controllers/CharacterController.ts";

export default class Character extends FSprite {
Expand All @@ -13,7 +13,6 @@ export default class Character extends FSprite {
})

this.setScaleHeight(0.8)

// Initialize the character controller
this.controller = new CharacterController(scene, {
component: this,
Expand Down
56 changes: 56 additions & 0 deletions src/classes/blocs/Bloc.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import { FScene, FSprite, FComponentEmpty } from '@fibbojs/2d'
import Character from "../Character.ts";

export default class Bloc extends FSprite {
topSensor: FComponentEmpty
audio= new Audio('maiojs/assets/oof.mp3')

public constructor(scene: FScene, position: { x: number, y: number }) {
super(scene, {
texture: 'bloc.png',
position: position
})

this.topSensor = new FComponentEmpty(this.scene, {
position : {
x: this.position.x,
y: this.position.y + 0.2
}
})

this.initSensorAndCollider()
this.initCollision()
this.addToScene()
}

initSensorAndCollider() {
this.topSensor.initSensor()
this.initCollider()
this.initSensor({
'scale': {x: 0.8, y: 0.2},
'position': {x: 0, y: -0.5},
})
}

initCollision() {
this.topSensor.onCollisionWith(Character, ({component}) => {
const character = component as Character
if(!character.controller.jumpAvailable) {
character.controller.jumpAvailable = true
}
})

this.onCollisionWith(Character, ({component}) => {
this.audio.play()
const character = component as Character
character.controller.yVelocity = 0
this.scene.removeComponent(this)
this.scene.removeComponent(this.topSensor)
})
}

addToScene() {
this.scene.addComponent(this)
this.scene.addComponent(this.topSensor)
}
}
10 changes: 4 additions & 6 deletions src/classes/blocs/Generator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,13 @@ export default abstract class Generator {
this.character = character
}

generate(position: { x: number, y: number }) {
generate(position: { x: number, y: number }, scale: { x: number, y: number } = { x: 1, y: 1 }) {
let secretSensor = new FComponentEmpty(this.scene, {
position
position : { x: position.x, y: position.y + 0.2 },
scale: scale
})

secretSensor.initSensor({
'scale': {x: 0.8, y: 0.2},
'position': {x: 0, y: 0.5},
})
secretSensor.initSensor()

this.character.onCollisionWith(secretSensor, () => {
if(!this.character.controller.jumpAvailable) {
Expand Down
3 changes: 3 additions & 0 deletions src/classes/blocs/GroundGenerator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,10 @@ export default class GroundGenerator extends Generator {
}

public generate(position: { x: number; y: number }) {
// Initialise le secret sensor pour recuperer le jump
super.generate(position)

// Cree le sol
let ground = new FSprite(this.scene, {
texture: this.texture,
position,
Expand Down
2 changes: 2 additions & 0 deletions src/classes/blocs/LootboxGenerator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import Character from "../Character.ts";

export default class LootboxGenerator extends Generator {
texture = 'lootbox.png'
audio = new Audio('maiojs/assets/coin.mp3')

public constructor(scene: FScene, character: Character) {
super(scene, character)
Expand All @@ -24,6 +25,7 @@ export default class LootboxGenerator extends Generator {
lootbox.initCollider()

this.character.onCollisionWith(lootbox, () => {
this.audio.play()
this.character.controller.yVelocity = 0
let position = lootbox.position
this.scene.removeComponent(lootbox)
Expand Down
36 changes: 36 additions & 0 deletions src/classes/blocs/TubeGenerator.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import { FScene } from '@fibbojs/2d'
import { FSprite } from '@fibbojs/2d'
import Generator from "./Generator.ts";
import Character from "../Character.ts";

export default class TubeGenerator extends Generator {
texture = 'tube.png'

public constructor(scene: FScene, character: Character) {
super(scene, character)
}

public generate(position: { x: number, y: number }, scale: { x: number, y: number }) {
super.generate(position, scale)
let tube = new FSprite(this.scene, {
texture: this.texture,
position,
scale: {x: 1, y: 1}
})


tube.initCollider()
tube.initSensor({
'scale': {x: 0.8, y: 0.2},
'position': {x: 0, y: -0.5},
})

this.scene.addComponent(tube)

tube.onLoaded(() => {
tube.scaleX = scale.x
tube.scaleY = scale.y

})
}
}
39 changes: 39 additions & 0 deletions src/classes/camera/Camera.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import { FCamera, FComponent, FScene } from '@fibbojs/2d'

export interface CameraOptions {
constY: number
target: FComponent
zoom: number
}

export class Camera extends FCamera {
target: FComponent
constY: number
zoom: number

constructor(scene: FScene, options: CameraOptions) {
super(scene)
this.target = options.target
this.constY = options.constY || 0
this.zoom = options.zoom || 1
}

onFrame(_delta: number): void {
// Move the camera to the target
this.scene.viewport.moveCenter(
this.target.transform.position.x * 100 + this.position.x * 100,
this.constY * 100
)
}

__ON_CAMERA_ADDED_TO_SCENE_PLEASE_DO_NOT_CALL_THIS_BY_HAND__(): void {
// Disable all plugins on the pixi viewport
this.scene.viewport.plugins.pause('drag')
this.scene.viewport.plugins.pause('pinch')
this.scene.viewport.plugins.pause('wheel')
this.scene.viewport.plugins.pause('decelerate')
}



}
23 changes: 14 additions & 9 deletions src/classes/controllers/CharacterController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ export class CharacterController extends FController {

// Define default values
const DEFAULT_OPTIONS = {
speed: 1,
speed: 2,
}
// Apply default options
options = { ...DEFAULT_OPTIONS, ...options }
Expand All @@ -65,7 +65,7 @@ export class CharacterController extends FController {
this.speed = options.speed

// Initialize the y velocity
this.yVelocity = -30
this.yVelocity = -50

// Map of the movements (will be updated by the keyboard)
this.inputs = {
Expand All @@ -87,16 +87,23 @@ export class CharacterController extends FController {
})
fKeyboard.onKeyDown('a', () => {
this.inputs.left = true
if(this.component.container.scale.x > 0)
this.component.container.scale.x *= -1
})
fKeyboard.onKeyDown('d', () => {
this.inputs.right = true
if(this.component.container.scale.x < 0)
this.component.container.scale.x *= -1

})
// For AZERTY keyboards
fKeyboard.onKeyDown('z', () => {
this.inputs.up = true
})
fKeyboard.onKeyDown('q', () => {
this.inputs.left = true
if(this.component.container.scale.x > 0)
this.component.container.scale.x *= -1
})

// Key up
Expand All @@ -123,7 +130,7 @@ export class CharacterController extends FController {
// Jump
fKeyboard.onKeyDown(' ', () => {
if(this.jumpAvailable) {
this.yVelocity = 38
this.yVelocity = 50
this.jumpAvailable = false;
}
})
Expand All @@ -150,7 +157,7 @@ export class CharacterController extends FController {
/**
* Return the corrected movements for the current frame.
*/
getCorrectedMovements(): { x: number, y: number } {
getCorrectedMovements(delta: number): { x: number, y: number } {
const movementDirection = new RAPIER.Vector2(0, 0)
// Compute the movement direction
movementDirection.x = this.inputs.left ? -1 : this.inputs.right ? 1 : 0
Expand All @@ -167,23 +174,21 @@ export class CharacterController extends FController {
RAPIER.QueryFilterFlags.EXCLUDE_SENSORS,
)

// If yVelocity is not 0, apply gravity
// Apply gravity
if (this.yVelocity > this.scene.world.gravity.y) {
this.yVelocity += this.scene.world.gravity.y * 0.00981 * 4
this.yVelocity += this.scene.world.gravity.y * delta * 11
}
else {
this.yVelocity = this.scene.world.gravity.y
}



// Get the corrected movement
return this.characterController.computedMovement()
}

onFrame(delta: number): void {
// Get the corrected movement
const correctedMovement = this.getCorrectedMovements()
const correctedMovement = this.getCorrectedMovements(delta)

// Apply the movement to the rigid body
this.component.rigidBody?.rigidBody.setNextKinematicTranslation({
Expand Down
97 changes: 97 additions & 0 deletions src/classes/controllers/EnemyController.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
import RAPIER from '@dimforge/rapier2d'
import {FController, FShapes} from '@fibbojs/2d'
import type { FControllerOptions, FScene } from '@fibbojs/2d'
import BasicEnemy from "../enemy/BasicEnemy.ts";

export interface CustomControllerOptions extends FControllerOptions {
/**
* The speed of the character.
*/
speed?: number

positionInitial: { x: number, y: number }

positionFinal: { x: number, y: number }

component: BasicEnemy
}

/**
* @description Custom controller
* @category Character
*/
export class EnemyController extends FController {
/**
* The speed of the character.
*/
speed: number

positionInitial: { x: number, y: number }

positionFinal: { x: number, y: number }

compteur: number = 0

component: BasicEnemy

/**
* The scene where the character is.
*/
scene: FScene


/**
* The character controller that will be used to move the character.
*/
characterController: RAPIER.KinematicCharacterController

constructor(scene: FScene, options: CustomControllerOptions) {
super(options)

this.component = options.component

this.positionInitial = options.positionInitial
this.positionFinal = options.positionFinal

// Define default values
const DEFAULT_OPTIONS = {
speed: options.speed,
}

// Apply default options
options = { ...DEFAULT_OPTIONS, ...options }
// Validate options
if (!options.speed)
throw new Error('FibboError: FCharacter requires speed option')

// Store options
this.scene = scene
this.speed = options.speed


// The gap the controller will leave between the character and its environment
const offset = 0.01
// Create the character controller
this.characterController = scene.world.createCharacterController(offset)

// Initialize a sensor
this.component.initSensor({
shape: FShapes.SQUARE,
scale: { x: 1, y: 1 },
})
}

onFrame(delta: number): void {
// Get the corrected movement
const mouvement = Math.sin(this.compteur)
this.compteur += delta

this.component.setPosition({
x: this.component.position.x + mouvement * delta * this.speed,
y: this.component.position.y
})
this.component.sensor?.rigidBody.setTranslation({ x: this.component.position.x, y: this.component.position.y }, true)

this.component.topSensor.sensor?.rigidBody.setTranslation({ x: this.component.position.x, y: this.component.position.y + 0.5 }, true)
}
}
Loading

0 comments on commit b5a14c0

Please sign in to comment.