Skip to content

Commit 59d3152

Browse files
Everything is now actually working and cleaner
1 parent d2db1aa commit 59d3152

File tree

8 files changed

+83
-84
lines changed

8 files changed

+83
-84
lines changed

_main

344 Bytes
Binary file not shown.

_main.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,14 +51,14 @@ int main(int argc, char *argv[])
5151
SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255);
5252
SDL_RenderClear(renderer);
5353

54-
handleInput(&event, &running, player_inputs); //TODO: fix crashing bug here
54+
handleInput(&event, player, &running, player_inputs); //TODO: fix crashing bug here
5555

5656
if (!game_state)
5757
{
5858
UpdateTimers();
5959

6060
handlePlayerMovement(&player[0].sprite, player_inputs); // TODO: Handle all players instead of just one
61-
handleCombat(&player[0], player_inputs); // TODO: Handle all players instead of one
61+
// handleCombat(&player[0], player_inputs); // TODO: Handle all players instead of one
6262
handleEnemies();
6363

6464
render_game(renderer, player);

tests/world_tests.h

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,17 +23,17 @@ int world_test(){
2323
// printf("Entity %d, indicator: %d, id: %d, sprite: %p, src: %p\n", i, arr->elements[i].indicator, arr->elements[i].id, arr->elements[i].sprite, arr->elements[i].src);
2424

2525
// Verify that the elements in the structs match
26-
assert(arr->elements[i].indicator == entity_buffer[3].indicator);
26+
// assert(arr->elements[i].indicator == entity_buffer[3].indicator);
2727

2828
assert(arr->elements[i].id == entity_buffer[3].id);
2929

30-
assert(arr->elements[i].sprite->w == entity_buffer[3].sprite->w);
30+
assert(arr->elements[i].sprite.w == entity_buffer[3].sprite.w);
3131

32-
assert(arr->elements[i].sprite->h == entity_buffer[3].sprite->h);
32+
assert(arr->elements[i].sprite.h == entity_buffer[3].sprite.h);
3333

34-
assert(arr->elements[i].sprite->x == 3.0);
34+
assert(arr->elements[i].sprite.x == 3.0);
3535

36-
assert(arr->elements[i].sprite->y == 3.0);
36+
assert(arr->elements[i].sprite.y == 3.0);
3737

3838
assert(arr->elements[i].components != NULL);
3939

tools/ECS.h

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,8 @@
77
#define BitClear(v,n) (v & ~(1 << n))
88
#define BitSet(v,n) (v | (1 << n))
99

10-
#define Item 0x1 // Move this to a place where we're likely to actuall place and check for this attribute
11-
// Or Move the other checks here
12-
1310
typedef struct entity{
14-
// unsigned int indicator : 4;
11+
unsigned int indicator : 4;
1512
unsigned int id : 8;
1613
SDL_FRect sprite;
1714
SDL_Rect *src;

tools/Player_Systems.h

Lines changed: 65 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -24,24 +24,80 @@ void UpdateTimers() {
2424
}
2525
}
2626

27-
void handleInput(SDL_Event *event, int *game_active, int *keyInput) {
27+
void handlePlayerMovement(SDL_FRect *p_sprite, int *directional_inputs) {
28+
int speed = 7;
29+
30+
if (directional_inputs[Dash] > 0 && BitCheck(cooldowns, 0) == 0) {
31+
speed = 100;
32+
cooldowns = BitSet(cooldowns, 0);
33+
}
34+
35+
p_sprite->x +=
36+
(-directional_inputs[Left] + directional_inputs[Right]) * speed;
37+
p_sprite->y += (-directional_inputs[Up] + directional_inputs[Down]) * speed;
38+
39+
// Collision
40+
if (p_sprite->x < 0)
41+
p_sprite->x = 0;
42+
if (p_sprite->x > 948 - p_sprite->w)
43+
p_sprite->x = 948 - p_sprite->w; // Screen width
44+
45+
if (p_sprite->y < 0)
46+
p_sprite->y = 0;
47+
if (p_sprite->y > 680 - p_sprite->h)
48+
p_sprite->y = 680 - p_sprite->h; // Screen width
49+
}
50+
51+
void handleCombat(entity *plr, unsigned char isAtk) {
52+
int pPoint;
53+
if (isAtk) {
54+
// if (inventory[1] > 1){
55+
int x, y;
56+
SDL_GetMouseState(&x, &y);
57+
pPoint = x + y;
58+
59+
inventory[1]--;
60+
// printf("Inventory Seeds: %i\n", inventory[0]);
61+
// }
62+
} else {
63+
pPoint = plr->sprite.x + plr->sprite.y + ((plr->sprite.w + plr->sprite.h) / 2);
64+
cooldowns = BitSet(cooldowns, 2);
65+
}
66+
67+
for (int i = 0; i < world.size; i++) {
68+
hitbox(world.elements + i, i, plr->components[1], pPoint, atk_range);
69+
}
70+
printf("HP: %d\n", (int) plr->components[0]);
71+
}
72+
73+
#define range 30
74+
void handleItems(entity *plr) {
75+
76+
int pPoint = plr->sprite.x + plr->sprite.y + ((plr->sprite.w + plr->sprite.h) / 2);
77+
for (int i = ITEM_BUFFER_LEN; i > -1 ; i -= 2) {
78+
int iPoint = item_buffer[i].sprite.x + item_buffer[i].sprite.y + ((item_buffer[i].sprite.w + item_buffer[i].sprite.h) / 2);
79+
if (abs(pPoint - iPoint) <= range){
80+
add_item(inventory, Inventory_Slots, item_buffer[i].id, item_buffer[i].amount);
81+
item_buffer[i] = item_buffer[item_buff_size--]; // TODO: check if this works as expectedt
82+
}
83+
}
84+
}
85+
86+
void handleInput(SDL_Event *event, entity * plr, int *game_active, int *keyInput) {
2887
while (SDL_PollEvent(event))
2988
if (event->type == SDL_QUIT)
3089
*game_active = 0;
3190

3291
else if (event->button.button == SDL_BUTTON_LEFT) {
33-
if (event->type == SDL_MOUSEBUTTONDOWN && !BitCheck(cooldowns, 1))
34-
handle
35-
// keyInput[Atk] = 1;
36-
else if (event->type == SDL_MOUSEBUTTONUP)
37-
// keyInput[Atk] = 0;
92+
if (event->type == SDL_MOUSEBUTTONDOWN && !BitCheck(cooldowns, 1)){
93+
handleCombat(plr, 1);
94+
handleItems(plr);
95+
}
3896
}
3997

4098
else if (event->button.button == SDL_BUTTON_RIGHT) {
4199
if (event->type == SDL_MOUSEBUTTONDOWN && BitCheck(cooldowns, 2) == 0)
42-
keyInput[Side] = 1;
43-
else if (event->type == SDL_MOUSEBUTTONUP)
44-
keyInput[Side] = 0;
100+
handleCombat(plr, 0);
45101
}
46102

47103
else if (event->type == SDL_KEYDOWN)
@@ -122,60 +178,3 @@ void handleInput(SDL_Event *event, int *game_active, int *keyInput) {
122178
// keyInput[2], keyInput[3]);
123179
}
124180

125-
void handlePlayerMovement(SDL_FRect *p_sprite, int *directional_inputs) {
126-
int speed = 7;
127-
128-
if (directional_inputs[Dash] > 0 && BitCheck(cooldowns, 0) == 0) {
129-
speed = 100;
130-
cooldowns = BitSet(cooldowns, 0);
131-
}
132-
133-
p_sprite->x +=
134-
(-directional_inputs[Left] + directional_inputs[Right]) * speed;
135-
p_sprite->y += (-directional_inputs[Up] + directional_inputs[Down]) * speed;
136-
137-
// Collision
138-
if (p_sprite->x < 0)
139-
p_sprite->x = 0;
140-
if (p_sprite->x > 948 - p_sprite->w)
141-
p_sprite->x = 948 - p_sprite->w; // Screen width
142-
143-
if (p_sprite->y < 0)
144-
p_sprite->y = 0;
145-
if (p_sprite->y > 680 - p_sprite->h)
146-
p_sprite->y = 680 - p_sprite->h; // Screen width
147-
}
148-
149-
void handleCombat(entity *plr, unsigned char isAtk) {
150-
int pPoint;
151-
if (isAtk) {
152-
// if (inventory[1] > 1){
153-
int x, y;
154-
SDL_GetMouseState(&x, &y);
155-
pPoint = x + y;
156-
157-
inventory[1]--;
158-
printf("Inventory Seeds: %i\n", inventory[0]);
159-
// }
160-
} else {
161-
pPoint = plr->sprite.x + plr->sprite.y + ((plr->sprite.w + plr->sprite.h) / 2);
162-
cooldowns = BitSet(cooldowns, 2);
163-
}
164-
165-
for (int i = 0; i < world.size; i++) {
166-
hitbox(world.elements + i, i, plr->components[1], pPoint, atk_range);
167-
}
168-
}
169-
170-
#define range 30
171-
void handleItems(entity *plr) {
172-
173-
int pPoint = plr->sprite.x + plr->sprite.y + ((plr->sprite.w + plr->sprite.h) / 2);
174-
for (int i = ITEM_BUFFER_LEN; i > -1 ; i -= 2) {
175-
int iPoint = item_buffer[i]->sprite.x + item_buffer[i]->sprite.y + ((item_buffer[i]->sprite.w + item_buffer[i]->sprite.h) / 2);
176-
if (abs(pPoint - iPoint) <= range){
177-
add_item(inventory, Inventory_Slots, item_buffer[i].id, item_buffer[i].amount);
178-
item_buffer[i] = item_buffer[item_buff_size--]; // TODO: check if this works as expectedt
179-
}
180-
}
181-
}

tools/Rendering.h

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,11 @@ void render_game(SDL_Renderer *renderer, entity plrs[3]){
4141
SDL_RenderCopyF(renderer, entity_texture_atlas, world.elements[i].src, &world.elements[i].sprite);
4242
// SDL_RenderCopyExF(renderer, entity_atlas, world.elements[i].src, world.elements[i].sprite, 0, NULL, SDL_FLIP_NONE); // Fill in the angle(0) with a value
4343

44-
//TODO: Render Items
45-
44+
// TODO: re-enable when you add items
45+
/*
46+
for (int i = 0; i < item_buff_size; i++)
47+
SDL_RenderCopyF(renderer, entity_texture_atlas, world.elements[i].src, &item_buffer[i].sprite);
48+
*/
4649

4750
for (int i = 0; i < 1; i++) // NOTE: Change 1 -> 3
4851
SDL_RenderCopyF(renderer, entity_texture_atlas, plrs[i].src, &plrs[i].sprite);

tools/Storage_System.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
#pragma once
12
#include <stdlib.h>
23
#include <stdio.h>
34

@@ -13,7 +14,7 @@ typedef struct item{
1314
unsigned char id;
1415
unsigned char amount;
1516
SDL_Rect *src;
16-
SDL_Frect spirte;
17+
SDL_FRect sprite;
1718
}item;
1819

1920

@@ -54,4 +55,4 @@ void Load_Game(){
5455
inventory[i] = (unsigned char) fgetc(fd);
5556

5657
fclose(fd);
57-
}
58+
}

tools/game_state.h

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#pragma once
22
#include "ECS.h"
3-
#include "Systems.h"
3+
#include "Storage_System.h"
44

55
#define Hub 0
66
#define Garden 1
@@ -11,10 +11,9 @@
1111
unsigned char game_state = 0;
1212
unsigned char game_location = 0;
1313

14-
1514
entity background[50] = {};
1615
item item_buffer[ITEM_BUFFER_LEN] = {};
17-
int item_buff_size = 1;
16+
int item_buff_size = 0;
1817

1918
world_array world;
2019

0 commit comments

Comments
 (0)