update examples
This commit is contained in:
@@ -30,7 +30,7 @@ app: ecs.App
|
|||||||
|
|
||||||
you can run your app with `run()`. run takes a reference to your app to run it's systems.
|
you can run your app with `run()`. run takes a reference to your app to run it's systems.
|
||||||
|
|
||||||
``` odin
|
```odin
|
||||||
main :: proc() {
|
main :: proc() {
|
||||||
ecs.run(&app)
|
ecs.run(&app)
|
||||||
}
|
}
|
||||||
@@ -103,8 +103,10 @@ since components are saved in storage next to their entity, you can loop through
|
|||||||
|
|
||||||
```odin
|
```odin
|
||||||
main :: proc() {
|
main :: proc() {
|
||||||
for entity_id, component in ecs.slice(&Example) {
|
components := ecs.slice(&Example)
|
||||||
//...
|
entities := ecs.slice_entity(&Example)
|
||||||
|
for v, i in components {
|
||||||
|
e := entities[i] // get entity that owns the component
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
@@ -113,7 +115,7 @@ or if you know your storage only has one entry use `single()`
|
|||||||
|
|
||||||
```odin
|
```odin
|
||||||
main :: proc() {
|
main :: proc() {
|
||||||
entity_id, component := ecs.single(&Example)
|
component, entity_id := ecs.single(&Example)
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
### plugins
|
### plugins
|
||||||
|
|||||||
@@ -1,6 +1,5 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
import "core:fmt"
|
|
||||||
import "core:math/rand"
|
import "core:math/rand"
|
||||||
import "core:strconv"
|
import "core:strconv"
|
||||||
import "core:strings"
|
import "core:strings"
|
||||||
@@ -9,7 +8,7 @@ import rl "vendor:raylib"
|
|||||||
|
|
||||||
app: ecs.App
|
app: ecs.App
|
||||||
|
|
||||||
ball_count: int = 1000
|
ball_count: int = 10000
|
||||||
BallComponent :: struct {
|
BallComponent :: struct {
|
||||||
radius: f32,
|
radius: f32,
|
||||||
speed: f32,
|
speed: f32,
|
||||||
@@ -27,18 +26,14 @@ main :: proc() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
setup_window :: proc() {
|
setup_window :: proc() {
|
||||||
_, window := ecs.single(&ecs.Window)
|
window, _ := ecs.single(&ecs.Window)
|
||||||
window.background = rl.BLACK
|
window.background = rl.BLACK
|
||||||
}
|
}
|
||||||
|
|
||||||
spawn_balls :: proc() {
|
spawn_balls :: proc() {
|
||||||
for i in 0 ..< ball_count {
|
for i in 0 ..< ball_count {
|
||||||
ball := ecs.spawn_entity(&app)
|
ball := ecs.spawn_entity(&app)
|
||||||
ecs.add_component(
|
ecs.add_component(ball, &Ball, BallComponent{radius = 5, speed = rand.float32_range(4, 6)})
|
||||||
ball,
|
|
||||||
&Ball,
|
|
||||||
BallComponent{radius = 10, speed = rand.float32_range(4, 6)},
|
|
||||||
)
|
|
||||||
ecs.add_component(
|
ecs.add_component(
|
||||||
ball,
|
ball,
|
||||||
&ecs.Transform,
|
&ecs.Transform,
|
||||||
@@ -51,10 +46,12 @@ spawn_balls :: proc() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
update_balls :: proc() {
|
update_balls :: proc() {
|
||||||
_, window := ecs.single(&ecs.Window)
|
window, _ := ecs.single(&ecs.Window)
|
||||||
|
|
||||||
for entity, ball in ecs.slice(&Ball) {
|
balls := ecs.slice(&Ball)
|
||||||
transform := ecs.get_component(entity, &ecs.Transform)
|
entities := ecs.slice_entity(&Ball)
|
||||||
|
for ball, i in balls {
|
||||||
|
transform := ecs.get_component(entities[i], &ecs.Transform)
|
||||||
pos := transform.translation
|
pos := transform.translation
|
||||||
|
|
||||||
if pos.x <= 0 || pos.x >= f32(window.width) || pos.y <= 0 || pos.y >= f32(window.height) {
|
if pos.x <= 0 || pos.x >= f32(window.width) || pos.y <= 0 || pos.y >= f32(window.height) {
|
||||||
@@ -63,13 +60,14 @@ update_balls :: proc() {
|
|||||||
dir := rl.Vector2Rotate(rl.Vector2{0, ball.speed}, transform.rotation)
|
dir := rl.Vector2Rotate(rl.Vector2{0, ball.speed}, transform.rotation)
|
||||||
|
|
||||||
transform.translation += dir
|
transform.translation += dir
|
||||||
fmt.println(transform.rotation)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
draw_balls :: proc() {
|
draw_balls :: proc() {
|
||||||
for entity, ball in ecs.slice(&Ball) {
|
balls := ecs.slice(&Ball)
|
||||||
transform := ecs.get_component(entity, &ecs.Transform)
|
entities := ecs.slice_entity(&Ball)
|
||||||
|
for ball, i in balls {
|
||||||
|
transform := ecs.get_component(entities[i], &ecs.Transform)
|
||||||
rl.DrawCircle(
|
rl.DrawCircle(
|
||||||
i32(transform.translation.x),
|
i32(transform.translation.x),
|
||||||
i32(transform.translation.y),
|
i32(transform.translation.y),
|
||||||
@@ -80,7 +78,9 @@ draw_balls :: proc() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
show_framerate :: proc() {
|
show_framerate :: proc() {
|
||||||
buf: [19]byte
|
fps_buf: [19]byte
|
||||||
framerate := strconv.write_int(buf[:], i64(1 / rl.GetFrameTime()), 10)
|
framerate := strconv.write_int(fps_buf[:], i64(1 / rl.GetFrameTime()), 10)
|
||||||
rl.DrawText(strings.clone_to_cstring(framerate), 0, 0, 24, rl.WHITE)
|
rl.DrawText(strings.clone_to_cstring(framerate), 0, 0, 24, rl.WHITE)
|
||||||
|
rl.DrawText(" fps", 0, 0, 24, rl.WHITE)
|
||||||
|
rl.DrawText("10000 balls", 0, 24, 24, rl.WHITE)
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user