tonk

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs

commit 7656f1ead122aa080dd6e050f650b9d31db45068
parent a6ccaab60f0a46fc93dde60df6e6250f5866834a
Author: thing1 <thing1@seacrossedlovers.xyz>
Date:   Wed, 24 Sep 2025 12:28:22 +0100

added shooting, and started work on the server!

Diffstat:
MMakefile | 9++++++---
Mtonk | 0
Mtonk.c | 54+++++++++++++++++++++++++++++++++++++-----------------
Atonk.h | 31+++++++++++++++++++++++++++++++
Atonk_srv | 0
Atonk_srv.c | 27+++++++++++++++++++++++++++
6 files changed, 101 insertions(+), 20 deletions(-)

diff --git a/Makefile b/Makefile @@ -1,10 +1,13 @@ -all: tonk +all: tonk tonk_srv -LIBS=-lraylib -lm +LIBS=-lm -lraylib -lOpenGL CFLAGS=-ggdb tonk: tonk.c cc tonk.c -o tonk ${LIBS} ${CFLAGS} + +tonk_srv: tonk_srv.c + cc tonk_srv.c -o tonk_srv ${LIBS} ${CFLAGS} clean: - rm tonk + rm tonk tonk_srv diff --git a/tonk b/tonk Binary files differ. diff --git a/tonk.c b/tonk.c @@ -6,6 +6,8 @@ #include <sys/param.h> #include <stdbool.h> +#define TANK_SPEED 100 +#define TANK_ROT_SPEED 100 #define TANK_WIDTH 20 #define TANK_HEIGHT 50 #define GUN_WIDTH 5 @@ -25,6 +27,7 @@ typedef struct shot { float speed, rot; int bounces; bool exists; + tank *shooter; } shot; void @@ -51,7 +54,7 @@ draw_shot(shot shot) { } void -update_tank(tank *t) { +update_tank(tank *t, float delta) { float dx = t->pos.x - GetMouseX(), dy = t->pos.y - GetMouseY(); float rads = atan((float)(dy / dx)); if (rads > 0 && dy < 0) rads = rads + M_PI; @@ -59,22 +62,22 @@ update_tank(tank *t) { t->gunrot = INDEG(rads); if (IsKeyDown(KEY_D)) - t->rot += 0.01; + t->rot += TANK_ROT_SPEED * delta; if (IsKeyDown(KEY_A)) - t->rot -= 0.01; + t->rot -= TANK_ROT_SPEED * delta; if (IsKeyDown(KEY_S)) { - t->pos.x += 0.01 * cos(t->rot * M_PI / 180); - t->pos.y += 0.01 * sin(t->rot * M_PI / 180); + t->pos.x += (TANK_SPEED * delta) * cos(t->rot * M_PI / 180); + t->pos.y += (TANK_SPEED * delta) * sin(t->rot * M_PI / 180); } if (IsKeyDown(KEY_W)) { - t->pos.x -= 0.01 * cos(t->rot * M_PI / 180); - t->pos.y -= 0.01 * sin(t->rot * M_PI / 180); + t->pos.x -= (TANK_SPEED * delta) * cos(t->rot * M_PI / 180); + t->pos.y -= (TANK_SPEED * delta) * sin(t->rot * M_PI / 180); } } void -update_shots(shot *shots, int shot_count) { +update_shots(shot *shots, int shot_count, float delta) { for (int i = 0; i < shot_count; i++) { shot *s = &shots[i]; @@ -104,8 +107,8 @@ update_shots(shot *shots, int shot_count) { continue; } - s->pos.x += s->speed * cos(s->rot * M_PI / 180); - s->pos.y += s->speed * sin(s->rot * M_PI / 180); + s->pos.x += (s->speed * delta) * cos(s->rot * M_PI / 180); + s->pos.y += (s->speed * delta) * sin(s->rot * M_PI / 180); while (s->rot > 360) s->rot -= 360; @@ -113,11 +116,11 @@ update_shots(shot *shots, int shot_count) { } void -append_shot(shot *shots, int shot_count, Vector2 pos, float speed, float rot, int bounces, bool exists) { +append_shot(shot *shots, int shot_count, Vector2 pos, float speed, float rot, int bounces, bool exists, tank *shooter) { shot new = { {(pos.x + GUN_HEIGHT * cos(rot * M_PI / 180)), (pos.y + GUN_HEIGHT * sin(rot * M_PI / 180))}, - speed, rot, bounces, exists}; + speed, rot, bounces, exists, shooter}; int i; for (i = 0; i < shot_count; i++) { @@ -128,12 +131,26 @@ append_shot(shot *shots, int shot_count, Vector2 pos, float speed, float rot, in memcpy(&shots[i], &new, sizeof(shot)); } +void +damage_step(tank *t, shot *shots, int shot_count) { + for (int i = 0; i < shot_count; i++) { + if (!shots[i].exists || shots[i].shooter == t) continue; + int dx = abs((t->pos.x - shots[i].pos.x)), + dy = abs((t->pos.y - shots[i].pos.y)); + float dist = sqrtf(dx * dx + dy * dy); + if (dist < TANK_HEIGHT/2) { + printf("HIT\n"); + shots[i].exists = false; + } + } +} + int main() { InitWindow(800, 800, "Tonks!"); //ToggleBorderlessWindowed(); - tank t = { + tank player = { {400, 400}, 0, 0 @@ -141,12 +158,14 @@ main() { shot *shots = alloca(MAX_SHOTS * sizeof(shot)); memset(shots, 0, MAX_SHOTS * sizeof(shot)); + float delta; while (!WindowShouldClose()) { + delta = GetFrameTime(); BeginDrawing(); ClearBackground(RAYWHITE); - draw_tank(t); + draw_tank(player); for (int i = 0; i < MAX_SHOTS; i++) { if (shots[i].exists) draw_shot(shots[i]); @@ -154,10 +173,11 @@ main() { EndDrawing(); if (IsKeyPressed(KEY_SPACE)) - append_shot(shots, MAX_SHOTS, t.pos, 0.1, t.gunrot + 180, 5, true); + append_shot(shots, MAX_SHOTS, player.pos, 800, player.gunrot + 180, 5, true, &player); - update_tank(&t); - update_shots(shots, MAX_SHOTS); + update_tank(&player, delta); + update_shots(shots, MAX_SHOTS, delta); + damage_step(&player, shots, MAX_SHOTS); } CloseWindow(); diff --git a/tonk.h b/tonk.h @@ -0,0 +1,31 @@ +#include <raylib.h> +#include <stdlib.h> +#include <stdio.h> +#include <math.h> +#include <string.h> +#include <sys/param.h> +#include <stdbool.h> + +#define TANK_SPEED 100 +#define TANK_ROT_SPEED 100 +#define TANK_WIDTH 20 +#define TANK_HEIGHT 50 +#define GUN_WIDTH 5 +#define GUN_HEIGHT 30 +#define MAX_SHOTS 100 + +#define INDEG(rad) (rad * (180 / M_PI)) +#define CLAMP(val, lower, upper) ((val < lower) ? lower : ((val > upper) ? upper: val)) + +typedef struct tank { + Vector2 pos; + float rot, gunrot; +} tank; + +typedef struct shot { + Vector2 pos; + float speed, rot; + int bounces; + bool exists; + tank *shooter; +} shot; diff --git a/tonk_srv b/tonk_srv Binary files differ. diff --git a/tonk_srv.c b/tonk_srv.c @@ -0,0 +1,27 @@ +#include <stdlib.h> +#include <sys/socket.h> +#include <arpa/inet.h> +#include <stdio.h> + +#include "tonk.h" + +#define PORT 25566 + +int +main() { + struct sockaddr_in addr = { + AF_INET, + htons(PORT), + INADDR_ANY + }; + int s = socket(AF_INET, SOCK_STREAM, 0); + if (bind(s, (struct sockaddr *)&addr, sizeof(addr))) { + perror("bind: "); + exit(1); + } + listen(s, 10); + + int p1, p2; + + close(s); +}