语言基础
CSL 基本类型、枚举、结构体、类、引用和多态。
最后更新于
pos: v2 = {10, 20};
color: v4 = {1, 0, 0, 1};Game_Phase :: enum {
LOBBY;
RUNNING;
POST_GAME;
}
phase := Game_Phase.LOBBY;
if phase == .RUNNING {
// ...
}Damage_Desc :: struct {
amount: int;
knockback: v2;
}
hit := Damage_Desc{amount=10, knockback={2, 1}};Base_Stats :: struct {
health: int;
}
Enemy_Stats :: struct : Base_Stats {
speed: float;
}
stats: Enemy_Stats = {100, 4.5};Enemy :: class {
health: int = 50;
target: v2 = {0, 0};
}
e := new(Enemy);Weapon_Definition :: class {
id: string;
name: string;
}
Gun_Definition :: class : Weapon_Definition {
fire_rate: float;
}Weapon_Definition :: class {
damage: int = 10;
}
Rocket_Definition :: class : Weapon_Definition {
damage = 100;
splash_radius: float = 3.0;
}spawn_points: [4]v2 = {{0,0}, {5,0}, {0,5}, {5,5}};
alive_enemy_ids: [..]int;
alive_enemy_ids.append(123);
alive_enemy_ids.append(456);apply_damage :: proc(health: ref int, amount: int) {
health -= amount;
}
hp := 100;
apply_damage(ref hp, 10);draw_panel :: proc(rect: ref Rect) {
header := rect.cut_top(50);
draw_header(header);
// rect 现在是内容的剩余区域
}f := 12.7;
i := f.(int); // 12
f2 := cast(float)i; // 12.0min :: proc(a: $T, b: T) -> T {
if a < b return a;
return b;
}
a := min(10, 20); // T == int
b := min(1.25, 3.5); // T == floatdefault_of :: proc($T: typeid) -> T {
t: T;
return t;
}
zero := default_of(int);
empty := default_of(string);