From: Brendan Hansen Date: Wed, 2 Mar 2022 03:28:02 +0000 (-0600) Subject: code cleanup X-Git-Url: https://git.brendanfh.com/?a=commitdiff_plain;h=40400912b447250b3048b8c4260095bf279ebb1d;p=bar-game.git code cleanup --- diff --git a/run_tree/scenes/quick_save_new.scene b/run_tree/scenes/quick_save_new.scene index 45034de..97090ee 100644 --- a/run_tree/scenes/quick_save_new.scene +++ b/run_tree/scenes/quick_save_new.scene @@ -1,14 +1,16 @@ [Background] id = 100 flags = 0 -pos.x = 0 -pos.y = 0 +pos.x = 0.0000 +pos.y = 0.0000 +size.x = 0.0000 +size.y = 0.0000 :RenderComponent -layer = -1 -color.r = 1 -color.g = 1 -color.b = 1 -color.a = 1 +layer = 0 +color.r = 1.0000 +color.g = 1.0000 +color.b = 1.0000 +color.a = 1.0000 [Wall] id = 10 @@ -38,32 +40,6 @@ color.g = 1.0000 color.b = 1.0000 color.a = 1.0000 -[Player] -id = 12 -flags = 2 -pos.x = 312.5422 -pos.y = 271.0389 -size.x = 16.0000 -size.y = 32.0000 -:MovementComponent -controls.up = 87 -controls.down = 83 -controls.left = 65 -controls.right = 68 -controls.interact = 70 -controls.pick_up = 71 -facing = 3 -velocity.x = -0.4420 -velocity.y = 0.4767 -:PlayerComponent -holding = 0 -:RenderComponent -layer = 10 -color.r = 0.0000 -color.g = 1.0000 -color.b = 0.0000 -color.a = 1.0000 - [Door] id = 13 flags = 3 @@ -131,15 +107,10 @@ pos.x = 136.0000 pos.y = 336.0000 size.x = 16.0000 size.y = 32.0000 -:RenderComponent -layer = 0 -color.r = 1.0000 -color.g = 1.0000 -color.b = 1.0000 -color.a = 1.0000 :FurnitureComponent furniture_type = 0 taken = false +:SpriteRenderComponent sprite.sheet = "./assets/images/spritesheet.png" sprite.pos.x = 32.0000 sprite.pos.y = 32.0000 @@ -157,15 +128,10 @@ pos.x = 184.0000 pos.y = 336.0000 size.x = 16.0000 size.y = 32.0000 -:RenderComponent -layer = 0 -color.r = 1.0000 -color.g = 1.0000 -color.b = 1.0000 -color.a = 1.0000 :FurnitureComponent furniture_type = 0 taken = false +:SpriteRenderComponent sprite.sheet = "./assets/images/spritesheet.png" sprite.pos.x = 48.0000 sprite.pos.y = 32.0000 @@ -183,15 +149,10 @@ pos.x = 160.0000 pos.y = 336.0000 size.x = 32.0000 size.y = 32.0000 -:RenderComponent -layer = 0 -color.r = 1.0000 -color.g = 1.0000 -color.b = 1.0000 -color.a = 1.0000 :FurnitureComponent furniture_type = 1 taken = false +:SpriteRenderComponent sprite.sheet = "./assets/images/spritesheet.png" sprite.pos.x = 48.0000 sprite.pos.y = 32.0000 @@ -209,15 +170,10 @@ pos.x = 136.0000 pos.y = 256.0000 size.x = 16.0000 size.y = 32.0000 -:RenderComponent -layer = 0 -color.r = 1.0000 -color.g = 1.0000 -color.b = 1.0000 -color.a = 1.0000 :FurnitureComponent furniture_type = 0 taken = false +:SpriteRenderComponent sprite.sheet = "./assets/images/spritesheet.png" sprite.pos.x = 32.0000 sprite.pos.y = 32.0000 @@ -235,15 +191,10 @@ pos.x = 184.0000 pos.y = 256.0000 size.x = 16.0000 size.y = 32.0000 -:RenderComponent -layer = 0 -color.r = 1.0000 -color.g = 1.0000 -color.b = 1.0000 -color.a = 1.0000 :FurnitureComponent furniture_type = 0 taken = false +:SpriteRenderComponent sprite.sheet = "./assets/images/spritesheet.png" sprite.pos.x = 48.0000 sprite.pos.y = 32.0000 @@ -261,15 +212,10 @@ pos.x = 160.0000 pos.y = 256.0000 size.x = 32.0000 size.y = 32.0000 -:RenderComponent -layer = 0 -color.r = 1.0000 -color.g = 1.0000 -color.b = 1.0000 -color.a = 1.0000 :FurnitureComponent furniture_type = 1 taken = false +:SpriteRenderComponent sprite.sheet = "./assets/images/spritesheet.png" sprite.pos.x = 48.0000 sprite.pos.y = 32.0000 @@ -287,15 +233,10 @@ pos.x = 536.0000 pos.y = 256.0000 size.x = 16.0000 size.y = 32.0000 -:RenderComponent -layer = 0 -color.r = 1.0000 -color.g = 1.0000 -color.b = 1.0000 -color.a = 1.0000 :FurnitureComponent furniture_type = 0 taken = false +:SpriteRenderComponent sprite.sheet = "./assets/images/spritesheet.png" sprite.pos.x = 32.0000 sprite.pos.y = 32.0000 @@ -313,15 +254,10 @@ pos.x = 584.0000 pos.y = 256.0000 size.x = 16.0000 size.y = 32.0000 -:RenderComponent -layer = 0 -color.r = 1.0000 -color.g = 1.0000 -color.b = 1.0000 -color.a = 1.0000 :FurnitureComponent furniture_type = 0 taken = false +:SpriteRenderComponent sprite.sheet = "./assets/images/spritesheet.png" sprite.pos.x = 48.0000 sprite.pos.y = 32.0000 @@ -339,15 +275,10 @@ pos.x = 560.0000 pos.y = 256.0000 size.x = 32.0000 size.y = 32.0000 -:RenderComponent -layer = 0 -color.r = 1.0000 -color.g = 1.0000 -color.b = 1.0000 -color.a = 1.0000 :FurnitureComponent furniture_type = 1 taken = false +:SpriteRenderComponent sprite.sheet = "./assets/images/spritesheet.png" sprite.pos.x = 48.0000 sprite.pos.y = 32.0000 @@ -365,15 +296,10 @@ pos.x = 536.0000 pos.y = 336.0000 size.x = 16.0000 size.y = 32.0000 -:RenderComponent -layer = 0 -color.r = 1.0000 -color.g = 1.0000 -color.b = 1.0000 -color.a = 1.0000 :FurnitureComponent furniture_type = 0 taken = false +:SpriteRenderComponent sprite.sheet = "./assets/images/spritesheet.png" sprite.pos.x = 32.0000 sprite.pos.y = 32.0000 @@ -391,15 +317,10 @@ pos.x = 584.0000 pos.y = 336.0000 size.x = 16.0000 size.y = 32.0000 -:RenderComponent -layer = 0 -color.r = 1.0000 -color.g = 1.0000 -color.b = 1.0000 -color.a = 1.0000 :FurnitureComponent furniture_type = 0 taken = false +:SpriteRenderComponent sprite.sheet = "./assets/images/spritesheet.png" sprite.pos.x = 48.0000 sprite.pos.y = 32.0000 @@ -417,15 +338,10 @@ pos.x = 560.0000 pos.y = 336.0000 size.x = 32.0000 size.y = 32.0000 -:RenderComponent -layer = 0 -color.r = 1.0000 -color.g = 1.0000 -color.b = 1.0000 -color.a = 1.0000 :FurnitureComponent furniture_type = 1 taken = false +:SpriteRenderComponent sprite.sheet = "./assets/images/spritesheet.png" sprite.pos.x = 48.0000 sprite.pos.y = 32.0000 @@ -485,8 +401,18 @@ pos.x = 240.0000 pos.y = 32.0000 size.x = 32.0000 size.y = 32.0000 +:SpriteRenderComponent +sprite.sheet = "./assets/images/spritesheet.png" +sprite.pos.x = 0.0000 +sprite.pos.y = 32.0000 +sprite.size.x = 32.0000 +sprite.size.y = 32.0000 +sprite.color.r = 1.0000 +sprite.color.g = 1.0000 +sprite.color.b = 1.0000 +sprite.color.a = 1.0000 :RenderComponent -layer = 1 +layer = 0 color.r = 1.0000 color.g = 1.0000 color.b = 1.0000 @@ -495,3 +421,29 @@ color.a = 1.0000 item = "beer" max_timeout = 2.0000 +[Player] +id = 12 +flags = 2 +pos.x = 312.4881 +pos.y = 271.0974 +size.x = 16.0000 +size.y = 32.0000 +:MovementComponent +controls.up = 87 +controls.down = 83 +controls.left = 65 +controls.right = 68 +controls.interact = 70 +controls.pick_up = 71 +facing = 3 +velocity.x = -0.0000 +velocity.y = 0.0000 +:PlayerComponent +holding = 0 +:RenderComponent +layer = 10 +color.r = 0.0000 +color.g = 1.0000 +color.b = 0.0000 +color.a = 1.0000 + diff --git a/src/entity/components/dispenser.onyx b/src/entity/components/dispenser.onyx index 512e7d8..eb73ec1 100644 --- a/src/entity/components/dispenser.onyx +++ b/src/entity/components/dispenser.onyx @@ -22,7 +22,7 @@ DispenserComponent :: struct { debug_log(.Error, "DispenserComponent expected entity to not have an InteractableComponent"); } - scene->create_and_add(entity, InteractableComponent) { + scene->modify_component(entity, InteractableComponent) { comp.interact = interact; } } diff --git a/src/entity/components/furniture.onyx b/src/entity/components/furniture.onyx index 07611d8..0ddd688 100644 --- a/src/entity/components/furniture.onyx +++ b/src/entity/components/furniture.onyx @@ -11,13 +11,6 @@ FurnitureComponent :: struct { furniture_type: Furniture_Type; taken := false; - sprite: Sprite; - - init :: (use this: ^FurnitureComponent) { - sprite.pos = .{0, 32}; - sprite.size = .{32, 32}; - sprite.color = .{1, 1, 1}; - } try_take :: (use this: ^FurnitureComponent) -> bool { if taken do return false; diff --git a/src/entity/components/patron.onyx b/src/entity/components/patron.onyx index 87b4d82..bb94c8d 100644 --- a/src/entity/components/patron.onyx +++ b/src/entity/components/patron.onyx @@ -26,7 +26,7 @@ PatronComponent :: struct { added :: (use this: ^PatronComponent, entity: ^Entity) { entity.flags |= .Interactable; - scene->create_and_add(entity, InteractableComponent) { + scene->modify_component(entity, InteractableComponent) { comp.interact = interact; } } diff --git a/src/entity/entities.onyx b/src/entity/entities.onyx index f5c5234..8e91e0b 100644 --- a/src/entity/entities.onyx +++ b/src/entity/entities.onyx @@ -20,7 +20,7 @@ wall_create :: (scene: ^Scene, pos, size: Vector2) -> ^Entity { this.size = size; this.flags |= .Solid; - scene->create_and_add(this, RenderComponent) { + scene->modify_component(this, RenderComponent) { comp.func = Wall.render; } @@ -80,10 +80,10 @@ door_create :: (scene: ^Scene, pos, size: Vector2) -> ^Entity { this.flags |= .Interactable; this.flags |= .Solid; - scene->create_and_add(this, DoorComponent) {} - scene->create_and_add(this, RenderComponent) { comp.func = Door.render; } - scene->create_and_add(this, SizeComponent) { comp.func = Door.get_rect; } - scene->create_and_add(this, InteractableComponent) { comp.interact = Door.interact; } + scene->modify_component(this, DoorComponent) {} + scene->modify_component(this, RenderComponent) { comp.func = Door.render; } + scene->modify_component(this, SizeComponent) { comp.func = Door.get_rect; } + scene->modify_component(this, InteractableComponent) { comp.interact = Door.interact; } return this; } diff --git a/src/entity/items.onyx b/src/entity/items.onyx index 12d3d08..eebbf0a 100644 --- a/src/entity/items.onyx +++ b/src/entity/items.onyx @@ -97,8 +97,8 @@ item_store_get_item :: (use this: ^Item_Store, id: str) -> ^Item { this.size = .{0, 0}; this.flags |= .Carryable; - scene->create_and_add(this, RenderComponent) { comp.func = render; comp.layer = 10; } - scene->create_and_add(this, ItemComponent) {} + scene->modify_component(this, RenderComponent) { comp.func = render; comp.layer = 10; } + scene->modify_component(this, ItemComponent) {} return this; } diff --git a/src/entity/scene.onyx b/src/entity/scene.onyx index 968afba..7ee3beb 100644 --- a/src/entity/scene.onyx +++ b/src/entity/scene.onyx @@ -64,6 +64,22 @@ RenderComponent :: struct { color := Color.{1, 1, 1}; } +SpriteRenderComponent :: struct { + use base: Component; + sprite: Sprite; + + added :: (use comp: ^SpriteRenderComponent, entity: ^Entity) { + scene->modify_component(entity, RenderComponent) { + comp.func = SpriteRenderComponent.render; + } + } + + render :: (entity: ^Entity) { + sprite := entity->get(SpriteRenderComponent); + sprite.sprite->render(entity->get_rect()); + } +} + SizeComponent :: struct { use base: Component; func : (e: ^Entity) -> Rect; @@ -148,7 +164,7 @@ Scene :: struct { query_by_component :: scene_query_by_component; create_component :: scene_create_component; - create_and_add :: scene_create_and_add; + modify_component :: scene_modify_component; create_from_schematic :: scene_create_from_schematic; @@ -255,16 +271,19 @@ scene_create_component :: (use this: ^Scene, component_type: type_expr) -> ^Comp return comp; } -scene_create_and_add :: macro (this: ^Scene, entity: ^Entity, $component_type: type_expr, init_block: Code) where IsComponent(^component_type) { +scene_modify_component :: macro (this: ^Scene, entity: ^Entity, $component_type: type_expr, init_block: Code) where IsComponent(^component_type) { use package core.intrinsics.onyx {__initialize} - comp := cast(^component_type) this->create_component(component_type); + comp: ^component_type; + if !entity->has(component_type) { + comp = cast(^component_type) this->create_component(component_type); - // The 'create_component' function does not initailize the component, as it is not - // a compile time known parameter and therefore cannot know how to initialize it. - // This 'create_and_add' function is more often used by the programmer, so initializing - // the component here before returning is a good idea. - __initialize(comp); + // The 'create_component' function does not initailize the component, as it is not + // a compile time known parameter and therefore cannot know how to initialize it. + // This 'modify_component' function is more often used by the programmer, so initializing + // the component here before returning is a good idea. + __initialize(comp); + } #insert init_block; entity->add(comp); diff --git a/src/entity/schematics/background.onyx b/src/entity/schematics/background.onyx index ca5a7db..7aefc89 100644 --- a/src/entity/schematics/background.onyx +++ b/src/entity/schematics/background.onyx @@ -8,7 +8,7 @@ Background :: struct { this.pos = .{0, 0}; this.size = .{0, 0}; - scene->create_and_add(this, RenderComponent) { + scene->modify_component(this, RenderComponent) { comp.func = Background.render; } diff --git a/src/entity/schematics/furniture.onyx b/src/entity/schematics/furniture.onyx index c21ca84..1a7af35 100644 --- a/src/entity/schematics/furniture.onyx +++ b/src/entity/schematics/furniture.onyx @@ -12,17 +12,15 @@ Furniture :: struct { this.size = .{16, 16}; this.flags |= .Solid; - scene->create_and_add(this, RenderComponent) { - comp.func = render; - } + scene->modify_component(this, FurnitureComponent) {} - scene->create_and_add(this, FurnitureComponent) {} + scene->modify_component(this, SpriteRenderComponent) { + comp.sprite.sheet = "./assets/images/spritesheet.png"; + comp.sprite.pos = .{0, 32}; + comp.sprite.size = .{32, 32}; + comp.sprite.color = .{1, 1, 1}; + } return this; } - - render :: (entity: ^Entity) { - furniture := entity->get(FurnitureComponent); - furniture.sprite->render(entity->get_rect()); - } } \ No newline at end of file diff --git a/src/entity/schematics/patron.onyx b/src/entity/schematics/patron.onyx index 1281629..3a91d6a 100644 --- a/src/entity/schematics/patron.onyx +++ b/src/entity/schematics/patron.onyx @@ -12,13 +12,13 @@ Patron :: struct { this.size = .{32, 32}; this.flags |= .Solid; - scene->create_and_add(this, RenderComponent) { + scene->modify_component(this, RenderComponent) { comp.func = render; comp.layer = 5; comp.color = .{1, 0, 0}; } - scene->create_and_add(this, PatronComponent) {} + scene->modify_component(this, PatronComponent) {} return this; } diff --git a/src/entity/schematics/player.onyx b/src/entity/schematics/player.onyx index d5ea2d5..2e17526 100644 --- a/src/entity/schematics/player.onyx +++ b/src/entity/schematics/player.onyx @@ -41,19 +41,19 @@ Player :: struct { this.size = .{32, 32}; this.flags |= .Solid; - scene->create_and_add(this, SizeComponent) { + scene->modify_component(this, SizeComponent) { comp.func = (use this: ^Entity) => Rect.{pos.x - size.x / 2, pos.y, size.x, size.y / 2}; } - scene->create_and_add(this, MovementComponent) { + scene->modify_component(this, MovementComponent) { comp.controls = controls; } - scene->create_and_add(this, PlayerComponent) { + scene->modify_component(this, PlayerComponent) { comp.holding = Entity_Nothing; } - scene->create_and_add(this, RenderComponent) { + scene->modify_component(this, RenderComponent) { comp.func = PlayerComponent.render; comp.color = .{1,1,1}; } diff --git a/src/entity/schematics/tap.onyx b/src/entity/schematics/tap.onyx index 8db88e8..ca956e6 100644 --- a/src/entity/schematics/tap.onyx +++ b/src/entity/schematics/tap.onyx @@ -11,21 +11,18 @@ Tap :: struct { this.flags |= .Solid; this.flags |= .Interactable; - scene->create_and_add(this, RenderComponent) { - comp.func = render; + scene->modify_component(this, SpriteRenderComponent) { + comp.sprite.sheet = "./assets/images/spritesheet.png"; + comp.sprite.pos = .{ 0, 32 }; + comp.sprite.size = .{ 32, 32 }; + comp.sprite.color = .{ 1, 1, 1 }; } - scene->create_and_add(this, DispenserComponent) { + scene->modify_component(this, DispenserComponent) { comp.item = "beer"; } return this; } - - render :: (use this: ^Entity) { - rect := this->get_rect(); - - immediate_subimage(^Spritesheet, rect.x, rect.y, rect.w, rect.h, 0*16, 2*16, 32, 32); - } }