added assets
This commit is contained in:
1 parent
32900a0b5a
commit
da8589a700
715 files changed
+195
-48
No files matched your search
@@ -0,0 +1,22 @@
|
||||
const rl = @import("raylib");
|
||||
const shared = @import("shared");
|
||||
const zm = @import("zmath");
|
||||
|
||||
pub const Elf = struct {
|
||||
elf: shared.entity.Elf,
|
||||
|
||||
boy: rl.Texture,
|
||||
|
||||
pub fn init() Elf {
|
||||
return .{
|
||||
.elf = .{ .hp = 10, .pos = zm.f32x4(0, 0, 0, 0) },
|
||||
.boy = rl.loadTexture("assets/tile_0000.png") catch unreachable,
|
||||
};
|
||||
}
|
||||
|
||||
pub fn draw(self: *Elf) void {
|
||||
const src = rl.Rectangle.init(0, 0, @floatFromInt(self.boy.width), @floatFromInt(self.boy.height));
|
||||
const dst = rl.Rectangle.init(100 + 100 * zm.cos(@as(f32, @floatCast(rl.getTime()))), 100 + 100 * zm.sin(@as(f32, @floatCast(rl.getTime()))), 2.0 * @as(f32, @floatFromInt(self.boy.width)), 2.0 * @as(f32, @floatFromInt(self.boy.height)));
|
||||
self.boy.drawPro(src, dst, .{ .x = 0, .y = 0 }, 0, .white);
|
||||
}
|
||||
};
|
||||
+28
-3
@@ -6,6 +6,7 @@ const rl = @import("raylib");
|
||||
const shared = @import("shared");
|
||||
|
||||
const client = @import("client.zig");
|
||||
const entity = @import("entity.zig");
|
||||
|
||||
const screen_width = 640;
|
||||
const screen_height = 480;
|
||||
@@ -25,7 +26,7 @@ pub fn main() !void {
|
||||
try init();
|
||||
try znet.init();
|
||||
defer znet.deinit();
|
||||
rl.initWindow(1280, 720, "raylib-zig [core] example - basic window");
|
||||
rl.initWindow(1280, 720, "zzz");
|
||||
defer rl.closeWindow();
|
||||
|
||||
const host = try znet.Host.init(.{
|
||||
@@ -65,6 +66,8 @@ pub fn main() !void {
|
||||
|
||||
shared.chunk.updateChunk(&the_chunk);
|
||||
|
||||
var elf = entity.Elf.init();
|
||||
|
||||
// var send_buf: [1024]u8 = undefined;
|
||||
// var writer = if (connection) |*conn| conn.writer(&send_buf) else return;
|
||||
|
||||
@@ -75,6 +78,21 @@ pub fn main() !void {
|
||||
// const line = try reader.interface().takeDelimiterExclusive('\n');
|
||||
// std.log.info("{s}", .{line});
|
||||
|
||||
// const camera = rl.Camera{ .fovy = 45, .position = .{ .x = 0, .y = 0, .z = 10 }, .projection = .perspective, .target = .{ .x = 0, .y = 0, .z = 0 }, .up = .{ .x = 0, .y = 1, .z = 0 } };
|
||||
|
||||
rl.initAudioDevice();
|
||||
|
||||
if (rl.isAudioDeviceReady()) {
|
||||
std.log.info("audio device is ready!", .{});
|
||||
} else {
|
||||
std.log.info("audio device is NOT ready!", .{});
|
||||
}
|
||||
|
||||
const music = try rl.loadMusicStream("assets/romantic-piano-431010.mp3");
|
||||
std.log.info("{}", .{rl.isMusicValid(music)});
|
||||
// rl.playMusicStream(music);
|
||||
std.log.info("is music playing? {}", .{rl.isMusicStreamPlaying(music)});
|
||||
|
||||
while (!rl.windowShouldClose()) { // Detect window close button or ESC key
|
||||
while (try host.service(0)) |event| switch (event) {
|
||||
.connect => |data| {
|
||||
@@ -101,17 +119,24 @@ pub fn main() !void {
|
||||
// TODO: Update your variables here
|
||||
//----------------------------------------------------------------------------------
|
||||
|
||||
rl.updateMusicStream(music);
|
||||
|
||||
// Draw
|
||||
//----------------------------------------------------------------------------------
|
||||
rl.clearBackground(.sky_blue);
|
||||
|
||||
rl.beginDrawing();
|
||||
defer rl.endDrawing();
|
||||
|
||||
rl.drawText("Congrats! You created your first window!", rl.getMouseX(), rl.getMouseY(), 20, .white);
|
||||
rl.drawRectangleLines(0, 0, 100, 100, .red);
|
||||
|
||||
rl.drawFPS(0, 0);
|
||||
elf.draw();
|
||||
|
||||
// rl.beginMode3D(camera);
|
||||
// rl.drawSphere(.{ .x = 0, .y = 0, .z = 0 }, 1, .red);
|
||||
// rl.endMode3D();
|
||||
|
||||
rl.endDrawing();
|
||||
|
||||
//----------------------------------------------------------------------------------
|
||||
}
|
||||
|
||||
@@ -12,7 +12,7 @@ pub fn spawn(chunk: *shared.chunk.Chunk(), comptime T: type, allocator: std.mem.
|
||||
inline for (@typeInfo(shared.chunk.Chunk()).@"struct".fields) |field| {
|
||||
if (field.type == shared.chunk.Storage(T)) {
|
||||
try @field(chunk, field.name).items.append(allocator, entity);
|
||||
return;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
+25
-25
@@ -26,6 +26,31 @@ pub fn Storage(comptime T: type) type {
|
||||
};
|
||||
}
|
||||
|
||||
pub fn Chunk() type {
|
||||
const FieldCount = entity.EntityKinds.len;
|
||||
|
||||
var fields: [FieldCount]std.builtin.Type.StructField = undefined;
|
||||
|
||||
inline for (entity.EntityKinds, 0..) |T, i| {
|
||||
fields[i] = .{
|
||||
.name = misc.short_type_name(T),
|
||||
.type = Storage(T),
|
||||
.default_value_ptr = null,
|
||||
.is_comptime = false,
|
||||
.alignment = @alignOf(Storage(T)),
|
||||
};
|
||||
}
|
||||
|
||||
return @Type(.{
|
||||
.@"struct" = .{
|
||||
.layout = .auto,
|
||||
.fields = &fields,
|
||||
.decls = &.{},
|
||||
.is_tuple = false,
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
pub fn initChunk(allocator: std.mem.Allocator) !Chunk() {
|
||||
var chunk: Chunk() = undefined;
|
||||
|
||||
@@ -53,31 +78,6 @@ pub fn deinitChunk(chunk: *Chunk(), allocator: std.mem.Allocator) void {
|
||||
}
|
||||
}
|
||||
|
||||
pub fn Chunk() type {
|
||||
const FieldCount = entity.EntityKinds.len;
|
||||
|
||||
var fields: [FieldCount]std.builtin.Type.StructField = undefined;
|
||||
|
||||
inline for (entity.EntityKinds, 0..) |T, i| {
|
||||
fields[i] = .{
|
||||
.name = misc.short_type_name(T),
|
||||
.type = Storage(T),
|
||||
.default_value_ptr = null,
|
||||
.is_comptime = false,
|
||||
.alignment = @alignOf(Storage(T)),
|
||||
};
|
||||
}
|
||||
|
||||
return @Type(.{
|
||||
.@"struct" = .{
|
||||
.layout = .auto,
|
||||
.fields = &fields,
|
||||
.decls = &.{},
|
||||
.is_tuple = false,
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
pub fn updateChunk(chunk: *Chunk()) void {
|
||||
const info = @typeInfo(Chunk());
|
||||
|
||||
|
||||
+5
-18
@@ -1,45 +1,32 @@
|
||||
const std = @import("std");
|
||||
const zm = @import("zmath");
|
||||
const protocol = @import("protocol.zig");
|
||||
const rl = @import();
|
||||
|
||||
pub const entity_id = u64;
|
||||
pub const INVALID_ENTITY_ID: entity_id = 0;
|
||||
|
||||
pub const EntityKinds = .{
|
||||
Player,
|
||||
Elf,
|
||||
Monster,
|
||||
Projectile,
|
||||
};
|
||||
|
||||
pub const Player = struct {
|
||||
pub const Elf = struct {
|
||||
id: entity_id = INVALID_ENTITY_ID,
|
||||
pos: zm.Vec,
|
||||
hp: i32,
|
||||
|
||||
boy: rl.Texture,
|
||||
|
||||
pub fn init() void {
|
||||
const boy = try rl.loadTexture("assets/nice_boy.png");
|
||||
}
|
||||
|
||||
pub fn update(self: *Player) void {
|
||||
pub fn update(self: *Elf) void {
|
||||
self.pos += zm.f32x4(1, 1, 0, 0);
|
||||
}
|
||||
|
||||
pub fn draw(self: *Player) void {
|
||||
const src = rl.Rectangle.init(0, 0, @floatFromInt(boy.width), @floatFromInt(boy.height));
|
||||
const dst = rl.Rectangle.init(100 + 100 * zm.cos(@as(f32, @floatCast(rl.getTime()))), 100 + 100 * zm.sin(@as(f32, @floatCast(rl.getTime()))), @floatFromInt(boy.width), @floatFromInt(boy.height));
|
||||
boy.drawPro(src, dst, .{ .x = 0, .y = 0 }, 0, .white);
|
||||
}
|
||||
|
||||
pub fn encode(self: Player, w: *std.Io.Writer) !void {
|
||||
pub fn encode(self: Elf, w: *std.Io.Writer) !void {
|
||||
try w.writeInt(u64, self.id, .little);
|
||||
try protocol.writeVec4(w, self.pos);
|
||||
try protocol.writeVec4(w, self.vel);
|
||||
}
|
||||
|
||||
pub fn decode(r: *std.Io.Reader) !Player {
|
||||
pub fn decode(r: *std.Io.Reader) !Elf {
|
||||
return .{
|
||||
.id = try r.readInt(u64, .little),
|
||||
.pos = try protocol.readVec4(r),
|
||||
|
||||
@@ -12,7 +12,7 @@ pub const MessageType = enum(u8) {
|
||||
pub const SpawnEntity = struct {
|
||||
id: entity.entity_id,
|
||||
kind: EntityKind,
|
||||
payload: []const u8, // serialized entity data
|
||||
payload: []const u8,
|
||||
};
|
||||
|
||||
pub fn write_message(writer: *std.Io.Writer, msg_type: MessageType, payload: []const u8) !void {
|
||||
|
||||
Reference in new issue
Block a user