Add raining effect of cubes
This commit is contained in:
parent
6d43f30c09
commit
1852cf3a7f
1 changed files with 92 additions and 87 deletions
139
main.odin
139
main.odin
|
|
@ -32,7 +32,8 @@ Globals :: struct {
|
||||||
physics_system: ^ecs.PhysicsSystem,
|
physics_system: ^ecs.PhysicsSystem,
|
||||||
camera_system: ^ecs.CameraSystem,
|
camera_system: ^ecs.CameraSystem,
|
||||||
render_system: ^ecs.RenderSystem,
|
render_system: ^ecs.RenderSystem,
|
||||||
entities: []ecs.EntityID,
|
|
||||||
|
time_since_cube: f32,
|
||||||
}
|
}
|
||||||
g: ^Globals
|
g: ^Globals
|
||||||
|
|
||||||
|
|
@ -49,7 +50,7 @@ main :: proc() {
|
||||||
default_context = context
|
default_context = context
|
||||||
|
|
||||||
sa.run({
|
sa.run({
|
||||||
window_title = "ECS",
|
window_title = "ECS Test",
|
||||||
|
|
||||||
allocator = sa.Allocator(sh.allocator(&default_context)),
|
allocator = sa.Allocator(sh.allocator(&default_context)),
|
||||||
logger = sa.Logger(sh.logger(&default_context)),
|
logger = sa.Logger(sh.logger(&default_context)),
|
||||||
|
|
@ -78,6 +79,12 @@ frame_cb:: proc "c" () {
|
||||||
|
|
||||||
dt := f32(sa.frame_duration())
|
dt := f32(sa.frame_duration())
|
||||||
|
|
||||||
|
g.time_since_cube += dt
|
||||||
|
if g.time_since_cube >= 0.2 {
|
||||||
|
create_cube()
|
||||||
|
g.time_since_cube = 0
|
||||||
|
}
|
||||||
|
|
||||||
ecs.physics_system_update(g.physics_system, &g.coordinator, dt)
|
ecs.physics_system_update(g.physics_system, &g.coordinator, dt)
|
||||||
ecs.camera_system_update(g.camera_system, &g.coordinator, dt)
|
ecs.camera_system_update(g.camera_system, &g.coordinator, dt)
|
||||||
ecs.render_system_update(g.render_system, &g.coordinator, dt)
|
ecs.render_system_update(g.render_system, &g.coordinator, dt)
|
||||||
|
|
@ -90,7 +97,6 @@ cleanup_cb :: proc "c" () {
|
||||||
|
|
||||||
ecs.render_system_delete(g.render_system, &g.coordinator)
|
ecs.render_system_delete(g.render_system, &g.coordinator)
|
||||||
|
|
||||||
delete(g.entities)
|
|
||||||
ecs.coordinator_destroy(&g.coordinator)
|
ecs.coordinator_destroy(&g.coordinator)
|
||||||
free(g)
|
free(g)
|
||||||
}
|
}
|
||||||
|
|
@ -101,6 +107,65 @@ event_cb :: proc "c" (event: ^sa.Event) {
|
||||||
ecs.input_system_update(g.input_system, &g.coordinator, event)
|
ecs.input_system_update(g.input_system, &g.coordinator, event)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
create_cube :: proc() {
|
||||||
|
entity := ecs.coordinator_create_entity(&g.coordinator)
|
||||||
|
|
||||||
|
ecs.coordinator_add_component(
|
||||||
|
ecs.GravityComponent,
|
||||||
|
&g.coordinator,
|
||||||
|
entity,
|
||||||
|
ecs.GravityComponent{
|
||||||
|
ecs.Vec3{ 0.0, -2.5, 0.0 }
|
||||||
|
})
|
||||||
|
ecs.coordinator_add_component(
|
||||||
|
ecs.RigidBodyComponent,
|
||||||
|
&g.coordinator,
|
||||||
|
entity,
|
||||||
|
ecs.RigidBodyComponent{
|
||||||
|
velocity = ecs.Vec3{ 0.0, 0.0, 0.0 },
|
||||||
|
acceleration = ecs.Vec3{ 0.0, 0.0, 0.0 },
|
||||||
|
})
|
||||||
|
ecs.coordinator_add_component(
|
||||||
|
ecs.TransformComponent,
|
||||||
|
&g.coordinator,
|
||||||
|
entity,
|
||||||
|
ecs.TransformComponent{
|
||||||
|
position = Vec3{
|
||||||
|
f32(rand.int_max(50)),
|
||||||
|
10,
|
||||||
|
f32(rand.int_max(50)),
|
||||||
|
},
|
||||||
|
rotation = ecs.Vec3{0.0, 0.0, 0.0},
|
||||||
|
scale = ecs.Vec3{1.0, 1.0, 1.0},
|
||||||
|
})
|
||||||
|
ecs.coordinator_add_component(
|
||||||
|
ecs.ColorComponent,
|
||||||
|
&g.coordinator,
|
||||||
|
entity,
|
||||||
|
ecs.ColorComponent{
|
||||||
|
color = Vec4{
|
||||||
|
rand.float32_uniform(0,1),
|
||||||
|
rand.float32_uniform(0,1),
|
||||||
|
rand.float32_uniform(0,1),
|
||||||
|
rand.float32_uniform(0,1),
|
||||||
|
},
|
||||||
|
})
|
||||||
|
ecs.coordinator_add_component(
|
||||||
|
ecs.MeshComponent,
|
||||||
|
&g.coordinator,
|
||||||
|
entity,
|
||||||
|
ecs.MeshComponent{
|
||||||
|
mesh_id = .Cube
|
||||||
|
})
|
||||||
|
ecs.coordinator_add_component(
|
||||||
|
ecs.MaterialComponent,
|
||||||
|
&g.coordinator,
|
||||||
|
entity,
|
||||||
|
ecs.MaterialComponent{
|
||||||
|
material_id = .Grid
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
create_scene :: proc() {
|
create_scene :: proc() {
|
||||||
ecs.coordinator_register_component(ecs.GravityComponent, &g.coordinator)
|
ecs.coordinator_register_component(ecs.GravityComponent, &g.coordinator)
|
||||||
ecs.coordinator_register_component(ecs.RigidBodyComponent, &g.coordinator)
|
ecs.coordinator_register_component(ecs.RigidBodyComponent, &g.coordinator)
|
||||||
|
|
@ -137,11 +202,11 @@ create_scene :: proc() {
|
||||||
ecs.signature_set(&signature, ecs.coordinator_get_component_type(ecs.InputComponent, &g.coordinator))
|
ecs.signature_set(&signature, ecs.coordinator_get_component_type(ecs.InputComponent, &g.coordinator))
|
||||||
ecs.coordinator_set_system_signature(ecs.InputSystem, &g.coordinator, signature)
|
ecs.coordinator_set_system_signature(ecs.InputSystem, &g.coordinator, signature)
|
||||||
|
|
||||||
camera_entity := ecs.coordinator_create_entity(&g.coordinator)
|
user_entity := ecs.coordinator_create_entity(&g.coordinator)
|
||||||
ecs.coordinator_add_component(
|
ecs.coordinator_add_component(
|
||||||
ecs.CameraComponent,
|
ecs.CameraComponent,
|
||||||
&g.coordinator,
|
&g.coordinator,
|
||||||
camera_entity,
|
user_entity,
|
||||||
ecs.CameraComponent{
|
ecs.CameraComponent{
|
||||||
position = { 30, 0, 60 },
|
position = { 30, 0, 60 },
|
||||||
target = { 0, 0, 1 },
|
target = { 0, 0, 1 },
|
||||||
|
|
@ -154,73 +219,13 @@ create_scene :: proc() {
|
||||||
ecs.coordinator_add_component(
|
ecs.coordinator_add_component(
|
||||||
ecs.InputComponent,
|
ecs.InputComponent,
|
||||||
&g.coordinator,
|
&g.coordinator,
|
||||||
camera_entity,
|
user_entity,
|
||||||
ecs.InputComponent{
|
ecs.InputComponent{
|
||||||
key_down = {},
|
key_down = {},
|
||||||
mouse_movement = Vec2{ 0, 0 }
|
mouse_movement = Vec2{ 0, 0 }
|
||||||
})
|
})
|
||||||
|
|
||||||
ecs.render_system_init(g.render_system, camera_entity)
|
ecs.render_system_init(g.render_system, user_entity)
|
||||||
|
|
||||||
g.entities = make([]ecs.EntityID, 1000)
|
|
||||||
for &entity in g.entities {
|
|
||||||
entity = ecs.coordinator_create_entity(&g.coordinator)
|
|
||||||
|
|
||||||
ecs.coordinator_add_component(
|
|
||||||
ecs.GravityComponent,
|
|
||||||
&g.coordinator,
|
|
||||||
entity,
|
|
||||||
ecs.GravityComponent{
|
|
||||||
ecs.Vec3{0.0, -2.82, 0.0}
|
|
||||||
})
|
|
||||||
ecs.coordinator_add_component(
|
|
||||||
ecs.RigidBodyComponent,
|
|
||||||
&g.coordinator,
|
|
||||||
entity,
|
|
||||||
ecs.RigidBodyComponent{
|
|
||||||
velocity = ecs.Vec3{0.0, 0.0, 0.0},
|
|
||||||
acceleration = ecs.Vec3{0.0, 0.0, 0.0},
|
|
||||||
})
|
|
||||||
ecs.coordinator_add_component(
|
|
||||||
ecs.TransformComponent,
|
|
||||||
&g.coordinator,
|
|
||||||
entity,
|
|
||||||
ecs.TransformComponent{
|
|
||||||
position = Vec3{
|
|
||||||
f32(rand.int_max(50)),
|
|
||||||
f32(rand.int_max(10)),
|
|
||||||
f32(rand.int_max(50)),
|
|
||||||
},
|
|
||||||
rotation = ecs.Vec3{0.0, 0.0, 0.0},
|
|
||||||
scale = ecs.Vec3{1.0, 1.0, 1.0},
|
|
||||||
})
|
|
||||||
ecs.coordinator_add_component(
|
|
||||||
ecs.ColorComponent,
|
|
||||||
&g.coordinator,
|
|
||||||
entity,
|
|
||||||
ecs.ColorComponent{
|
|
||||||
color = Vec4{
|
|
||||||
rand.float32_uniform(0,1),
|
|
||||||
rand.float32_uniform(0,1),
|
|
||||||
rand.float32_uniform(0,1),
|
|
||||||
rand.float32_uniform(0,1),
|
|
||||||
},
|
|
||||||
})
|
|
||||||
ecs.coordinator_add_component(
|
|
||||||
ecs.MeshComponent,
|
|
||||||
&g.coordinator,
|
|
||||||
entity,
|
|
||||||
ecs.MeshComponent{
|
|
||||||
mesh_id = .Cube
|
|
||||||
})
|
|
||||||
ecs.coordinator_add_component(
|
|
||||||
ecs.MaterialComponent,
|
|
||||||
&g.coordinator,
|
|
||||||
entity,
|
|
||||||
ecs.MaterialComponent{
|
|
||||||
material_id = .Grid
|
|
||||||
})
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
reset_tracking_allocator :: proc(track: ^mem.Tracking_Allocator) {
|
reset_tracking_allocator :: proc(track: ^mem.Tracking_Allocator) {
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue