0xc3 il y a 2 semaines
Parent
commit
9e20e0ea28
1 fichiers modifiés avec 93 ajouts et 36 suppressions
  1. 93 36
      src/cmd/sandbox/main.odin

+ 93 - 36
src/cmd/sandbox/main.odin

@@ -49,6 +49,8 @@ SLOT_SIZE: f32 : 40.0
 SLOT_SPACING: f32 : 4.0
 SLOT_PADDING: f32 : 4.0
 
+transform_nil: Transform_Component = {}
+
 // --- Data Types ---
 
 Vertex :: struct {
@@ -114,13 +116,14 @@ UI_Context :: struct {
 	line_height:   f32,
 }
 
-ComponentID :: enum u8 {
+Component_Type :: enum {
 	Transform,
 	Physics,
 	Render,
 	Garden,
 	ColliderAABB,
 }
+Component_Set :: distinct bit_set[Component_Type;u64]
 
 Garden_State :: enum u8 {
 	Dry, // Сухой
@@ -144,7 +147,6 @@ Transform_Component :: struct {
 	rotation: Vec3,
 	scale:    Vec3,
 }
-transform_nil := Transform_Component{{}, {}, {1.0, 1.0, 1.0}}
 
 Physics_Component :: struct {
 	velocity: Vec3,
@@ -154,7 +156,7 @@ Render_Component :: struct {}
 
 Entity :: struct {
 	id:         Entity_Id,
-	components: u64,
+	components: Component_Set,
 }
 
 Camera :: struct {
@@ -663,60 +665,61 @@ system_render :: proc(view, proj: linalg.Matrix4f32) {
 	sgl.pop_pipeline()
 }
 
-has_component :: proc(ent: ^Entity, comp_id: ComponentID) -> bool {
-	return (ent.components & (1 << comp_id)) != 0
+has_component :: proc(ent: ^Entity, comp_id: Component_Type) -> bool {
+	return comp_id in ent.components
 }
 
-remove_component :: proc(ent: ^Entity, comp_id: ComponentID) {
-	ent.components &= ~(1 << comp_id)
+remove_component :: proc(ent: ^Entity, comp_id: Component_Type) {
+	ent.components -= {comp_id}
 }
 
 add_transofrm_component :: proc(
 	ent: ^Entity,
 	comp: Transform_Component = {{}, {}, {1.0, 1.0, 1.0}},
 ) {
-	ent.components |= (1 << ComponentID.Transform)
+	ent.components += {.Transform}
 	inject_at(&state.transform_components, ent.id, comp)
 }
 
 get_transform_component :: proc(ent: ^Entity) -> ^Transform_Component {
-	if !has_component(ent, ComponentID.Transform) {
+	if !has_component(ent, Component_Type.Transform) {
+		transform_nil = {}
 		return &transform_nil
 	}
 	return &state.transform_components[ent.id]
 }
 
 add_physics_component :: proc(ent: ^Entity, comp: Physics_Component = {}) {
-	ent.components |= (1 << ComponentID.Physics)
-	// inject_at_soa(&state.physics_components, ent.id, comp)
+	ent.components += {.Physics}
+	inject_at(&state.physics_components, ent.id, comp)
 }
 
 get_physics_component :: proc(ent: ^Entity) -> ^Physics_Component {
-	if !has_component(ent, ComponentID.Physics) {
+	if !has_component(ent, Component_Type.Physics) {
 		return nil
 	}
 	return &state.physics_components[ent.id]
 }
 
 add_garden_component :: proc(ent: ^Entity, comp: Garden_Component) {
-	ent.components |= (1 << ComponentID.Garden)
-	// inject_at_soa(&state.garde_components, ent.id, comp)
+	ent.components += {.Garden}
+	inject_at(&state.garde_components, ent.id, comp)
 }
 
 get_garden_component :: proc(ent: ^Entity) -> ^Garden_Component {
-	if !has_component(ent, ComponentID.Garden) {
+	if !has_component(ent, Component_Type.Garden) {
 		return nil
 	}
 	return &state.garde_components[ent.id]
 }
 
 add_collider_aabb_component :: proc(ent: ^Entity, comp: ColliderAABB_Component) {
-	ent.components |= (1 << ComponentID.ColliderAABB)
-	// inject_at_soa(&state.collider_aabb_components, ent.id, comp)
+	ent.components += {.ColliderAABB}
+	inject_at(&state.collider_aabb_components, ent.id, comp)
 }
 
 get_collider_aabb_component :: proc(ent: ^Entity) -> ^ColliderAABB_Component {
-	if !has_component(ent, ComponentID.ColliderAABB) {
+	if !has_component(ent, Component_Type.ColliderAABB) {
 		return nil
 	}
 	return &state.collider_aabb_components[ent.id]
@@ -750,6 +753,7 @@ make_cube_mesh :: proc(allocator: mem.Allocator) -> Mesh {
 	m: Mesh
 	m.vertices = make(#soa[dynamic]Vertex, 0, 24, allocator)
 	m.indices = make([dynamic]u16, 0, 36, allocator)
+
 	add_face :: proc(m: ^Mesh, p1, p2, p3, p4: [3]f32, color: [3]f32) {
 		start_idx := u16(len(m.vertices))
 		append_soa(
@@ -759,6 +763,7 @@ make_cube_mesh :: proc(allocator: mem.Allocator) -> Mesh {
 			Vertex{p3, color},
 			Vertex{p4, color},
 		)
+		// Индексы: два треугольника, CCW
 		append(
 			&m.indices,
 			start_idx,
@@ -769,13 +774,69 @@ make_cube_mesh :: proc(allocator: mem.Allocator) -> Mesh {
 			start_idx + 3,
 		)
 	}
-	c := [3]f32{1, 1, 1} // Base color, tinted later
-	add_face(&m, {-0.5, -0.5, 0.5}, {-0.5, 0.5, 0.5}, {0.5, 0.5, 0.5}, {0.5, -0.5, 0.5}, c) // Front
-	add_face(&m, {0.5, -0.5, -0.5}, {0.5, 0.5, -0.5}, {-0.5, 0.5, -0.5}, {-0.5, -0.5, -0.5}, c) // Back
-	add_face(&m, {-0.5, 0.5, 0.5}, {-0.5, 0.5, -0.5}, {0.5, 0.5, -0.5}, {0.5, 0.5, 0.5}, c) // Top
-	add_face(&m, {-0.5, -0.5, -0.5}, {-0.5, -0.5, 0.5}, {0.5, -0.5, 0.5}, {0.5, -0.5, -0.5}, c) // Bottom
-	add_face(&m, {0.5, -0.5, 0.5}, {0.5, 0.5, 0.5}, {0.5, 0.5, -0.5}, {0.5, -0.5, -0.5}, c) // Right
-	add_face(&m, {-0.5, -0.5, -0.5}, {-0.5, 0.5, -0.5}, {-0.5, 0.5, 0.5}, {-0.5, -0.5, 0.5}, c) // Left
+
+	c := [3]f32{1, 1, 1} // базовый цвет
+
+	// Front (Z+)
+	add_face(
+		&m,
+		{-0.5, -0.5, 0.5}, // bottom-left
+		{0.5, -0.5, 0.5}, // bottom-right
+		{0.5, 0.5, 0.5}, // top-right
+		{-0.5, 0.5, 0.5}, // top-left
+		c,
+	)
+
+	// Back (Z-)
+	add_face(
+		&m,
+		{0.5, -0.5, -0.5}, // bottom-right
+		{-0.5, -0.5, -0.5}, // bottom-left
+		{-0.5, 0.5, -0.5}, // top-left
+		{0.5, 0.5, -0.5}, // top-right
+		c,
+	)
+
+	// Top (Y+)
+	add_face(
+		&m,
+		{-0.5, 0.5, 0.5}, // front-left
+		{0.5, 0.5, 0.5}, // front-right
+		{0.5, 0.5, -0.5}, // back-right
+		{-0.5, 0.5, -0.5}, // back-left
+		c,
+	)
+
+	// Bottom (Y-)
+	add_face(
+		&m,
+		{-0.5, -0.5, -0.5}, // back-left
+		{0.5, -0.5, -0.5}, // back-right
+		{0.5, -0.5, 0.5}, // front-right
+		{-0.5, -0.5, 0.5}, // front-left
+		c,
+	)
+
+	// Right (X+)
+	add_face(
+		&m,
+		{0.5, -0.5, 0.5}, // front-bottom
+		{0.5, -0.5, -0.5}, // back-bottom
+		{0.5, 0.5, -0.5}, // back-top
+		{0.5, 0.5, 0.5}, // front-top
+		c,
+	)
+
+	// Left (X-)
+	add_face(
+		&m,
+		{-0.5, -0.5, -0.5}, // back-bottom
+		{-0.5, -0.5, 0.5}, // front-bottom
+		{-0.5, 0.5, 0.5}, // front-top
+		{-0.5, 0.5, -0.5}, // back-top
+		c,
+	)
+
 	return m
 }
 
@@ -874,6 +935,7 @@ draw_mesh_instance :: proc(m: ^Mesh, pos: Vec3, scale: Vec3, tint: Vec3) {
 
 draw_grid_thick :: proc(slices: int, spacing: f32, thickness: f32) {
 	half := f32(slices) * spacing * 0.5
+	y: f32 = -0.5
 
 	for i in 0 ..= slices {
 		p := -half + f32(i) * spacing
@@ -882,24 +944,19 @@ draw_grid_thick :: proc(slices: int, spacing: f32, thickness: f32) {
 		if math.abs(p) < 0.0001 {
 			// X axis (red)
 			draw_thick_line(
-				Vec3{-half, 0, 0},
-				Vec3{half, 0, 0},
+				Vec3{-half, y + 0.001, 0},
+				Vec3{half, y, 0},
 				thickness * 2,
-				Vec3{0.8, 0.2, 0.2},
+				Vec3{0.8, y, 0.2},
 			)
 			// Z axis (blue)
-			draw_thick_line(
-				Vec3{0, 0, -half},
-				Vec3{0, 0, half},
-				thickness * 2,
-				Vec3{0.2, 0.2, 0.8},
-			)
+			draw_thick_line(Vec3{0, y, -half}, Vec3{0, y, half}, thickness * 2, Vec3{0.2, y, 0.8})
 			continue
 		}
 
 		// Обычные линии сетки (серые)
-		draw_thick_line(Vec3{-half, 0, p}, Vec3{half, 0, p}, thickness, Vec3{0.35, 0.35, 0.35})
-		draw_thick_line(Vec3{p, 0, -half}, Vec3{p, 0, half}, thickness, Vec3{0.35, 0.35, 0.35})
+		draw_thick_line(Vec3{-half, y, p}, Vec3{half, y, p}, thickness, Vec3{0.35, 0.35, 0.35})
+		draw_thick_line(Vec3{p, y, -half}, Vec3{p, y, half}, thickness, Vec3{0.35, 0.35, 0.35})
 	}
 }