#ifdef FRAGMENT_SHADER
+uniform vec4 color;
out vec4 fragColor;
void main() {
- fragColor = vec4(v_col.xyz, 1);
+ fragColor = vec4(v_col.xyz, 1) * color;
}
#endif
world: ^World;
selected_block: Vector3i;
-Player_Vertex :: struct {
- pos: Vector3;
- col: Color;
-}
-player_mesh: ^Mesh(Player_Vertex);
player_shader: Shader;
Game_State :: struct {
player = player_make();
player.camera = ^camera;
- player_mesh = mesh_make(Player_Vertex.[
- .{ .{-1, -1, -1}, .{1, 0, 0, 1}, },
- .{ .{ 1, -1, -1}, .{1, 0, 0, 1}, },
- .{ .{ 1, 1, -1}, .{1, 0, 0, 1}, },
- .{ .{-1, -1, -1}, .{1, 0, 0, 1}, },
- .{ .{ 1, 1, -1}, .{1, 0, 0, 1}, },
- .{ .{-1, 1, -1}, .{1, 0, 0, 1}, },
-
- .{ .{-1, -1, -1}, .{1, 0, 0, 1}, },
- .{ .{-1, 1, -1}, .{1, 0, 0, 1}, },
- .{ .{-1, 1, 1}, .{1, 0, 0, 1}, },
- .{ .{-1, -1, -1}, .{1, 0, 0, 1}, },
- .{ .{-1, 1, 1}, .{1, 0, 0, 1}, },
- .{ .{-1, -1, 1}, .{1, 0, 0, 1}, },
-
- .{ .{-1, -1, -1}, .{1, 0, 0, 1}, },
- .{ .{-1, -1, 1}, .{1, 0, 0, 1}, },
- .{ .{ 1, -1, 1}, .{1, 0, 0, 1}, },
- .{ .{-1, -1, -1}, .{1, 0, 0, 1}, },
- .{ .{ 1, -1, 1}, .{1, 0, 0, 1}, },
- .{ .{ 1, -1, -1}, .{1, 0, 0, 1}, },
- ], .[]);
+ Meshes.init();
}
game_update :: (_: rawptr, dt: f32) {
chunk_highlight_block(~~selected_block.x, ~~selected_block.y, ~~selected_block.z);
shader_use(player_shader);
+ shader_set_uniform(player_shader, #cstr "color", Color.{1,0,0});
for^ other_players.entries {
- update_model_matrix(it.value.position);
- mesh_draw(player_mesh);
+ update_model_matrix(it.value.position - .{0,0.8,0}, .{0.5, 1, 0.5});
+ mesh_draw(Meshes.white_box);
}
}
}
+//
+// Common meshes
+//
+Basic_Vertex :: struct {
+ position: Vector3;
+ color: Color;
+}
+Meshes :: struct {
+ #persist white_box: ^Mesh(Basic_Vertex);
+
+ init :: () {
+ white_box = mesh_make(Basic_Vertex.[
+ .{ .{-1, -1, -1}, .{1, 1, 1, 1}, },
+ .{ .{-1, 1, -1}, .{1, 1, 1, 1}, },
+ .{ .{ 1, 1, -1}, .{1, 1, 1, 1}, },
+ .{ .{ 1, -1, -1}, .{1, 1, 1, 1}, },
+ .{ .{-1, -1, 1}, .{1, 1, 1, 1}, },
+ .{ .{-1, 1, 1}, .{1, 1, 1, 1}, },
+ .{ .{ 1, 1, 1}, .{1, 1, 1, 1}, },
+ .{ .{ 1, -1, 1}, .{1, 1, 1, 1}, },
+ ], .[
+ 0, 2, 1, 0, 3, 2,
+ 3, 6, 2, 3, 7, 6,
+ 5, 0, 1, 5, 4, 0,
+ 4, 5, 6, 4, 6, 7,
+ 5, 1, 2, 5, 2, 6,
+ 0, 4, 7, 0, 7, 3,
+ ]);
+ }
+}
glBindBuffer(GL_UNIFORM_BUFFER, -1);
}
-update_model_matrix :: (v: Vector3) {
+update_model_matrix :: (v: Vector3, scale := Vector3.{1,1,1}) {
model_mat: [16] f32;
- model_mat[0] = 1;
- model_mat[5] = 1;
- model_mat[10] = 1;
+ model_mat[0] = scale.x;
+ model_mat[5] = scale.y;
+ model_mat[10] = scale.z;
model_mat[12] = v.x;
model_mat[13] = v.y;
model_mat[14] = v.z;
case .Player_Joined {
joined_packet := cast(^packets.Player_Joined) packet;
- if joined_packet.player_id == player_id do return;
- buf: [1024] u8;
- chat_messages << (
- conv.format(buf, "{} joined the server!", str.{
- ~~ ^joined_packet.name_data,
- ~~ joined_packet.name_length
- })
- |> string.alloc_copy()
- );
+ name := string.alloc_copy(.{
+ ~~ ^joined_packet.name_data,
+ ~~ joined_packet.name_length
+ });
other_players[~~ joined_packet.player_id] = .{
+ name,
.{0, 0, 0}, .{0, 0, 0}, .{0, 0, 0}
};
+
+ if joined_packet.player_id != player_id {
+ buf: [1024] u8;
+ chat_messages << (conv.format(buf, "{} joined the server!", name) |> string.alloc_copy());
+ }
}
case .Connection_Rejected {
case .Chat_Message {
chat_packet := cast(^packets.Chat_Message) packet;
- chat_messages << string.alloc_copy(.{
- ~~ ^chat_packet.message_data,
- ~~ chat_packet.message_length,
- });
+
+ buf: [1024] u8;
+ msg: str;
+
+ sender := ^other_players[~~ chat_packet.sender_id];
+ if sender != null {
+ msg = conv.format(buf, "[{}] {}\n", sender.name, str.{
+ ~~ ^chat_packet.message_data,
+ ~~ chat_packet.message_length,
+ });
+ } else {
+ msg = .{
+ ~~ ^chat_packet.message_data,
+ ~~ chat_packet.message_length,
+ };
+ }
+
+ chat_messages << string.alloc_copy(msg);
}
case .Player_Moved {
moved := cast(^packets.Player_Moved) packet;
+ if moved.player_id == player_id do return;
- other_players[~~ moved.player_id] = .{
- position = moved.position,
- velocity = moved.velocity,
- facing = moved.facing,
- };
+ player := ^other_players[~~ moved.player_id];
+ player.position = moved.position;
+ player.velocity = moved.velocity;
+ player.facing = moved.facing;
}
}
}
onet.peer_send(peer, 0, p);
}
+net_send_disconnect :: () {
+ disconnect := new(packets.Disconnect);
+ disconnect.player_id = player_id;
+}
+
net_send_chat_message :: (message: str) {
msg := cast(^packets.Chat_Message) calloc(sizeof packets.Chat_Message + message.count);
msg.type = .Chat_Message;
net_send_movement_update :: (position: Vector3, velocity: Vector3, facing: Vector3) {
movement := new(packets.Player_Moved);
- movement.type = .Player_Moved;
movement.player_id = player_id;
movement.position = position;
movement.velocity = velocity;
Remote_Player :: struct {
+ name: str;
+
position: Vector3;
velocity: Vector3;
facing: Vector3;
@TEMPORARY
other_players: Map(u32, Remote_Player);
-
#local {
use package core
onet :: package onyx_net
// Send a verify connect packet
{
msg := new(packets.Verify_Connect);
- msg.type = .Verify_Connect;
msg.player_id = ~~next_player_id;
p := new(onet.Packet);
respond_with_error :: macro (reason: packets.Connection_Rejected.Reason) {
msg := new(packets.Connection_Rejected);
- msg.type = .Connection_Rejected;
msg.reason = reason;
p := new(onet.Packet);
send_player_joined :: (player_id: u16, player_name: str, dest: ^onet.Peer) {
joined, packet_size := new_with_extra(packets.Player_Joined, player_name.count);
- joined.type = .Player_Joined;
joined.player_id = player_id;
joined.name_length = ~~ player_name.count;
memory.copy(~~ ^joined.name_data, player_name.data, player_name.count);
send_chat_message :: (from: u16, msg: [] u8) {
chat_packet, packet_size := new_with_extra(packets.Chat_Message, msg.count);
- chat_packet.type = .Chat_Message;
chat_packet.sender_id = from;
chat_packet.message_length = ~~ msg.count;
memory.copy(~~ ^chat_packet.message_data, msg.data, msg.count);
loop :: () {
while true {
- for host->get_events(timeout=1000) {
+ for host->get_events(timeout=4000) {
if it.type == .Connection {
printf("Connection from {}:{}\n", net.ipv4_to_str(it.peer.addr.addr), it.peer.addr.port);
}
}
Connect :: struct #pack {
- use base: Packet_Base;
+ use base := Packet_Base.{ .Connect };
client_version: u32;
name_length: u16;
name_data: void;
}
Verify_Connect :: struct #pack {
- use base: Packet_Base;
+ use base := Packet_Base.{ .Verify_Connect };
player_id: u16;
}
Connection_Rejected :: struct #pack {
- use base: Packet_Base;
+ use base := Packet_Base.{ .Connection_Rejected };
Reason :: enum {
Server_Full :: 1;
reason: Reason;
}
+Disconnect :: struct #pack {
+ use base := Packet_Base.{ .Disconnect };
+ player_id: u16;
+}
+
Player_Joined :: struct #pack {
- use base: Packet_Base;
+ use base := Packet_Base.{ .Player_Joined };
player_id: u16;
name_length: u16;
name_data: void;
}
Player_Left :: struct #pack {
- use base: Packet_Base;
+ use base := Packet_Base.{ .Player_Left };
player_id: u16;
}
Player_Moved :: struct #pack {
- use base: Packet_Base;
+ use base := Packet_Base.{ .Player_Moved };
player_id: u16;
position: Vector3;
velocity: Vector3;
}
Chat_Message :: struct #pack {
- use base: Packet_Base;
+ use base := Packet_Base.{ .Chat_Message };
sender_id: u16; // Should this be a Player ID?
message_length: u16;
message_data: void;