Skip to content

Commit

Permalink
chore: format many files
Browse files Browse the repository at this point in the history
  • Loading branch information
lajbel committed Oct 23, 2024
1 parent fd84dd6 commit 9b3d2b9
Show file tree
Hide file tree
Showing 21 changed files with 537 additions and 504 deletions.
2 changes: 1 addition & 1 deletion examples/animation.js
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ timedSquaringBean.animate(
},
);

// We'll move this bean in a curve
// We'll move this bean in a curve
// Using spline interpolation to move according to a smoothened path
const curvingBean = add([
sprite("bean"),
Expand Down
78 changes: 39 additions & 39 deletions examples/binding.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,68 +2,68 @@

// You can set the input bindings for your game!
kaplay({
buttons: {
// Buttons for jumping
"jump": {
// When using a gamepad the button for jumping will be south
gamepad: ["south"],
// When using a keyboard the button will be "up" or "w"
keyboard: ["up", "w"],
// When using a mouse the button will be "left"
mouse: "left",
},
// Buttons for inspecting
"inspect": {
gamepad: "east",
keyboard: "f",
mouse: "right",
},
},
buttons: {
// Buttons for jumping
"jump": {
// When using a gamepad the button for jumping will be south
gamepad: ["south"],
// When using a keyboard the button will be "up" or "w"
keyboard: ["up", "w"],
// When using a mouse the button will be "left"
mouse: "left",
},
// Buttons for inspecting
"inspect": {
gamepad: "east",
keyboard: "f",
mouse: "right",
},
},
});

loadBean()
loadBean();

// Set the gravity acceleration (pixels per second)
setGravity(1600);

// Add player game object
const player = add([
sprite("bean"),
pos(center()),
area(),
// body() component gives the ability to respond to gravity
body(),
sprite("bean"),
pos(center()),
area(),
// body() component gives the ability to respond to gravity
body(),
]);

// Add a platform to hold the player
add([
rect(width(), 48),
outline(4),
area(),
pos(0, height() - 48),
// Give objects a body() component if you don't want other solid objects pass through
body({ isStatic: true }),
rect(width(), 48),
outline(4),
area(),
pos(0, height() - 48),
// Give objects a body() component if you don't want other solid objects pass through
body({ isStatic: true }),
]);

// Adds an object with a text
add([
text("Press jump button", { width: width() / 2 }),
pos(12, 12),
text("Press jump button", { width: width() / 2 }),
pos(12, 12),
]);

// This runs when the button for "jump" is pressed (will be on any input device)
onButtonPress("jump", () => {
// You can get the type of device that the last input was inputted in!
debug.log(getLastInputDeviceType());
// You can get the type of device that the last input was inputted in!
debug.log(getLastInputDeviceType());

// Now we'll check if the player is on the ground to make it jump
if (player.isGrounded()) {
// .jump() is provided by body()
player.jump();
}
// Now we'll check if the player is on the ground to make it jump
if (player.isGrounded()) {
// .jump() is provided by body()
player.jump();
}
});

// When the button for inspecting is pressed we will log in the debug console for our game the text "inspecting"
onButtonDown("inspect", () => {
debug.log("inspecting");
debug.log("inspecting");
});
6 changes: 5 additions & 1 deletion examples/button.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,11 @@ kaplay({
onUpdate(() => setCursor("default"));

// Function that adds a button to the game with a given text, position and function
function addButton(txt = "start game", p = vec2(200, 100), f = () => debug.log("hello")) {
function addButton(
txt = "start game",
p = vec2(200, 100),
f = () => debug.log("hello"),
) {
// add a parent background object
const btn = add([
rect(240, 80, { radius: 8 }),
Expand Down
2 changes: 1 addition & 1 deletion examples/camera.js
Original file line number Diff line number Diff line change
Expand Up @@ -103,4 +103,4 @@ onClick(() => {
// Use toWorld() to transform a screen-space coordinate (like mousePos()) to
// the world-space coordinate, which has the camera transform applied
addKaboom(toWorld(mousePos()));
});
});
2 changes: 1 addition & 1 deletion examples/collision.js
Original file line number Diff line number Diff line change
Expand Up @@ -121,4 +121,4 @@ player.onUpdate(() => {
// Can also be toggled by pressing F1
debug.inspect = true;

// Check out https://kaplayjs.com/doc/AreaComp/ for everything area() provides
// Check out https://kaplayjs.com/doc/AreaComp/ for everything area() provides
4 changes: 2 additions & 2 deletions examples/collisionshapes.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,9 @@ loop(1, () => {
body(),
offscreen({ destroy: true, distance: 10 }),
]);

// getTreeRoot() gets the root of the game, the object that holds every other object
// This line basically means that if there are more than 20 objects, we destroy the last one
// This line basically means that if there are more than 20 objects, we destroy the last one
if (getTreeRoot().children.length > 20) {
destroy(getTreeRoot().children[1]);
}
Expand Down
164 changes: 82 additions & 82 deletions examples/component.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,103 +8,103 @@ loadBean();
// Components are just function that returns a js object that follows a certain format
// This object contains certain properties which then become available in your object to use
function funky() {
// Can use local closed variables to store component state
let isFunky = false;

return {
// ------------------
// Special properties that controls the behavior of the component (all optional)

// These properties (id and require specially id) are handled by kaplay, id is the name of the component
// If you want to get all objects with this component you can do get("funky")
// Be careful to tag objects with what might be the id of a component

id: "funky", // The name of the component
require: ["scale", "color"], // If this component depend on any other components
// If the you put components in require and attach this component to an object that doesn't have these components
// The game will throw an error

// Runs when the host object is added to the game
add() {
// E.g. Register some events from other components, do some bookkeeping, etc.
},

// Runs every frame as long as the host object exists
update() {
if (!isFunky) return;

// "this" in all component methods refers to the the game object this component is attached to
// Here we're updating some properties provided by other components
this.color = rgb(rand(0, 255), rand(0, 255), rand(0, 255));
this.scale = vec2(rand(1, 2));
},

// Runs every frame (after update) as long as the host object exists
draw() {
// E.g. Custom drawXXX() operations.
},

// Runs when the host object is destroyed
destroy() {
// E.g. Clean up event handlers, etc.
},

// When you press F1 you can get a list of inspect properties a component might provide for an object
// Here you can provide custom ones
inspect() {
return "funky: " + isFunky;
},

// ------------------
// All other properties and methods are directly assigned to the host object

// This means that the object is getting funky, not that you're getting the property funky lol!
getFunky() {
isFunky = true;
},
};
// Can use local closed variables to store component state
let isFunky = false;

return {
// ------------------
// Special properties that controls the behavior of the component (all optional)

// These properties (id and require specially id) are handled by kaplay, id is the name of the component
// If you want to get all objects with this component you can do get("funky")
// Be careful to tag objects with what might be the id of a component

id: "funky", // The name of the component
require: ["scale", "color"], // If this component depend on any other components
// If the you put components in require and attach this component to an object that doesn't have these components
// The game will throw an error

// Runs when the host object is added to the game
add() {
// E.g. Register some events from other components, do some bookkeeping, etc.
},

// Runs every frame as long as the host object exists
update() {
if (!isFunky) return;

// "this" in all component methods refers to the the game object this component is attached to
// Here we're updating some properties provided by other components
this.color = rgb(rand(0, 255), rand(0, 255), rand(0, 255));
this.scale = vec2(rand(1, 2));
},

// Runs every frame (after update) as long as the host object exists
draw() {
// E.g. Custom drawXXX() operations.
},

// Runs when the host object is destroyed
destroy() {
// E.g. Clean up event handlers, etc.
},

// When you press F1 you can get a list of inspect properties a component might provide for an object
// Here you can provide custom ones
inspect() {
return "funky: " + isFunky;
},

// ------------------
// All other properties and methods are directly assigned to the host object

// This means that the object is getting funky, not that you're getting the property funky lol!
getFunky() {
isFunky = true;
},
};
}

// Adds an object with the funky component
const bean = add([
sprite("bean"),
pos(center()),
anchor("center"),
scale(1),
color(),
area(),
// Use our component here
funky(),
// Tags are empty components, it's equivalent to a { id: "friend" }
"friend",
// Plain objects here are components too and work the same way, except unnamed
{
coolness: 100,
friends: [],
},
sprite("bean"),
pos(center()),
anchor("center"),
scale(1),
color(),
area(),
// Use our component here
funky(),
// Tags are empty components, it's equivalent to a { id: "friend" }
"friend",
// Plain objects here are components too and work the same way, except unnamed
{
coolness: 100,
friends: [],
},
]);

onKeyPress("space", () => {
// .coolness is from our plain object 'unnamed component'
if (bean.coolness >= 100) {
// We can use .getFunky() provided by the funky() component now
bean.getFunky();
}
// .coolness is from our plain object 'unnamed component'
if (bean.coolness >= 100) {
// We can use .getFunky() provided by the funky() component now
bean.getFunky();
}
});

onKeyPress("r", () => {
// .use() is on every game object, it adds a component at runtime
bean.use(rotate(rand(0, 360)));
// .use() is on every game object, it adds a component at runtime
bean.use(rotate(rand(0, 360)));
});

onKeyPress("escape", () => {
// .unuse() removes a component from the game object
// The tag is the one that appears on the id
bean.unuse("funky");
// .unuse() removes a component from the game object
// The tag is the one that appears on the id
bean.unuse("funky");
});

// Adds a text object
add([
text("Press space to get funky", { width: width() }),
pos(12, 12),
text("Press space to get funky", { width: width() }),
pos(12, 12),
]);
Loading

0 comments on commit 9b3d2b9

Please sign in to comment.