jecs/docs/learn/overview/first-jecs-project.md

214 lines
5.5 KiB
Markdown
Raw Normal View History

2025-02-19 16:10:38 +00:00
# Your First Jecs Project
2025-02-19 16:10:38 +00:00
This tutorial will walk you through creating your first project with Jecs, demonstrating core concepts and best practices.
2024-07-31 02:16:28 +00:00
2025-02-19 16:10:38 +00:00
## Setting Up
First, make sure you have Jecs installed. If not, check the [Getting Started](get-started.md) guide.
::: code-group
```lua [luau]
local jecs = require(path.to.jecs)
2024-07-31 02:16:28 +00:00
local world = jecs.World.new()
```
```typescript [typescript]
2025-02-19 16:10:38 +00:00
import { World } from "@rbxts/jecs";
const world = new World();
2024-07-31 02:16:28 +00:00
```
:::
2025-02-19 16:10:38 +00:00
## Creating Components
2024-07-31 02:16:28 +00:00
2025-02-19 16:10:38 +00:00
Let's create some basic components for a simple game:
2024-07-31 02:16:28 +00:00
2025-02-19 16:10:38 +00:00
::: code-group
```lua [luau]
-- Position in 3D space
local Position = world:component() :: jecs.Entity<Vector3>
-- Velocity for movement
local Velocity = world:component() :: jecs.Entity<Vector3>
-- Health for gameplay
local Health = world:component() :: jecs.Entity<number>
2024-07-31 02:16:28 +00:00
2025-02-19 16:10:38 +00:00
-- Tag for marking enemies
local IsEnemy = world:component()
```
2024-07-31 02:16:28 +00:00
```typescript [typescript]
2025-02-19 16:10:38 +00:00
// Position in 3D space
const Position = world.component<Vector3>();
// Velocity for movement
const Velocity = world.component<Vector3>();
// Health for gameplay
const Health = world.component<number>();
2024-07-31 02:16:28 +00:00
2025-02-19 16:10:38 +00:00
// Tag for marking enemies
const IsEnemy = world.component();
2024-07-31 02:16:28 +00:00
```
:::
2025-02-19 16:10:38 +00:00
## Creating Game Systems
2024-07-31 02:16:28 +00:00
2025-02-19 16:10:38 +00:00
Let's create some basic systems to handle movement and gameplay:
2024-07-31 02:16:28 +00:00
2025-02-19 16:10:38 +00:00
### Movement System
::: code-group
```lua [luau]
-- Cache the query for better performance
local movementQuery = world:query(Position, Velocity):cached()
2024-07-31 02:16:28 +00:00
2025-02-19 16:10:38 +00:00
local function updateMovement(deltaTime)
for id, position, velocity in movementQuery:iter() do
-- Update position based on velocity and time
world:set(id, Position, position + velocity * deltaTime)
end
2024-07-31 02:16:28 +00:00
end
```
2025-02-19 16:10:38 +00:00
```typescript [typescript]
// Cache the query for better performance
const movementQuery = world.query(Position, Velocity).cached();
function updateMovement(deltaTime: number) {
for (const [id, position, velocity] of movementQuery) {
// Update position based on velocity and time
world.set(id, Position, position.add(velocity.mul(deltaTime)));
}
}
```
:::
2024-07-31 02:16:28 +00:00
2025-02-19 16:10:38 +00:00
### Damage System
::: code-group
```lua [luau]
local function applyDamage(entity, amount)
local currentHealth = world:get(entity, Health)
if currentHealth then
local newHealth = currentHealth - amount
if newHealth <= 0 then
world:delete(entity)
else
world:set(entity, Health, newHealth)
end
end
end
```
2024-07-31 02:16:28 +00:00
```typescript [typescript]
2025-02-19 16:10:38 +00:00
function applyDamage(entity: Entity, amount: number) {
const currentHealth = world.get(entity, Health);
if (currentHealth) {
const newHealth = currentHealth - amount;
if (newHealth <= 0) {
world.delete(entity);
} else {
world.set(entity, Health, newHealth);
}
}
}
```
:::
## Creating Game Entities
2024-07-31 02:16:28 +00:00
2025-02-19 16:10:38 +00:00
Now let's create some game entities:
2024-07-31 02:16:28 +00:00
2025-02-19 16:10:38 +00:00
::: code-group
```lua [luau]
-- Create a player
local player = world:entity()
world:set(player, Position, Vector3.new(0, 0, 0))
world:set(player, Velocity, Vector3.new(0, 0, 0))
world:set(player, Health, 100)
-- Create an enemy
local enemy = world:entity()
world:set(enemy, Position, Vector3.new(10, 0, 10))
world:set(enemy, Health, 50)
world:add(enemy, IsEnemy)
```
```typescript [typescript]
// Create a player
const player = world.entity();
world.set(player, Position, new Vector3(0, 0, 0));
world.set(player, Velocity, new Vector3(0, 0, 0));
world.set(player, Health, 100);
// Create an enemy
const enemy = world.entity();
world.set(enemy, Position, new Vector3(10, 0, 10));
world.set(enemy, Health, 50);
world.add(enemy, IsEnemy);
```
:::
## Adding Relationships
Let's add some parent-child relationships:
::: code-group
```lua [luau]
-- Create weapon entity
local weapon = world:entity()
world:add(weapon, pair(jecs.ChildOf, player))
-- Query for player's children
for child in world:query(pair(jecs.ChildOf, player)) do
print("Found player's child:", child)
end
```
```typescript [typescript]
// Create weapon entity
const weapon = world.entity();
world.add(weapon, pair(jecs.ChildOf, player));
// Query for player's children
for (const [child] of world.query(pair(jecs.ChildOf, player))) {
print("Found player's child:", child);
2024-07-31 02:16:28 +00:00
}
```
:::
2025-02-19 16:10:38 +00:00
## Running the Game Loop
Here's how to put it all together:
::: code-group
```lua [luau]
local RunService = game:GetService("RunService")
RunService.Heartbeat:Connect(function(deltaTime)
-- Update movement
updateMovement(deltaTime)
-- Other game systems...
end)
```
```typescript [typescript]
const RunService = game.GetService("RunService");
RunService.Heartbeat.Connect((deltaTime) => {
// Update movement
updateMovement(deltaTime);
// Other game systems...
});
```
:::
## Next Steps
1. Learn more about [Entities and Components](../concepts/entities-and-components.md)
2. Explore [Queries](../concepts/queries.md) in depth
3. Understand [Relationships](../concepts/relationships.md)
4. Check out [Component Traits](../concepts/component-traits.md)
5. Browse the [API Reference](../../api/jecs.md)
2024-07-31 02:16:28 +00:00
## Where To Get Help
If you are encountering problems, there are resources for you to get help:
2024-07-31 02:16:28 +00:00
- [Roblox OSS Discord server](https://discord.gg/h2NV8PqhAD) has a [#jecs](https://discord.com/channels/385151591524597761/1248734074940559511) thread under the [#projects](https://discord.com/channels/385151591524597761/1019724676265676930) channel
- [Open an issue](https://github.com/ukendio/jecs/issues) if you run into bugs or have feature requests
- Dive into the nitty gritty in the [thesis paper](https://raw.githubusercontent.com/Ukendio/jecs/main/thesis/drafts/1/paper.pdf)