Begin new Asset Pipeline
This commit is contained in:
119
src/Game.zig
Normal file
119
src/Game.zig
Normal file
@@ -0,0 +1,119 @@
|
||||
const Game = @This();
|
||||
const std = @import("std");
|
||||
|
||||
const glfw = @import("zglfw");
|
||||
|
||||
const math = @import("math.zig");
|
||||
|
||||
const Materials = @import("assets/Materials.zig");
|
||||
const Swapchain = @import("engine/Swapchain.zig");
|
||||
const Iterator3 = math.Iterator3;
|
||||
const Matrix4x4 = math.Matrix4x4;
|
||||
const Quaternion = math.Quaternion;
|
||||
const Vector2 = math.Vector2;
|
||||
const Vector3 = math.Vector3;
|
||||
|
||||
allocator: std.mem.Allocator,
|
||||
swapchain: *Swapchain,
|
||||
|
||||
materials: Materials,
|
||||
|
||||
camera_position: Vector3 = .init(0, 0, 1.62),
|
||||
camera_pitch: f32 = 0,
|
||||
camera_yaw: f32 = 0,
|
||||
|
||||
input_forwards: bool = false,
|
||||
input_backwards: bool = false,
|
||||
input_left: bool = false,
|
||||
input_right: bool = false,
|
||||
|
||||
const camera_near_plane = 0.1;
|
||||
const camera_vertical_fov_deg = 90.0;
|
||||
const camera_half_vertical_fov_rad = 0.5 * camera_vertical_fov_deg * std.math.rad_per_deg;
|
||||
const camera_mouse_sensitivity = 0.0015;
|
||||
|
||||
const max_point_lights = 100;
|
||||
const max_directional_lights = 4;
|
||||
|
||||
const player_speed = 5.0;
|
||||
|
||||
pub fn init(allocator: std.mem.Allocator, swapchain: *Swapchain) !Game {
|
||||
var materials = try Materials.init(allocator);
|
||||
errdefer materials.deinit();
|
||||
|
||||
var it = materials.map.iterator();
|
||||
while (it.next()) |entry| {
|
||||
std.debug.print("Material: {s}\n", .{entry.key_ptr.*});
|
||||
std.debug.print("Value: {}\n", .{entry.value_ptr.*});
|
||||
}
|
||||
|
||||
return .{
|
||||
.allocator = allocator,
|
||||
.swapchain = swapchain,
|
||||
.materials = materials,
|
||||
};
|
||||
}
|
||||
|
||||
pub fn deinit(self: *Game) void {
|
||||
self.materials.deinit();
|
||||
self.* = undefined;
|
||||
}
|
||||
|
||||
pub fn update(self: *Game, dt: f32) void {
|
||||
var camera_d = Vector2.init(
|
||||
@as(f32, @floatFromInt(@intFromBool(self.input_right))) - @as(f32, @floatFromInt(@intFromBool(self.input_left))),
|
||||
@as(f32, @floatFromInt(@intFromBool(self.input_forwards))) - @as(f32, @floatFromInt(@intFromBool(self.input_backwards))),
|
||||
).rotate(self.camera_yaw).mulScalar(player_speed * dt);
|
||||
|
||||
self.camera_position = Vector3.add(self.camera_position, camera_d.asVector3(0));
|
||||
}
|
||||
|
||||
pub fn onKeyDown(self: *Game, key_code: glfw.Key, mods: glfw.Mods) void {
|
||||
const no_mods = @as(c_int, @bitCast(mods)) == 0;
|
||||
|
||||
if (key_code == .escape and no_mods) {
|
||||
self.swapchain.engine.mode.surface.window.setShouldClose(true);
|
||||
}
|
||||
|
||||
if (key_code == .w) {
|
||||
self.input_forwards = true;
|
||||
self.input_backwards = false;
|
||||
}
|
||||
if (key_code == .s) {
|
||||
self.input_backwards = true;
|
||||
self.input_forwards = false;
|
||||
}
|
||||
if (key_code == .a) {
|
||||
self.input_left = true;
|
||||
self.input_right = false;
|
||||
}
|
||||
if (key_code == .d) {
|
||||
self.input_right = true;
|
||||
self.input_left = false;
|
||||
}
|
||||
}
|
||||
|
||||
pub fn onKeyUp(self: *Game, key_code: glfw.Key, mods: glfw.Mods) void {
|
||||
_ = mods;
|
||||
|
||||
if (key_code == .w) {
|
||||
self.input_forwards = false;
|
||||
}
|
||||
if (key_code == .s) {
|
||||
self.input_backwards = false;
|
||||
}
|
||||
if (key_code == .a) {
|
||||
self.input_left = false;
|
||||
}
|
||||
if (key_code == .d) {
|
||||
self.input_right = false;
|
||||
}
|
||||
}
|
||||
|
||||
pub fn onMouseMove(self: *Game, dx: f32, dy: f32) void {
|
||||
self.camera_pitch -= dy * camera_mouse_sensitivity;
|
||||
self.camera_yaw -= dx * camera_mouse_sensitivity;
|
||||
|
||||
self.camera_pitch = std.math.clamp(self.camera_pitch, -0.5 * std.math.pi, 0.5 * std.math.pi);
|
||||
self.camera_yaw = @mod(self.camera_yaw, 2 * std.math.pi);
|
||||
}
|
||||
Reference in New Issue
Block a user