Skip to content

Commit

Permalink
Refactor major structs in client
Browse files Browse the repository at this point in the history
  • Loading branch information
EduardoLR10 committed Aug 31, 2024
1 parent 21462d3 commit 2763611
Show file tree
Hide file tree
Showing 13 changed files with 216 additions and 205 deletions.
24 changes: 15 additions & 9 deletions client/src/client.zig
Original file line number Diff line number Diff line change
@@ -1,26 +1,32 @@
const std = @import("std");
const rl = @import("raylib");
const config = @import("config.zig");
const assets = @import("assets.zig");
const state = @import("game/state.zig");
const user = @import("menu/user.zig");
const character = @import("menu/character.zig");
const mainMenu = @import("menu/main.zig");
const connection = @import("menu/connection.zig");
const config = @import("config.zig");
const erl = @import("erlang.zig");
const game = @import("game/main.zig");
const mainMenu = @import("menu/main.zig");
const rl = @import("raylib");
const std = @import("std");
const state = @import("game/state.zig");
const user = @import("menu/user.zig");

pub fn main() anyerror!void {
rl.setConfigFlags(.{ .window_resizable = true });
rl.initWindow(@intFromFloat(config.Screen.initialWidth), @intFromFloat(config.Screen.initialHeight), "Lyceum");
defer rl.closeWindow();
rl.setTargetFPS(60);

var gameState = try state.init(config.Screen.initialWidth, config.Screen.initialHeight);
var node: erl.Node = try erl.Node.init();
var gameState = try state.init(config.Screen.initialWidth, config.Screen.initialHeight, &node);
gameState.menu = .{
.character_name = try gameState.allocator.allocSentinel(u8, config.nameSize, 0),
.character = .{
.create = .{
.name = try gameState.allocator.allocSentinel(u8, config.nameSize, 0),
},
},
.assets = try mainMenu.loadAssets(),
};
@memset(gameState.menu.character_name, 0);
@memset(gameState.menu.character.create.name, 0);

// try character.goToSpawn(&gameState);
mainMenu.spawn(&gameState);
Expand Down
2 changes: 1 addition & 1 deletion client/src/components/button.zig
Original file line number Diff line number Diff line change
Expand Up @@ -140,5 +140,5 @@ pub const Selectable = struct {

pub const SelectableGroup = struct {
selected: ?usize = null,
buttons: [config.maximumCharacters]Selectable = .{Selectable{}} ** config.maximumCharacters,
instances: [config.maximumCharacters]Selectable = .{Selectable{}} ** config.maximumCharacters,
};
5 changes: 2 additions & 3 deletions client/src/erlang.zig
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ pub const Node = struct {
fd: i32,
node_name: [nodeLength:0]u8,
cookie: [:0]const u8 = "lyceum",
handler: ?ei.erlang_pid = null,

pub fn init() !@This() {
var tempNode: Node = .{
Expand Down Expand Up @@ -63,14 +62,14 @@ pub const Node = struct {
return receiver.run(T, allocator, ec);
}

pub fn send(ec: *Node, data: anytype) Send_Error!void {
pub fn send(ec: *Node, data: anytype, handler: ?ei.erlang_pid) Send_Error!void {
var buf: ei.ei_x_buff = undefined;
// TODO: get rid of hidden allocation
try validate(error.new_with_version, ei.ei_x_new_with_version(&buf));
defer _ = ei.ei_x_free(&buf);

try sender.send_payload(&buf, data);
if (ec.handler) |*pid| {
if (handler) |pid| {
try validate(
error.reg_send_failed_to_subprocess,
ei.ei_send(ec.fd, pid, buf.buff, buf.index),
Expand Down
8 changes: 4 additions & 4 deletions client/src/game/camera.zig
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@ const config = @import("../config.zig");
const GameState = @import("../game/state.zig");

pub fn update(gameState: *GameState) void {
gameState.camera.target = gameState.character.position;
gameState.world.camera.target = gameState.world.character.position;

const cameraAngle = rm.vector3Normalize(config.angleCameraVector);

gameState.cameraDistance -= rl.getMouseWheelMove() * 2;
gameState.world.cameraDistance -= rl.getMouseWheelMove() * 2;

gameState.camera.position = rm.vector3Add(rm.vector3Scale(cameraAngle, gameState.cameraDistance), gameState.character.position);
rl.updateCamera(&gameState.camera, rl.CameraMode.camera_first_person);
gameState.world.camera.position = rm.vector3Add(rm.vector3Scale(cameraAngle, gameState.world.cameraDistance), gameState.world.character.position);
rl.updateCamera(&gameState.world.camera, rl.CameraMode.camera_first_person);
}
6 changes: 3 additions & 3 deletions client/src/game/main.zig
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ const camera = @import("../game/camera.zig");
const messages = @import("../server_messages.zig");

fn drawPlayers(gameState: *GameState) void {
for (gameState.other_players) |player| {
const maybeModel = gameState.character.model;
for (gameState.world.other_players) |player| {
const maybeModel = gameState.world.character.model;
if (maybeModel) |model| {
const position: rl.Vector3 = .{
.x = @floatFromInt(player.x_position),
Expand All @@ -20,7 +20,7 @@ fn drawPlayers(gameState: *GameState) void {
}

pub fn spawn(gameState: *GameState) !void {
rl.beginMode3D(gameState.camera);
rl.beginMode3D(gameState.world.camera);
defer rl.endMode3D();

physics.character.draw(gameState);
Expand Down
20 changes: 10 additions & 10 deletions client/src/game/physics.zig
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ pub const character = struct {
const ceilingLevel = 48;

pub fn draw(gameState: *GameState) void {
var tempAngle = gameState.character.stats.face_direction;
const velocity = &gameState.character.velocity;
var tempAngle = gameState.world.character.stats.face_direction;
const velocity = &gameState.world.character.velocity;
const deltaTime = rl.getFrameTime();
const deltaVelocity = deltaTime * acceleration;

Expand Down Expand Up @@ -71,22 +71,22 @@ pub const character = struct {

velocity.* = rm.vector3Clamp(velocity.*, rm.vector3Scale(velocityCeiling, -1), velocityCeiling);

var tempPosition: rl.Vector3 = rm.vector3Add(gameState.character.position, rm.vector3Scale(velocity.*, deltaTime));
var tempPosition: rl.Vector3 = rm.vector3Add(gameState.world.character.position, rm.vector3Scale(velocity.*, deltaTime));
tempPosition.y = rm.clamp(tempPosition.y, floorLevel, ceilingLevel);

const previous = &gameState.character.stats.face_direction;
const previous = &gameState.world.character.stats.face_direction;
if (previous.* != tempAngle) {
if (gameState.character.model) |model| {
rl.drawModelEx(model, gameState.character.position, heightAxis, @floatFromInt(previous.*), modelScale, rl.Color.white);
if (gameState.world.character.model) |model| {
rl.drawModelEx(model, gameState.world.character.position, heightAxis, @floatFromInt(previous.*), modelScale, rl.Color.white);
}
previous.* = tempAngle;
} else {
if (gameState.character.model) |model| {
if (gameState.world.character.model) |model| {
rl.drawModelEx(model, tempPosition, heightAxis, @floatFromInt(tempAngle), modelScale, rl.Color.white);
}
gameState.character.position = tempPosition;
gameState.character.stats.x_position = @intFromFloat(tempPosition.x);
gameState.character.stats.y_position = @intFromFloat(tempPosition.z);
gameState.world.character.position = tempPosition;
gameState.world.character.stats.x_position = @intFromFloat(tempPosition.x);
gameState.world.character.stats.y_position = @intFromFloat(tempPosition.z);
}
}
};
107 changes: 52 additions & 55 deletions client/src/game/protocol.zig
Original file line number Diff line number Diff line change
Expand Up @@ -5,72 +5,69 @@ const std = @import("std");
pub fn pingUpdateCharacter(gameState: *GameState) !void {
// TODO: We should a time out functionality (Zerl should provide one) to correctly assess
// if we are not overwhelming the database
if (gameState.node) |*nod| {
try nod.send(messages.Payload{
.update_character = .{
.name = gameState.character.stats.name,
.x_position = gameState.character.stats.x_position,
.y_position = gameState.character.stats.y_position,
.map_name = gameState.character.stats.map_name,
.face_direction = gameState.character.stats.face_direction,
.username = gameState.menu.login.username[0..gameState.menu.login.usernamePosition],
.email = gameState.menu.email,
},
});
const server_response = try messages.receive_characters_list(gameState.allocator, nod);
switch (server_response) {
.ok => |players| {
gameState.other_players = players;
},
.@"error" => |msg| {
defer gameState.allocator.free(msg);
std.debug.print("[ERROR]: {s}\n", .{msg});
gameState.scene = .nothing;
},
}
const node = gameState.connection.node;
try node.send(messages.Payload{
.update_character = .{
.name = gameState.world.character.stats.name,
.x_position = gameState.world.character.stats.x_position,
.y_position = gameState.world.character.stats.y_position,
.map_name = gameState.world.character.stats.map_name,
.face_direction = gameState.world.character.stats.face_direction,
.username = gameState.menu.credentials.username[0..gameState.menu.credentials.usernamePosition],
.email = gameState.menu.credentials.email,
},
}, gameState.connection.handler);
const server_response = try messages.receive_characters_list(gameState.allocator, node);
switch (server_response) {
.ok => |players| {
gameState.world.other_players = players;
},
.@"error" => |msg| {
defer gameState.allocator.free(msg);
std.debug.print("[ERROR]: {s}\n", .{msg});
gameState.scene = .nothing;
},
}
}

pub fn pingJoinMap(gameState: *GameState) !void {
// TODO: We should a time out functionality (Zerl should provide one) to correctly assess
// if we are not overwhelming the database
if (gameState.node) |*nod| {
try nod.send(messages.Payload{
.joining_map = .{
.name = gameState.character.stats.name,
.x_position = gameState.character.stats.x_position,
.y_position = gameState.character.stats.y_position,
.map_name = gameState.character.stats.map_name,
.face_direction = gameState.character.stats.face_direction,
.username = gameState.menu.login.username[0..gameState.menu.login.usernamePosition],
.email = gameState.menu.email,
},
});
const server_response = try messages.receive_standard_response(gameState.allocator, nod);
switch (server_response) {
.ok => {},
.@"error" => |msg| {
defer gameState.allocator.free(msg);
std.debug.print("[ERROR]: {s}\n", .{msg});
gameState.scene = .nothing;
},
}
const node = gameState.connection.node;
try node.send(messages.Payload{
.joining_map = .{
.name = gameState.world.character.stats.name,
.x_position = gameState.world.character.stats.x_position,
.y_position = gameState.world.character.stats.y_position,
.map_name = gameState.world.character.stats.map_name,
.face_direction = gameState.world.character.stats.face_direction,
.username = gameState.menu.credentials.username[0..gameState.menu.credentials.usernamePosition],
.email = gameState.menu.credentials.email,
},
}, gameState.connection.handler);
const server_response = try messages.receive_standard_response(gameState.allocator, node);
switch (server_response) {
.ok => {},
.@"error" => |msg| {
defer gameState.allocator.free(msg);
std.debug.print("[ERROR]: {s}\n", .{msg});
gameState.scene = .nothing;
},
}
}

pub fn pingExitMap(gameState: *GameState) !void {
// TODO: We should a time out functionality (Zerl should provide one) to correctly assess
// if we are not overwhelming the database
if (gameState.node) |*nod| {
try nod.send(messages.Payload.exit_map);
const server_response = try messages.receive_standard_response(gameState.allocator, nod);
switch (server_response) {
.ok => {},
.@"error" => |msg| {
defer gameState.allocator.free(msg);
std.debug.print("[ERROR]: {s}\n", .{msg});
gameState.scene = .nothing;
},
}
const node = gameState.connection.node;
try node.send(messages.Payload.exit_map, gameState.connection.handler);
const server_response = try messages.receive_standard_response(gameState.allocator, node);
switch (server_response) {
.ok => {},
.@"error" => |msg| {
defer gameState.allocator.free(msg);
std.debug.print("[ERROR]: {s}\n", .{msg});
gameState.scene = .nothing;
},
}
}
68 changes: 36 additions & 32 deletions client/src/game/state.zig
Original file line number Diff line number Diff line change
Expand Up @@ -18,40 +18,44 @@ pub const Scene = enum {
connect,
};

pub const Character = struct {
stats: messages.Character_Info = .{},
model: ?rl.Model = null,
// TODO: Remove this position and use spatial info from stats
position: rl.Vector3 = .{
.x = 0.0,
.y = physics.character.floorLevel,
.z = 0.0,
},
preview: ?rl.Texture2D = null,
faceDirection: f32 = 270,
velocity: rl.Vector3 = .{
.x = 0,
.y = 0,
.z = 0,
},
pub const World = struct {
pub const Character = struct {
stats: messages.Character_Info = .{},
model: ?rl.Model = null,
// TODO: Remove this position and use spatial info from stats
position: rl.Vector3 = .{
.x = 0.0,
.y = physics.character.floorLevel,
.z = 0.0,
},
preview: ?rl.Texture2D = null,
faceDirection: f32 = 270,
velocity: rl.Vector3 = .{
.x = 0,
.y = 0,
.z = 0,
},
};
character: Character = .{},
other_players: []const messages.Character_Info = &.{},
camera: rl.Camera = undefined,
cameraDistance: f32 = 60,
};

scene: Scene = .nothing,
pub const Connection = struct {
handler: ?erl.ei.erlang_pid = null,
node: *erl.Node,
is_connected: bool = false,
};
// Common
width: f32,
height: f32,
menu: mainMenu.Menu = undefined,
node: ?erl.Node = null,
allocator: std.mem.Allocator = std.heap.c_allocator,
character: Character = .{},
character_list: []const Character = &.{},
other_players: []const messages.Character_Info = &.{},
camera: rl.Camera,

// TODO: Change the name and maybe even the location of this
test_value: usize = 0,
cameraDistance: f32 = 60,
scene: Scene = .nothing,
connection: Connection,
world: World = undefined,

pub fn init(width: f32, height: f32) !@This() {
pub fn init(width: f32, height: f32, node: *erl.Node) !@This() {
const camera: rl.Camera = .{
.position = .{ .x = 50.0, .y = 50.0, .z = 50.0 },
.target = .{ .x = 0.0, .y = 10.0, .z = 0.0 },
Expand All @@ -61,9 +65,9 @@ pub fn init(width: f32, height: f32) !@This() {
};
if (width < 0) return error.negative_width;
if (height < 0) return error.negative_height;
return .{
.width = width,
.height = height,
return .{ .width = width, .height = height, .connection = .{
.node = node,
}, .world = .{
.camera = camera,
};
} };
}
Loading

0 comments on commit 2763611

Please sign in to comment.