Working with GameObjects
Game objects are the fundamental building blocks of your game world. Each object can have properties, components, and participate in the game's render and update cycles.
Creating a GameObject
A GameObject is defined by its name, position, and size. The constructor takes these three parameters:
csharp var player = new GameObject("Player", new Vector2(100, 100), new Vector2(50, 50));
The constructor parameters are:
- name - A descriptive string identifier for the object
- position - The initial position (X, Y coordinates) as a Vector2
- size - The dimensions (width, height) as a Vector2
GameObject Properties
Each GameObject has several built-in properties you can configure:
• Name - string
• Position - Vector2
• Size - Vector2
• Visible - bool
• CanCollide - bool
• Layer - int
Adding Components
Components add behavior and rendering capabilities to your GameObject. The engine uses a component-based architecture where you attach specific components to give your objects abilities.
Common Component Types
// Transform - handles position and rotation
player.AddComponent(new TransformComponent());
// Physics - handles collision detection and resolution
player.AddComponent(new PhysicsComponent());
// Renderer - handles visual appearance
player.AddComponent(new RendererComponent());
Adding a Component
// Create and add a component
var spriteRenderer = new RendererComponent();
spriteRenderer.Texture = new Texture("player.png"); // Apply a texture
player.AddComponent(spriteRenderer);
Removing a Component
When a component is no longer needed, remove it from the object:
player.RemoveComponent(oldTransformComponent);
Adding GameObjects to the Workspace
The Workspace is the container for all your game objects. It handles: - Storing GameObject instances - Sorting objects by layer for correct rendering order - Managing the main camera viewport - Updating and rendering all objects each frame
Basic Addition
var core = new Core(800, 600, "Game");
var workspace = core.Workspace;
// Add a GameObject to the workspace
workspace.Add(new GameObject("Enemy", new Vector2(500, 300), new Vector2(40, 40)));
Texturing Your Objects
Textures give your game objects visual appearance. A Texture is typically added through a RendererComponent.
Basic Texturing
// Create a GameObject with a texture
var enemy = new GameObject("Enemy", new Vector2(500, 300), new Vector2(40, 40));
// Create a renderer component with texture
var renderer = new RendererComponent();
renderer.Texture = new Texture("enemy.png");
renderer.Visible = true;
// Add the renderer to the enemy
enemy.AddComponent(renderer);
Texture Properties
When you set a texture on a renderer, you can typically configure:
- The image file path
- Whether to stretch or preserve aspect ratio
- Whether to show the texture
- Filter settings for image quality
renderer.Texture = new Texture("player.png");
renderer.Texture.Stretch = true; // Stretch texture to fit the object
renderer.Texture.Visible = true; // Only render if texture is visible
Sprite Batching
For better performance, objects with the same texture can be batched together during rendering. The workspace handles this optimization automatically.
Layering and Rendering Order
Objects are rendered in order of their Layer property (lowest to highest). This is crucial for depth rendering:
// Background objects (render first)
var background = new GameObject("Background", new Vector2(0, 0), new Vector2(800, 600));
background.Layer = 0;
background.Visible = true;
// Midground objects
var midground = new GameObject("Midground", new Vector2(0, 0), new Vector2(800, 600));
midground.Layer = 1;
midground.Visible = true;
// Foreground objects (render last)
var foreground = new GameObject("Foreground", new Vector2(0, 0), new Vector2(800, 600));
foreground.Layer = 2;
foreground.Visible = true;
Best Practices
- Always make objects visible before adding them - Invisible objects still take memory
- Use appropriate layers - Keep rendering order logical for your game
- Name your objects clearly - Makes debugging and scene management easier
- Remove objects when done - Use
workspace.Remove()to clean up destroyed objects - Group related components - Keep your GameObject's component collection organized
Example: Creating a Complete Enemy
var enemy = new GameObject("Enemy", new Vector2(500, 300), new Vector2(40, 40));
enemy.Visible = true;
enemy.CanCollide = true;
enemy.Layer = 2;
// Add physics for collision detection
var physics = new PhysicsComponent();
enemy.AddComponent(physics);
// Add renderer with texture
var renderer = new RendererComponent();
renderer.Texture = new Texture("enemy_sprite.png");
renderer.Stretch = true;
renderer.Visible = true;
enemy.AddComponent(renderer);
// Add to workspace
workspace.Add(enemy);
TIP: Remember to call
workspace.Remove(enemy)when destroying an enemy to free up resources!