-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
f1378b8
commit 2c0d212
Showing
31 changed files
with
176 additions
and
149 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,20 +1,20 @@ | ||
import { Entity, ActionMap, Position, ValidAction } from "@piggo-gg/core"; | ||
import { Entity, ActionMap, Position, Action } from "@piggo-gg/core"; | ||
|
||
const TURN_SPEED = 0.1; | ||
const SLIDE_FACTOR = 1.5; | ||
const SPEED = 2; | ||
const SPEED = 200; | ||
|
||
export type VehicleMovementActions = "up" | "down" | "left" | "right" | "skidleft" | "skidright"; | ||
|
||
export const VehicleMovement: ActionMap<VehicleMovementActions> = { | ||
"up": ValidAction(({ components: { position } }: Entity<Position>) => { | ||
"up": Action((_, { components: { position } }: Entity<Position>) => { | ||
const x = Math.cos(position.data.rotation - Math.PI / 1.35) * SPEED; | ||
const y = Math.sin(position.data.rotation - Math.PI / 1.35) * SPEED; | ||
position.setVelocity({ x, y }); | ||
}), | ||
"down": ValidAction(({ components: { position } }: Entity<Position>) => position.setVelocity({ x: 0, y: 0 })), | ||
"left": ValidAction(({ components: { position } }: Entity<Position>) => position.rotateDown(TURN_SPEED)), | ||
"right": ValidAction(({ components: { position } }: Entity<Position>) => position.rotateUp(TURN_SPEED)), | ||
"skidleft": ValidAction(({ components: { position } }: Entity<Position>) => position.rotateDown(TURN_SPEED * SLIDE_FACTOR)), | ||
"skidright": ValidAction(({ components: { position } }: Entity<Position>) => position.rotateUp(TURN_SPEED * SLIDE_FACTOR)) | ||
"down": Action((_, { components: { position } }: Entity<Position>) => position.setVelocity({ x: 0, y: 0 })), | ||
"left": Action((_, { components: { position } }: Entity<Position>) => position.rotateDown(TURN_SPEED)), | ||
"right": Action((_, { components: { position } }: Entity<Position>) => position.rotateUp(TURN_SPEED)), | ||
"skidleft": Action((_, { components: { position } }: Entity<Position>) => position.rotateDown(TURN_SPEED * SLIDE_FACTOR)), | ||
"skidright": Action((_, { components: { position } }: Entity<Position>) => position.rotateUp(TURN_SPEED * SLIDE_FACTOR)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,51 +1,75 @@ | ||
import { ActionMap, Entity, Position, ValidAction, currentJoystickPosition } from "@piggo-gg/core"; | ||
import { ActionMap, ControllerMap, Entity, Position, currentJoystickPosition } from "@piggo-gg/core"; | ||
|
||
const speed = 140; | ||
const speedDiagonal = speed / Math.sqrt(2); | ||
const speedHorizontal = speed / 2; | ||
|
||
export type WASDMovementActions = "up" | "down" | "left" | "right" | "upleft" | "upright" | "downleft" | "downright"; | ||
|
||
export const WASDMovementPhysics: ActionMap<WASDMovementActions> = { | ||
"up": ValidAction((entity: Entity<Position>) => move(entity, "u", { x: -speedDiagonal, y: -speedDiagonal })), | ||
"down": ValidAction((entity: Entity<Position>) => move(entity, "d", { x: speedDiagonal, y: speedDiagonal })), | ||
"left": ValidAction((entity: Entity<Position>) => move(entity, "l", { x: -speedHorizontal, y: speedHorizontal })), | ||
"right": ValidAction((entity: Entity<Position>) => move(entity, "r", { x: speedHorizontal, y: -speedHorizontal })), | ||
"upleft": ValidAction((entity: Entity<Position>) => move(entity, "ul", { x: -speed, y: 0 })), | ||
"upright": ValidAction((entity: Entity<Position>) => move(entity, "ur", { x: 0, y: -speed })), | ||
"downleft": ValidAction((entity: Entity<Position>) => move(entity, "dl", { x: 0, y: speed })), | ||
"downright": ValidAction((entity: Entity<Position>) => move(entity, "dr", { x: speed, y: 0 })) | ||
} | ||
type WASDParams = { animation: string, x: number, y: number }; | ||
|
||
// shout out to chatgpt (best programmer i know fr) | ||
const move = (entity: Entity<Position>, animation: string | undefined, velocity: { x: number, y: number }) => { | ||
const { position, renderable } = entity.components; | ||
const getAnimationXYForJoystick = (): WASDParams => { | ||
console.log("getAnimationXYForJoystick"); | ||
const { power, angle } = currentJoystickPosition; | ||
|
||
// make the joystick feel stiff | ||
const powerToApply = Math.min(1, power * 2); | ||
|
||
if (currentJoystickPosition.power) { | ||
const { power, angle } = currentJoystickPosition; | ||
// Adjusting the angle to match the isometric projection | ||
const angleAdjusted = angle + 45; | ||
const angleRad = angleAdjusted * Math.PI / 180; | ||
|
||
// make the joystick feel stiff | ||
const powerToApply = Math.min(1, power * 2); | ||
// x,y components of the vector | ||
let cosAngle = Math.cos(angleRad); | ||
let sinAngle = -Math.sin(angleRad); | ||
|
||
// Adjusting the angle to match the isometric projection | ||
const angleAdjusted = angle + 45; | ||
const angleRad = angleAdjusted * Math.PI / 180; | ||
// Adjusting for consistent speed in isometric projection | ||
const magnitude = Math.sqrt(cosAngle * cosAngle + sinAngle * sinAngle); | ||
cosAngle /= magnitude; | ||
sinAngle /= magnitude; | ||
|
||
// x,y components of the vector | ||
let cosAngle = Math.cos(angleRad); | ||
let sinAngle = -Math.sin(angleRad); | ||
// Apply the power to the vector | ||
const x = cosAngle * powerToApply * speed; | ||
const y = sinAngle * powerToApply * speed; | ||
|
||
// Adjusting for consistent speed in isometric projection | ||
const magnitude = Math.sqrt(cosAngle * cosAngle + sinAngle * sinAngle); | ||
cosAngle /= magnitude; | ||
sinAngle /= magnitude; | ||
let animation = ""; | ||
|
||
// Apply the power to the vector | ||
velocity.x = cosAngle * powerToApply * speed; | ||
velocity.y = sinAngle * powerToApply * speed; | ||
} | ||
const increment = currentJoystickPosition.angle / (360 / 8); | ||
|
||
position.setVelocity(velocity); | ||
((increment > 0.5) && (increment < 1.5)) ? | ||
animation = "ur" : | ||
((increment > 0.5) && (increment < 2.5)) ? animation = "u" : | ||
((increment > 0.5) && (increment < 3.5)) ? animation = "ul" : | ||
((increment > 0.5) && (increment < 4.5)) ? animation = "l" : | ||
((increment > 0.5) && (increment < 5.5)) ? animation = "dl" : | ||
((increment > 0.5) && (increment < 6.5)) ? animation = "d" : | ||
((increment > 0.5) && (increment < 7.5)) ? animation = "dr" : | ||
animation = "r"; | ||
|
||
if (renderable && animation) renderable.setAnimation(animation); | ||
return { x, y, animation }; | ||
} | ||
|
||
export const WASDController: ControllerMap<"move", WASDParams> = { | ||
keyboard: { | ||
"a,d": null, "w,s": null, | ||
"w,a": { action: "move", params: { animation: "ul", x: -speed, y: 0 } }, | ||
"w,d": { action: "move", params: { animation: "ur", x: 0, y: -speed } }, | ||
"s,a": { action: "move", params: { animation: "dl", x: 0, y: speed } }, | ||
"s,d": { action: "move", params: { animation: "dr", x: speed, y: 0 } }, | ||
"w": { action: "move", params: { animation: "u", x: -speedDiagonal, y: -speedDiagonal } }, | ||
"s": { action: "move", params: { animation: "d", x: speedDiagonal, y: speedDiagonal } }, | ||
"a": { action: "move", params: { animation: "l", x: -speedHorizontal, y: speedHorizontal } }, | ||
"d": { action: "move", params: { animation: "r", x: speedHorizontal, y: -speedHorizontal } } | ||
}, | ||
joystick: () => ({ action: "move", params: getAnimationXYForJoystick() }) | ||
} | ||
|
||
export const WASDmove = (params: WASDParams, entity: Entity<Position>) => { | ||
const { position, renderable } = entity.components; | ||
|
||
position.setVelocity({ x: params.x, y: params.y }); | ||
|
||
if (renderable && params.animation) renderable.setAnimation(params.animation); | ||
} | ||
|
||
export const WASDActionMap: ActionMap<"move", WASDParams> = { | ||
move: { apply: WASDmove } | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.