add shooting
This commit is contained in:
parent
052c3ad624
commit
5c2b60b6e2
48
src/game.zig
48
src/game.zig
@ -33,6 +33,13 @@ const Player = struct {
|
||||
pos: Vec2 = .zero,
|
||||
vel: Vec2 = .zero,
|
||||
acc: Vec2 = .zero,
|
||||
last_shot_at: ?Nanoseconds = null
|
||||
};
|
||||
|
||||
const Bullet = struct {
|
||||
pos: Vec2 = .zero,
|
||||
vel: Vec2 = .zero,
|
||||
size: f32 = 5
|
||||
};
|
||||
|
||||
arena: std.heap.ArenaAllocator,
|
||||
@ -41,6 +48,7 @@ rng: RNGState,
|
||||
assets: *Assets,
|
||||
|
||||
player: Player = .{},
|
||||
bullets: std.ArrayList(Bullet) = .empty,
|
||||
|
||||
pub fn init(gpa: Allocator, seed: u64, assets: *Assets) !Game {
|
||||
var arena = std.heap.ArenaAllocator.init(gpa);
|
||||
@ -59,6 +67,7 @@ pub fn init(gpa: Allocator, seed: u64, assets: *Assets) !Game {
|
||||
|
||||
pub fn deinit(self: *Game) void {
|
||||
self.arena.deinit();
|
||||
self.bullets.deinit(self.gpa);
|
||||
}
|
||||
|
||||
pub fn tick(self: *Game, frame: *Engine.Frame) !void {
|
||||
@ -106,6 +115,33 @@ pub fn tick(self: *Game, frame: *Engine.Frame) !void {
|
||||
self.player.vel = self.player.vel.multiplyScalar(friction_force);
|
||||
self.player.pos = self.player.pos.add(self.player.vel.multiplyScalar(dt));
|
||||
|
||||
if (frame.input.mouse_position) |mouse| {
|
||||
const cursor_size = Vec2.init(10, 10);
|
||||
frame.drawRectangle(.{
|
||||
.rect = .{
|
||||
.pos = mouse.sub(cursor_size.divideScalar(2)),
|
||||
.size = cursor_size,
|
||||
},
|
||||
.color = rgba(255, 200, 200, 0.5)
|
||||
});
|
||||
|
||||
const bullet_dir = mouse.sub(self.player.pos).normalized();
|
||||
|
||||
var cooldown_complete = true;
|
||||
if (self.player.last_shot_at) |last_shot_at| {
|
||||
const cooldown_ns = std.time.ns_per_ms * 500;
|
||||
cooldown_complete = frame.time_ns > last_shot_at + cooldown_ns;
|
||||
}
|
||||
|
||||
if (frame.isMouseDown(.left) and cooldown_complete) {
|
||||
self.player.last_shot_at = frame.time_ns;
|
||||
try self.bullets.append(self.gpa, .{
|
||||
.pos = self.player.pos,
|
||||
.vel = bullet_dir.multiplyScalar(50)
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
var size = self.assets.players_tilemap.tile_size;
|
||||
frame.drawRectangle(.{
|
||||
.rect = .{
|
||||
@ -114,6 +150,18 @@ pub fn tick(self: *Game, frame: *Engine.Frame) !void {
|
||||
},
|
||||
.color = rgb(255, 255, 255)
|
||||
});
|
||||
|
||||
for (self.bullets.items) |*bullet| {
|
||||
bullet.pos = bullet.pos.add(bullet.vel.multiplyScalar(dt));
|
||||
|
||||
frame.drawRectangle(.{
|
||||
.rect = .{
|
||||
.pos = bullet.pos.sub(.init(bullet.size, bullet.size)),
|
||||
.size = .init(bullet.size, bullet.size),
|
||||
},
|
||||
.color = rgb(200, 20, 255)
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
pub fn debug(self: *Game) !void {
|
||||
|
||||
Loading…
Reference in New Issue
Block a user