Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "monster.h"
- #include <assert.h>
- #include <string.h>
- #include <SDL\SDL_image.h>
- #include "map.h"
- void Monster_setArray(MONSTERTYPE monsterType, const char *monsterPath)
- {
- if (strlen(monsterPath) > 0)
- {
- FILE *monsterFile = NULL;
- monsterFile = fopen(monsterPath, "r");
- if (monsterFile)
- {
- MONSTER *monster = malloc(sizeof(MONSTER));
- char spritesPath[128];
- char animPath[128];
- fscanf(monsterFile, "%s %s", spritesPath,
- animPath);
- monster_addsprite(spritesPath, monster);
- Anim_load(animPath, &monster->animation);
- monster->position.x = rand() % 1280;
- monster->position.y = rand() % 720;
- monster->frame = 0;
- memcpy(&sMonster_array[monsterType], monster, sizeof(MONSTER));
- }
- fclose(monsterFile);
- }
- }
- void Monster_addList(MONSTERTYPE monsterType)
- {
- MONSTER *monster = malloc(sizeof(MONSTER));
- memcpy(monster, &sMonster_array[monsterType], sizeof(MONSTER));
- monster->position.x = rand() % 1280;
- monster->position.y = rand() % 720;
- if (sList_monster == NULL)
- sList_monster = list_create(monster);
- else
- list_insert_end(sList_monster, monster);
- free(monster);
- }
- void Monster_freeList(void)
- {
- list_node *tmp = NULL;
- tmp = sList_monster;
- while (tmp->next != NULL)
- {
- //Monster_free(tmp->data);
- free(tmp);
- tmp = tmp->next;
- }
- }
- void Monster_free(MONSTER *monster)
- {
- assert(monster != NULL);
- for (int i = 0; i < (sizeof(monster->sprites) / sizeof(SDL_Surface*)); ++i)
- {
- SDL_FreeSurface(monster->sprites[i]);
- }
- free(monster);
- }
- void Monster_blitsWave(SDL_Surface *screen)
- {
- list_node *it = sList_monster;
- while (it ->next != NULL)
- {
- MONSTER monster;
- memcpy(&monster, it->data, sizeof(MONSTER));
- SDL_Rect rect = { .x = monster.position.x, .y = monster.position.y };
- SDL_BlitSurface(monster.sprites[monster.frame], NULL, screen, &rect);
- it = it->next;
- }
- }
- void Monster_handleMovement(MAP *map)
- {
- list_node *it = sList_monster;
- while (it->next != NULL)
- {
- MONSTER monster;
- memcpy(&monster, it->data, sizeof(MONSTER));
- Anim_play(&monster.animation, &monster.frame);
- memcpy(it->data, &monster, sizeof(MONSTER));
- it = it->next;
- }
- }
- static void monster_addsprite(const char *spritesPath, MONSTER *monster)
- {
- if (strlen(spritesPath) > 0)
- {
- FILE *spritesFile = NULL;
- spritesFile = fopen(spritesPath, "r");
- if (spritesFile)
- {
- int nSprites = 0;
- fscanf(spritesFile, "%d", &nSprites);
- monster->sprites = malloc(nSprites * sizeof(SDL_Surface));
- for (int i = 0; i < nSprites; ++i)
- {
- char path[256];
- fscanf(spritesFile, "%s", path);
- monster->sprites[i] = IMG_Load(path);
- }
- }
- fclose(spritesFile);
- }
- }
- static void monster_findPath(MAP *map)
- {
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement