forked from PixelGuys/Cubyz
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEntity.zig
More file actions
88 lines (75 loc) · 2.69 KB
/
Entity.zig
File metadata and controls
88 lines (75 loc) · 2.69 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
const std = @import("std");
const main = @import("main");
const chunk = main.chunk;
const game = main.game;
const graphics = main.graphics;
const c = graphics.c;
const ZonElement = main.ZonElement;
const renderer = main.renderer;
const settings = main.settings;
const utils = main.utils;
const vec = main.vec;
const Mat4f = vec.Mat4f;
const Vec3d = vec.Vec3d;
const Vec3f = vec.Vec3f;
const Vec4f = vec.Vec4f;
const NeverFailingAllocator = main.heap.NeverFailingAllocator;
const BinaryReader = main.utils.BinaryReader;
interpolatedValues: utils.GenericInterpolation(6) = undefined,
_interpolationPos: [6]f64 = undefined,
_interpolationVel: [6]f64 = undefined,
width: f64,
height: f64,
pos: Vec3d = undefined,
rot: Vec3f = undefined,
id: u32,
name: []const u8,
playerIndex: ?usize, // TODO extract into own component #2760
pub fn init(self: *@This(), zon: ZonElement, allocator: NeverFailingAllocator) void {
self.* = @This(){
.id = zon.get(u32, "id", std.math.maxInt(u32)),
.width = zon.get(f64, "width", 1),
.height = zon.get(f64, "height", 1),
.name = allocator.dupe(u8, zon.get([]const u8, "name", "")),
.playerIndex = zon.get(?usize, "playerIndex", null),
};
self._interpolationPos = [_]f64{
self.pos[0],
self.pos[1],
self.pos[2],
@floatCast(self.rot[0]),
@floatCast(self.rot[1]),
@floatCast(self.rot[2]),
};
self._interpolationVel = @splat(0);
self.interpolatedValues.init(&self._interpolationPos, &self._interpolationVel);
}
pub fn deinit(self: @This(), allocator: NeverFailingAllocator) void {
allocator.free(self.name);
}
pub fn getRenderPosition(self: *const @This()) Vec3d {
return Vec3d{self.pos[0], self.pos[1], self.pos[2]};
}
pub fn updatePosition(self: *@This(), pos: *const [6]f64, vel: *const [6]f64, time: i16) void {
self.interpolatedValues.updatePosition(pos, vel, time);
}
pub fn update(self: *@This(), time: i16, lastTime: i16) void {
self.interpolatedValues.update(time, lastTime);
self.pos[0] = self.interpolatedValues.outPos[0];
self.pos[1] = self.interpolatedValues.outPos[1];
self.pos[2] = self.interpolatedValues.outPos[2];
self.rot[0] = @floatCast(self.interpolatedValues.outPos[3]);
self.rot[1] = @floatCast(self.interpolatedValues.outPos[4]);
self.rot[2] = @floatCast(self.interpolatedValues.outPos[5]);
}
pub fn format(self: *const @This(), writer: *std.Io.Writer) std.Io.Writer.Error!void {
if (main.settings.showPlayerIndexWithName and self.playerIndex != null) {
try self.formatWithPlayerIndex(writer);
} else {
try writer.print("{s}", .{self.name});
}
}
pub fn formatWithPlayerIndex(self: @This(), writer: *std.Io.Writer) std.Io.Writer.Error!void {
std.debug.assert(self.playerIndex != null);
try writer.print("{s}@{d}", .{self.name, self.playerIndex.?});
}