OOP & Klassen
Klassen, Vererbung, virtuelle Methoden, Structs, Properties und statische Klassen in CS2SX.
Unterstützte OOP-Features
| Feature | Status | Hinweis |
|---|---|---|
| Klassen mit Feldern und Methoden | ✓ | → C-Structs |
| static class | ✓ | → reine C-Funktionen |
| Vererbung (einzeln) | ✓ | SwitchApp, Control als Basis |
| abstract-Klassen | ✓ | → vtable-Infrastruktur |
| virtual / override | ✓ | → vtable-Funktionszeiger |
| Auto-Properties | ✓ | → f_-prefixed Struct-Felder |
| Properties mit Body | ✓ | → get_X() / set_X() |
| Enums mit Werten | ✓ | |
| Value-type struct | ✓ | → C Stack-Struct, kein malloc |
| Explizite Konstruktoren | ✓ | → ClassName_New(params...) |
| interface | ✗ | nicht unterstützt |
| Generics | ✗ | nicht unterstützt |
Normale Klasse
// Enemy.cs
public class Enemy
{
private int _x;
private int _y;
private int _hp;
public int X { get; set; } // Auto-Property → f_X Feld
public int Hp => _hp; // Read-only Property → get_Hp()
public void Move(int dx, int dy)
{
_x += dx;
_y += dy;
}
public bool IsAlive() => _hp > 0;
}
// Verwendung in Program.cs
var enemy = new Enemy();
enemy.X = 100;
enemy.Move(5, 0);static class
// UI.cs
public static class UI
{
public static void DrawHeader(string title, int y)
{
Graphics.FillRect(0, y, 1280, 50, Color.RGB(30, 30, 60));
Graphics.DrawText(20, y + 12, title, Color.White, 2);
}
public static void DrawButton(int x, int y, string label, bool focused)
{
var color = focused ? Color.Cyan : Color.Gray;
Graphics.DrawRoundedRect(x, y, 200, 40, 8, color);
Graphics.DrawText(x + 10, y + 10, label, Color.White, 1);
}
}
// Aufruf → UI_DrawHeader("Menü", 10)
UI.DrawHeader("Menü", 10);Vererbung & virtual
// Entity.cs
public abstract class Entity
{
public abstract void Update();
public virtual void Draw()
{
Graphics.DrawText(10, 10, "Entity", Color.White, 1);
}
}
// Player.cs
public class Player : Entity
{
private int _x = 640, _y = 360;
public override void Update()
{
StickPos s = Input.GetStickLeft();
_x += s.x / 8000;
_y -= s.y / 8000;
}
public override void Draw()
{
Graphics.FillCircle(_x, _y, 20, Color.Cyan);
}
}Value-type struct
// Vec2.cs
public struct Vec2
{
public float X;
public float Y;
}
// Verwendung — kein malloc, Stack-Allokation
Vec2 pos = new Vec2 { X = 100f, Y = 200f };
Vec2 vel = new Vec2 { X = 2.5f, Y = 0f };
pos.X += vel.X;
pos.Y += vel.Y;
// Array von Structs
Vec2[] points = new Vec2[10];Expliziter Konstruktor
// ColorPreset.cs
public class ColorPreset
{
public uint Background { get; set; }
public uint Foreground { get; set; }
public uint Accent { get; set; }
public ColorPreset(uint bg, uint fg, uint accent)
{
Background = bg;
Foreground = fg;
Accent = accent;
}
}
// Aufruf → ColorPreset_New(COLOR_GRAY, COLOR_WHITE, COLOR_CYAN)
var theme = new ColorPreset(Color.Gray, Color.White, Color.Cyan);const-Felder als case-Labels
public class Grid
{
private const int EMPTY = 0;
private const int WALL = 1;
private const int FOOD = 2;
public void Draw(int cell, int x, int y)
{
switch (cell)
{
case WALL: Graphics.SetPixel(x, y, Color.Gray); break;
case FOOD: Graphics.SetPixel(x, y, Color.Green); break;
case EMPTY: break;
}
}
}