2024-10-07 20:15:20 +00:00
|
|
|
#include "twn_game_api.h"
|
|
|
|
#include "state.h"
|
|
|
|
|
|
|
|
#include <complex.h>
|
|
|
|
#include <malloc.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
2024-10-07 22:20:42 +00:00
|
|
|
#define GRAY ((Color){130, 130, 130, 255})
|
|
|
|
#define BLACK ((Color){0, 0, 0, 255})
|
2024-10-07 20:15:20 +00:00
|
|
|
|
|
|
|
#define LEFT_CLICK_ADD 20
|
|
|
|
#define RIGHT_CLICK_ADD 500
|
|
|
|
|
|
|
|
|
2024-10-07 22:20:42 +00:00
|
|
|
void handle_input(void)
|
|
|
|
{
|
2024-10-07 20:15:20 +00:00
|
|
|
State *state = ctx.udata;
|
2024-10-07 22:20:42 +00:00
|
|
|
if (ctx.input.mouse_state == 1 && ctx.input.mouse_window_position.y > 60)
|
|
|
|
{ // Left click
|
2024-10-07 20:15:20 +00:00
|
|
|
for (int i = 0; i < LEFT_CLICK_ADD; i++)
|
2024-10-07 22:20:42 +00:00
|
|
|
{
|
|
|
|
if (state->bunniesCount < MAX_BUNNIES)
|
2024-10-07 20:15:20 +00:00
|
|
|
{
|
2024-10-07 22:20:42 +00:00
|
|
|
state->bunnies[state->bunniesCount].position =
|
|
|
|
(Vec2){(float)ctx.input.mouse_window_position.x, (float)ctx.input.mouse_window_position.y};
|
|
|
|
state->bunnies[state->bunniesCount].speed.x = (float)(rand() % 500 - 250) / 60.0;
|
|
|
|
state->bunnies[state->bunniesCount].speed.y = (float)(rand() % 500 - 250) / 60.0;
|
|
|
|
state->bunnies[state->bunniesCount].color =
|
|
|
|
(Color){rand() % 190 + 50, rand() % 160 + 80, rand() % 140 + 100, 255};
|
|
|
|
state->bunniesCount++;
|
2024-10-07 20:15:20 +00:00
|
|
|
}
|
2024-10-07 22:20:42 +00:00
|
|
|
}
|
2024-10-07 20:15:20 +00:00
|
|
|
}
|
2024-10-07 22:20:42 +00:00
|
|
|
|
|
|
|
if (ctx.input.mouse_state == 4)
|
|
|
|
{ // Right click
|
2024-10-07 20:15:20 +00:00
|
|
|
for (int i = 0; i < RIGHT_CLICK_ADD; i++)
|
2024-10-07 22:20:42 +00:00
|
|
|
{
|
|
|
|
if (state->bunniesCount < MAX_BUNNIES)
|
2024-10-07 20:15:20 +00:00
|
|
|
{
|
2024-10-07 22:20:42 +00:00
|
|
|
state->bunnies[state->bunniesCount].position =
|
|
|
|
(Vec2){(float)ctx.input.mouse_window_position.x, (float)ctx.input.mouse_window_position.y};
|
|
|
|
state->bunnies[state->bunniesCount].speed.x = (float)(rand() % 500 - 250) / 60.0;
|
|
|
|
state->bunnies[state->bunniesCount].speed.y = (float)(rand() % 500 - 250) / 60.0;
|
|
|
|
state->bunnies[state->bunniesCount].color =
|
|
|
|
(Color){rand() % 190 + 50, rand() % 160 + 80, rand() % 140 + 100, 255};
|
|
|
|
state->bunniesCount++;
|
2024-10-07 20:15:20 +00:00
|
|
|
}
|
2024-10-07 22:20:42 +00:00
|
|
|
}
|
2024-10-07 20:15:20 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-10-07 22:20:42 +00:00
|
|
|
void game_tick(void)
|
|
|
|
{
|
2024-10-07 20:15:20 +00:00
|
|
|
static char bunny_count_text[64];
|
|
|
|
static char bunny_path[64] = "wabbit_alpha.png";
|
|
|
|
|
|
|
|
// State *state = ctx.udata;
|
2024-10-07 22:20:42 +00:00
|
|
|
if (ctx.initialization_needed)
|
|
|
|
{ // First tick, initalizing data
|
2024-10-07 20:15:20 +00:00
|
|
|
// Allocating State struct to store data there
|
2024-10-07 22:20:42 +00:00
|
|
|
if (!ctx.udata)
|
|
|
|
ctx.udata = ccalloc(1, sizeof(State));
|
2024-10-07 20:15:20 +00:00
|
|
|
((State *)ctx.udata)->bunniesCount = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
State *state = ctx.udata;
|
|
|
|
|
2024-10-07 22:20:42 +00:00
|
|
|
const double delta =
|
|
|
|
(double)(ctx.delta_time) / 1000.0; // Receiving floating point delta value (diving by 1000 based on vibe)
|
2024-10-07 20:15:20 +00:00
|
|
|
|
2024-10-07 22:20:42 +00:00
|
|
|
for (int i = 0; i < state->bunniesCount; i++)
|
|
|
|
{
|
|
|
|
state->bunnies[i].position.x += state->bunnies[i].speed.x;
|
|
|
|
state->bunnies[i].position.y += state->bunnies[i].speed.y;
|
2024-10-07 20:15:20 +00:00
|
|
|
|
2024-10-07 22:20:42 +00:00
|
|
|
if (((state->bunnies[i].position.x + BUNNY_W / 2) > ctx.window_w) ||
|
|
|
|
((state->bunnies[i].position.x + BUNNY_W / 2) < 0))
|
|
|
|
state->bunnies[i].speed.x *= -1;
|
|
|
|
if (((state->bunnies[i].position.y + BUNNY_H / 2) > ctx.window_h) ||
|
|
|
|
((state->bunnies[i].position.y + BUNNY_H / 2 - 60) < 0))
|
|
|
|
state->bunnies[i].speed.y *= -1;
|
2024-10-07 20:15:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
handle_input();
|
|
|
|
|
|
|
|
// Clear window with Gray color (set the background color this way)
|
2024-10-07 22:20:42 +00:00
|
|
|
draw_rectangle((Rect){0, 0, ctx.window_w, ctx.window_h}, GRAY);
|
|
|
|
|
|
|
|
for (int i = 0; i < state->bunniesCount; i++)
|
|
|
|
{ // Draw each bunny based on their position and color, also scale accordingly
|
|
|
|
m_sprite(m_set(path, bunny_path),
|
|
|
|
m_set(rect, ((Rect){.x = (int)state->bunnies[i].position.x,
|
|
|
|
.y = (int)state->bunnies[i].position.y,
|
|
|
|
.w = BUNNY_W * SPRITE_SCALE,
|
|
|
|
.h = BUNNY_H * SPRITE_SCALE})),
|
|
|
|
m_opt(color, (state->bunnies[i].color)), m_opt(stretch, true), );
|
2024-10-07 20:15:20 +00:00
|
|
|
}
|
|
|
|
// Formatting text to display, might want to add FPS here too
|
|
|
|
snprintf(bunny_count_text, 64, "Bunnies: %d", state->bunniesCount);
|
|
|
|
|
2024-10-07 22:20:42 +00:00
|
|
|
draw_text(bunny_count_text, (Vec2){0, 0}, 40, BLACK, "/fonts/kenney-pixel.ttf");
|
2024-10-07 20:15:20 +00:00
|
|
|
}
|
|
|
|
|
2024-10-07 22:20:42 +00:00
|
|
|
void game_end(void)
|
|
|
|
{
|
2024-10-07 20:15:20 +00:00
|
|
|
State *state = ctx.udata;
|
|
|
|
|
|
|
|
// Free state when game ends
|
|
|
|
free(state);
|
|
|
|
}
|