123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112 |
- #include "game_item.h"
- #include "sdl_helper.h"
- #include "game_math.h"
- #include <time.h>
- #include "game_map.h"
- #define SPAWN_FREQUENCY 100
- // initialise texture for items, their size, and all inactive
- void game_item_manager_init(struct game_item_manager *s) {
- srand((unsigned int) time(NULL));
- // image stats are constant
- s->tex = load_image("images/crystal.png");
- s->tex_rect.x = 0;
- s->tex_rect.y = 0;
- s->tex_rect.w = 48;
- s->tex_rect.h = 48;
- // all items have the same size
- for (int i = 0; i < ITEM_MANAGER_MAX_ITEMS; i++) {
- s->active_item[i] = 0;
- s->item[i].w = 48;
- s->item[i].h = 48;
- }
- // starting countdown
- s->new_item_countdown = SPAWN_FREQUENCY;
- }
- // add a new item at location x y, only if there is space for new items
- void game_item_manager_add_item(struct game_item_manager *s, int x, int y) {
- for (int i = 0; i < ITEM_MANAGER_MAX_ITEMS; i++) {
- if (s->active_item[i] == 0) {
- s->item[i].x = x;
- s->item[i].y = y;
- s->active_item[i] = 1;
- break;
- }
- }
- }
- void game_item_manager_update(struct game_item_manager *s) {
- for (int i = 0; i < ITEM_MANAGER_MAX_ITEMS; i++) {
- if (s->active_item[i] != 0) {
- s->item[i].y += 1;
- }
- }
- }
- // scroll all items based on given x and y
- void game_item_manager_scroll(struct game_item_manager *s, int x, int y) {
- for (int i = 0; i < ITEM_MANAGER_MAX_ITEMS; i++) {
- if (s->active_item[i] != 0) {
- s->item[i].x -= x;
- s->item[i].y -= y;
- }
- }
- s->new_item_countdown += y;
- if (s->new_item_countdown <= 0) {
- game_item_manager_add_item(s,
- game_map_platform_x(s->map, 0) +rand()
- %(game_map_platform_x(s->map,
- game_map_platform_number(s->map)-1)),
- -s->item[0].h);
- s->new_item_countdown = SPAWN_FREQUENCY;
- }
- }
- // draw all active items
- void game_item_manager_draw(struct game_item_manager *s) {
- for (int i = 0; i < ITEM_MANAGER_MAX_ITEMS; i++) {
- if (s->active_item[i] != 0) {
- SDL_RenderCopy(ren, s->tex, &s->tex_rect, &s->item[i]);
- }
- }
- }
- void game_item_manager_remove(struct game_item_manager *s, int index) { s->active_item[index] = 0; }
- /* check all items for collision
- * on success, remove item and provide effect
- */
- int game_item_manager_collide(struct game_item_manager *s, struct game_player *p) {
- // for all items
- for (int i = 0; i < ITEM_MANAGER_MAX_ITEMS; i++) {
- // item is active
- if (s->active_item[i] != 0) {
- // player touched it
- if (game_math_collide(&s->item[i], &p->rect)) {
- // remove item
- game_item_manager_remove(s, i);
- // do effect ?
- p->level++;
- // item touched
- return 1;
- }
- }
- }
- // no item touched
- return 0;
- }
|