From de29b11a88dbdd3af0824e59b51528b91ee73c54 Mon Sep 17 00:00:00 2001 From: ptitSeb Date: Thu, 30 Nov 2017 22:49:38 +0100 Subject: First commit. Version works on Linux (keyboard only, not configurable) --- src/3ds/audio.c | 316 +++++++++ src/3ds/audio.h | 51 ++ src/3ds/graphics.c | 435 ++++++++++++ src/3ds/graphics.h | 67 ++ src/3ds/input.c | 56 ++ src/3ds/input.h | 25 + src/3ds/system.c | 48 ++ src/3ds/system.h | 12 + src/PHL.c | 83 +++ src/PHL.h | 50 ++ src/collision.c | 336 +++++++++ src/collision.h | 27 + src/effect.c | 384 ++++++++++ src/effect.h | 31 + src/enemies/bat.c | 172 +++++ src/enemies/bat.h | 16 + src/enemies/batboss.c | 319 +++++++++ src/enemies/batboss.h | 18 + src/enemies/bee.c | 214 ++++++ src/enemies/bee.h | 16 + src/enemies/boar.c | 221 ++++++ src/enemies/boar.h | 18 + src/enemies/boomknight.c | 285 ++++++++ src/enemies/boomknight.h | 27 + src/enemies/crab.c | 509 ++++++++++++++ src/enemies/crab.h | 30 + src/enemies/devil.c | 458 ++++++++++++ src/enemies/devil.h | 28 + src/enemies/dodo.c | 587 ++++++++++++++++ src/enemies/dodo.h | 22 + src/enemies/dog.c | 294 ++++++++ src/enemies/dog.h | 17 + src/enemies/firewheel.c | 272 +++++++ src/enemies/firewheel.h | 18 + src/enemies/fish.c | 139 ++++ src/enemies/fish.h | 24 + src/enemies/garm.c | 578 +++++++++++++++ src/enemies/garm.h | 31 + src/enemies/gas.c | 116 +++ src/enemies/gas.h | 18 + src/enemies/ghoul.c | 218 ++++++ src/enemies/ghoul.h | 22 + src/enemies/golem.c | 199 ++++++ src/enemies/golem.h | 16 + src/enemies/gyra.c | 332 +++++++++ src/enemies/gyra.h | 19 + src/enemies/heads.c | 838 ++++++++++++++++++++++ src/enemies/heads.h | 89 +++ src/enemies/hydra.c | 1127 +++++++++++++++++++++++++++++ src/enemies/hydra.h | 65 ++ src/enemies/jellyfish.c | 195 +++++ src/enemies/jellyfish.h | 16 + src/enemies/knight.c | 232 ++++++ src/enemies/knight.h | 23 + src/enemies/lolidra.c | 367 ++++++++++ src/enemies/lolidra.h | 40 ++ src/enemies/pendulum.c | 78 ++ src/enemies/pendulum.h | 19 + src/enemies/podoboo.c | 176 +++++ src/enemies/podoboo.h | 20 + src/enemies/poisonknight.c | 318 +++++++++ src/enemies/poisonknight.h | 26 + src/enemies/pumpkin.c | 375 ++++++++++ src/enemies/pumpkin.h | 27 + src/enemies/seal.c | 204 ++++++ src/enemies/seal.h | 14 + src/enemies/skeleton.c | 298 ++++++++ src/enemies/skeleton.h | 31 + src/enemies/skull.c | 174 +++++ src/enemies/skull.h | 15 + src/enemies/slime.c | 228 ++++++ src/enemies/slime.h | 17 + src/enemies/slug.c | 164 +++++ src/enemies/slug.h | 13 + src/enemies/thwomp.c | 265 +++++++ src/enemies/thwomp.h | 18 + src/enemies/waterjumper.c | 356 ++++++++++ src/enemies/waterjumper.h | 23 + src/enemies/wizard.c | 133 ++++ src/enemies/wizard.h | 20 + src/enemy.c | 16 + src/enemy.h | 13 + src/game.c | 1678 ++++++++++++++++++++++++++++++++++++++++++++ src/game.h | 217 ++++++ src/hero.c | 1406 +++++++++++++++++++++++++++++++++++++ src/hero.h | 55 ++ src/ini.c | 304 ++++++++ src/ini.h | 10 + src/inventory.c | 149 ++++ src/inventory.h | 9 + src/main.c | 57 ++ src/object.c | 1525 ++++++++++++++++++++++++++++++++++++++++ src/object.h | 181 +++++ src/options.c | 264 +++++++ src/options.h | 9 + src/platform.c | 223 ++++++ src/platform.h | 26 + src/psp/audio.c | 87 +++ src/psp/audio.h | 24 + src/psp/graphics.c | 249 +++++++ src/psp/graphics.h | 66 ++ src/psp/input.c | 75 ++ src/psp/input.h | 21 + src/psp/system.c | 48 ++ src/psp/system.h | 16 + src/qda.c | 62 ++ src/qda.h | 14 + src/sdl/audio.c | 70 ++ src/sdl/audio.h | 30 + src/sdl/graphics.c | 220 ++++++ src/sdl/graphics.h | 65 ++ src/sdl/input.c | 88 +++ src/sdl/input.h | 21 + src/sdl/system.c | 43 ++ src/sdl/system.h | 15 + src/stagedata.c | 93 +++ src/stagedata.h | 6 + src/text.c | 205 ++++++ src/text.h | 34 + src/titlescreen.c | 120 ++++ src/titlescreen.h | 6 + src/weapon.c | 601 ++++++++++++++++ src/weapon.h | 34 + src/wii/audio.c | 98 +++ src/wii/audio.h | 35 + src/wii/graphics.c | 290 ++++++++ src/wii/graphics.h | 61 ++ src/wii/input.c | 162 +++++ src/wii/input.h | 20 + src/wii/system.c | 47 ++ src/wii/system.h | 12 + 131 files changed, 22358 insertions(+) create mode 100644 src/3ds/audio.c create mode 100644 src/3ds/audio.h create mode 100644 src/3ds/graphics.c create mode 100644 src/3ds/graphics.h create mode 100644 src/3ds/input.c create mode 100644 src/3ds/input.h create mode 100644 src/3ds/system.c create mode 100644 src/3ds/system.h create mode 100644 src/PHL.c create mode 100644 src/PHL.h create mode 100644 src/collision.c create mode 100644 src/collision.h create mode 100644 src/effect.c create mode 100644 src/effect.h create mode 100644 src/enemies/bat.c create mode 100644 src/enemies/bat.h create mode 100644 src/enemies/batboss.c create mode 100644 src/enemies/batboss.h create mode 100644 src/enemies/bee.c create mode 100644 src/enemies/bee.h create mode 100644 src/enemies/boar.c create mode 100644 src/enemies/boar.h create mode 100644 src/enemies/boomknight.c create mode 100644 src/enemies/boomknight.h create mode 100644 src/enemies/crab.c create mode 100644 src/enemies/crab.h create mode 100644 src/enemies/devil.c create mode 100644 src/enemies/devil.h create mode 100644 src/enemies/dodo.c create mode 100644 src/enemies/dodo.h create mode 100644 src/enemies/dog.c create mode 100644 src/enemies/dog.h create mode 100644 src/enemies/firewheel.c create mode 100644 src/enemies/firewheel.h create mode 100644 src/enemies/fish.c create mode 100644 src/enemies/fish.h create mode 100644 src/enemies/garm.c create mode 100644 src/enemies/garm.h create mode 100644 src/enemies/gas.c create mode 100644 src/enemies/gas.h create mode 100644 src/enemies/ghoul.c create mode 100644 src/enemies/ghoul.h create mode 100644 src/enemies/golem.c create mode 100644 src/enemies/golem.h create mode 100644 src/enemies/gyra.c create mode 100644 src/enemies/gyra.h create mode 100644 src/enemies/heads.c create mode 100644 src/enemies/heads.h create mode 100644 src/enemies/hydra.c create mode 100644 src/enemies/hydra.h create mode 100644 src/enemies/jellyfish.c create mode 100644 src/enemies/jellyfish.h create mode 100644 src/enemies/knight.c create mode 100644 src/enemies/knight.h create mode 100644 src/enemies/lolidra.c create mode 100644 src/enemies/lolidra.h create mode 100644 src/enemies/pendulum.c create mode 100644 src/enemies/pendulum.h create mode 100644 src/enemies/podoboo.c create mode 100644 src/enemies/podoboo.h create mode 100644 src/enemies/poisonknight.c create mode 100644 src/enemies/poisonknight.h create mode 100644 src/enemies/pumpkin.c create mode 100644 src/enemies/pumpkin.h create mode 100644 src/enemies/seal.c create mode 100644 src/enemies/seal.h create mode 100644 src/enemies/skeleton.c create mode 100644 src/enemies/skeleton.h create mode 100644 src/enemies/skull.c create mode 100644 src/enemies/skull.h create mode 100644 src/enemies/slime.c create mode 100644 src/enemies/slime.h create mode 100644 src/enemies/slug.c create mode 100644 src/enemies/slug.h create mode 100644 src/enemies/thwomp.c create mode 100644 src/enemies/thwomp.h create mode 100644 src/enemies/waterjumper.c create mode 100644 src/enemies/waterjumper.h create mode 100644 src/enemies/wizard.c create mode 100644 src/enemies/wizard.h create mode 100644 src/enemy.c create mode 100644 src/enemy.h create mode 100644 src/game.c create mode 100644 src/game.h create mode 100644 src/hero.c create mode 100644 src/hero.h create mode 100644 src/ini.c create mode 100644 src/ini.h create mode 100644 src/inventory.c create mode 100644 src/inventory.h create mode 100644 src/main.c create mode 100644 src/object.c create mode 100644 src/object.h create mode 100644 src/options.c create mode 100644 src/options.h create mode 100644 src/platform.c create mode 100644 src/platform.h create mode 100644 src/psp/audio.c create mode 100644 src/psp/audio.h create mode 100644 src/psp/graphics.c create mode 100644 src/psp/graphics.h create mode 100644 src/psp/input.c create mode 100644 src/psp/input.h create mode 100644 src/psp/system.c create mode 100644 src/psp/system.h create mode 100644 src/qda.c create mode 100644 src/qda.h create mode 100644 src/sdl/audio.c create mode 100644 src/sdl/audio.h create mode 100644 src/sdl/graphics.c create mode 100644 src/sdl/graphics.h create mode 100644 src/sdl/input.c create mode 100644 src/sdl/input.h create mode 100644 src/sdl/system.c create mode 100644 src/sdl/system.h create mode 100644 src/stagedata.c create mode 100644 src/stagedata.h create mode 100644 src/text.c create mode 100644 src/text.h create mode 100644 src/titlescreen.c create mode 100644 src/titlescreen.h create mode 100644 src/weapon.c create mode 100644 src/weapon.h create mode 100644 src/wii/audio.c create mode 100644 src/wii/audio.h create mode 100644 src/wii/graphics.c create mode 100644 src/wii/graphics.h create mode 100644 src/wii/input.c create mode 100644 src/wii/input.h create mode 100644 src/wii/system.c create mode 100644 src/wii/system.h (limited to 'src') diff --git a/src/3ds/audio.c b/src/3ds/audio.c new file mode 100644 index 0000000..a63c88e --- /dev/null +++ b/src/3ds/audio.c @@ -0,0 +1,316 @@ +#include "audio.h" +#include +#include +#include + +const int MODE_NDSP = 1, + MODE_CSND = 2; + +int audioMode = 0; + +PHL_Sound loadWav(char* fname); +PHL_Music loadRaw(char* fname, u32 sampleRate, u32 bitsPerSample, u16 numChannels, u8 loop); + +void PHL_AudioInit() +{ + int loadMode = 0; + + Result rdsp = ndspInit(); + if (rdsp) { + //printf("\nNDSP Failed: %08lX\n", rdsp); + //loadMode = MODE_CSND; + loadMode = 0; //DSP or no sound for you! CSND a shit. + }else{ + loadMode = MODE_NDSP; + } + + if (loadMode == MODE_NDSP) { + //printf("\nNDSP Audio Mode"); + + ndspSetOutputMode(NDSP_OUTPUT_STEREO); + ndspSetOutputCount(1); + + audioMode = MODE_NDSP; + } + + else if (loadMode == MODE_CSND) { + //printf("\nCSND Audio Mode"); + + csndInit(); + audioMode = MODE_CSND; + } +} + +void PHL_AudioClose() +{ + if (audioMode == MODE_NDSP) { + ndspExit(); + } + + else if (audioMode == MODE_CSND) { + csndExit(); + } +} + +//Each system can use a custom music file format +PHL_Music PHL_LoadMusic(char* fname, int loop) +{ + return loadRaw(fname, 32000, 16, 1, loop); +} + +PHL_Music loadRaw(char* fname, u32 sampleRate, u32 bitsPerSample, u16 numChannels, u8 loop) +{ + PHL_Music result; + + char fullPath[128]; + strcpy(fullPath, "romfs:/"); + strcat(fullPath, fname); + strcat(fullPath, ".raw"); + + FILE* file; + if ((file = fopen(fullPath, "rb"))) + { + u32 size; + + fseek(file, 0, SEEK_END); + size = ftell(file); + + fseek(file, 0, SEEK_SET); + + result.sampleRate = sampleRate; + result.dataSize = size; + result.bitsPerSample = bitsPerSample; + result.numChannels = numChannels; + + if(bitsPerSample == 8) { + result.ndspFormat = (numChannels == 1) ? + NDSP_FORMAT_MONO_PCM8 : + NDSP_FORMAT_STEREO_PCM8; + } + else { + result.ndspFormat = (numChannels == 1) ? + NDSP_FORMAT_MONO_PCM16 : + NDSP_FORMAT_STEREO_PCM16; + } + + result.loop = loop; + + result.data = linearAlloc(size); + fread(result.data, 1, size, file); + + fclose(file); + } + + return result; + +} + +PHL_Sound PHL_LoadSound(char* fname) +{ + return loadWav(fname); +} + +PHL_Sound loadWav(char* fname) +{ + PHL_Sound snd; + + char fullPath[128]; + strcpy(fullPath, "romfs:/"); + strcat(fullPath, fname); + + FILE* f; + if ( (f = fopen(fullPath, "rb")) ) + { + //Check for valid fileName + u32 sig; + fread(&sig, 4, 1, f); + + if (sig == 0x46464952) //RIFF + { + u32 chunkSize; + u32 format; + + fread(&chunkSize, 4, 1, f); + fread(&format, 4, 1, f); + + if (format == 0x45564157) //WAVE + { + u32 subchunk1ID; + fread(&subchunk1ID, 4, 1, f); + + if (subchunk1ID == 0x20746D66) //fmt + { + u32 subchunk1Size; + u16 audioFormat; + u16 numChannels; + u32 sampleRate; + u32 byteRate; + u16 blockAlign; + u16 bitsPerSample; + + fread(&subchunk1Size, 4, 1, f); + fread(&audioFormat, 2, 1, f); + fread(&numChannels, 2, 1, f); + fread(&sampleRate, 4, 1, f); + fread(&byteRate, 4, 1, f); + fread(&blockAlign, 2, 1, f); + fread(&bitsPerSample, 2, 1, f); + + //Search for 'data' + for (int i = 0; i < 100; i++) { + u8 c; + fread(&c, 1, 1, f); + if (c == 0x64) { //'d' + fread(&c, 1, 1, f); + if (c == 0x61) { //'a' + fread(&c, 1, 1, f); + if (c == 0x74) { //'t' + fread(&c, 1, 1, f); + if (c == 0x61) { //'a' + i = 100; + } + } + } + } + } + + u32 subchunk2Size; + fread(&subchunk2Size, 4, 1, f); + + snd.numChannels = numChannels; + + if(bitsPerSample == 8) + { + snd.ndspFormat = (numChannels == 1) ? + NDSP_FORMAT_MONO_PCM8 : + NDSP_FORMAT_STEREO_PCM8; + } + else + { + snd.ndspFormat = (numChannels == 1) ? + NDSP_FORMAT_MONO_PCM16 : + NDSP_FORMAT_STEREO_PCM16; + } + + snd.sampleRate = sampleRate; + snd.dataSize = subchunk2Size; + snd.bitsPerSample = bitsPerSample; + + snd.data = (u8*)(linearAlloc(subchunk2Size)); + fread(snd.data, 1, subchunk2Size, f); + } + } + } + + fclose(f); + } + + return snd; +} + +void PHL_PlayMusic(PHL_Music snd) +{ + if (snd.data != NULL) { + if (audioMode == MODE_NDSP) + { + ndspChnReset(0); + ndspChnSetInterp(0, NDSP_INTERP_NONE); + ndspChnSetRate(0, (float)snd.sampleRate); + ndspChnSetFormat(0, snd.ndspFormat); + + + memset(&waveBuf[0], 0, sizeof(ndspWaveBuf)); + waveBuf[0].data_vaddr = (u32)(snd.data); + waveBuf[0].nsamples = snd.dataSize / (snd.bitsPerSample >> 3) / snd.numChannels; + waveBuf[0].looping = snd.loop; + waveBuf[0].status = NDSP_WBUF_FREE; + + DSP_FlushDataCache(snd.data, snd.dataSize); + ndspChnWaveBufAdd(0, &waveBuf[0]); + } + + else if (audioMode == MODE_CSND) + { + u32 flags = SOUND_FORMAT_16BIT; + + if (snd.loop == 1) { + flags = SOUND_FORMAT_16BIT | SOUND_REPEAT; + } + + int channel = (0 + 1) * 8; + csndPlaySound(channel, flags, snd.sampleRate, 1, 0, snd.data, snd.data, snd.dataSize); + } + } +} + +void PHL_PlaySound(PHL_Sound snd, int channel) +{ + if (snd.data != NULL) { + if (audioMode == MODE_NDSP) + { + ndspChnReset(channel); + ndspChnSetInterp(channel, NDSP_INTERP_NONE); + ndspChnSetRate(channel, (float)snd.sampleRate); + ndspChnSetFormat(channel, snd.ndspFormat); + + + memset(&waveBuf[channel], 0, sizeof(ndspWaveBuf)); + waveBuf[channel].data_vaddr = (u32)(snd.data); + waveBuf[channel].nsamples = snd.dataSize / (snd.bitsPerSample >> 3) / snd.numChannels; + waveBuf[channel].looping = false; + waveBuf[channel].status = NDSP_WBUF_FREE; + + DSP_FlushDataCache(snd.data, snd.dataSize); + ndspChnWaveBufAdd(channel, &waveBuf[channel]); + } + + else if (audioMode == MODE_CSND) + { + channel = (channel + 1) * 8; + csndPlaySound(channel, SOUND_FORMAT_16BIT, snd.sampleRate, 1, 0, snd.data, snd.data, snd.dataSize); + } + } +} + +void PHL_StopMusic() +{ + if (audioMode == MODE_NDSP) + { + ndspChnWaveBufClear(0); + } + + else if (audioMode == MODE_CSND) + { + CSND_SetPlayState(8, 0); + } +} + +void PHL_StopSound(PHL_Sound snd, int channel) +{ + if (audioMode == MODE_NDSP) + { + ndspChnWaveBufClear(channel); + } + + else if (audioMode == MODE_CSND) + { + channel = (channel + 1) * 8; + CSND_SetPlayState(channel, 0); + } +} + +void PHL_FreeMusic(PHL_Music snd) +{ + if (snd.data != NULL) { + linearFree(snd.data); + snd.data = NULL; + } +} + +void PHL_FreeSound(PHL_Sound snd) +{ + if (snd.data != NULL) { + linearFree(snd.data); + snd.data = NULL; + } +} \ No newline at end of file diff --git a/src/3ds/audio.h b/src/3ds/audio.h new file mode 100644 index 0000000..28b615c --- /dev/null +++ b/src/3ds/audio.h @@ -0,0 +1,51 @@ +#ifndef AUDIO_H +#define AUDIO_H + +#include <3ds.h> + +typedef struct { + u16 sampleRate; + u32 dataSize; + u16 bitsPerSample; + u16 ndspFormat; + u16 numChannels; + u8 loop; + u8* data; + //int used; +} PHL_Music; + +typedef struct{ + u32 sampleRate; + u32 dataSize; + u16 bitsPerSample; + u16 ndspFormat; + u16 numChannels; + u8* data; +} PHL_Sound; + +ndspWaveBuf waveBuf[6]; //One for each channel? + +void PHL_AudioInit(); +void PHL_AudioClose(); + +PHL_Music PHL_LoadMusic(char* fname, int loop); //Same as PHL_LoadSound, but expects a file name without extension +PHL_Sound PHL_LoadSound(char* fname); + +void PHL_PlayMusic(PHL_Music snd); +void PHL_PlaySound(PHL_Sound snd, int channel); + +void PHL_StopMusic(); +void PHL_StopSound(PHL_Sound snd, int channel); + +/* +PHL_Music PHL_LoadMusic(char* fname, int channel); //Same as PHL_LoadSound, but expects a file name without extension +PHL_Sound PHL_LoadSound(char* fname, int loop, int stream, int channel); + +void PHL_PlayMusic(PHL_Music snd); +void PHL_PlaySound(PHL_Sound snd); +*/ + +void PHL_FreeMusic(PHL_Music snd); +void PHL_FreeSound(PHL_Sound snd); + +#endif \ No newline at end of file diff --git a/src/3ds/graphics.c b/src/3ds/graphics.c new file mode 100644 index 0000000..dd82955 --- /dev/null +++ b/src/3ds/graphics.c @@ -0,0 +1,435 @@ +//3DS graphics.c +#include "graphics.h" +#include +#include +#include +#include "../game.h" +#include "../qda.h" +#include "../hero.h" + +Screen scrnTop = { GFX_TOP, + GFX_LEFT, + 400, + 240 }; + +Screen scrnBottom = { GFX_BOTTOM, + GFX_LEFT, + 320, + 240 }; + +PHL_Rect offset/*, crop*/; + +//One time graphics setup +void PHL_GraphicsInit() +{ + gfxInitDefault(); + + activeScreen = &scrnTop; + /* + offset.w = 320; + offset.h = 240; + offset.x = (activeScreen->width - offset.w) / 2; + offset.y = (activeScreen->height - offset.h) / 2; + + crop.w = 320; + crop.h = 320; + crop.x = 0; + crop.y = 0; + + db.width = activeScreen->width; + db.height = activeScreen->height; + */ + PHL_ResetDrawbuffer(); + + //Create background image + backBuffer = PHL_NewSurface(640, 480); + + //consoleInit(GFX_BOTTOM, NULL); + //printf("Console Init"); +} + +void PHL_GraphicsExit() +{ + gfxExit(); +} + +void PHL_StartDrawing() +{ + PHL_ResetDrawbuffer(); + + gfxFlushBuffers(); +} + +void PHL_EndDrawing() +{ + gfxSwapBuffers(); + gspWaitForVBlank(); +} + +void PHL_ForceScreenUpdate() +{ + //Draw black borders on sides of top screen + /* + PHL_DrawRect(-80, 0, 80, 480, PHL_NewRGB(0, 0, 0)); + PHL_DrawRect(640, 0, 80, 480, PHL_NewRGB(0, 0, 0)); + + gspWaitForVBlank(); + gfxSwapBuffers(); + + gfxFlushBuffers(); + swapScreen(GFX_TOP, GFX_LEFT); + */ + + //PHL_DrawRect(0, 0, 640, 48, PHL_NewRGB(0, 0, 0)); + //gfxSwapBuffers(); +} + +void PHL_SetDrawbuffer(PHL_Surface surf) +{ + db = surf; + + offset.w = db.width; + offset.h = db.height; + offset.x = 0; + offset.y = 0; + /* + crop.w = db.width; + crop.h = db.height; + crop.x = 0; + crop.y = 0; + */ +} + +void PHL_ResetDrawbuffer() +{ + db.width = activeScreen->width; + db.height = activeScreen->height; + db.pxdata = gfxGetFramebuffer(activeScreen->screen, activeScreen->side, NULL, NULL); + + offset.w = 320; + offset.h = 240; + offset.x = (activeScreen->width - offset.w) / 2; + offset.y = (activeScreen->height - offset.h) / 2; + /* + crop.w = 320; + crop.h = 240; + crop.x = 0; + crop.y = 0;*/ +} + +PHL_RGB PHL_NewRGB(u8 r, u8 g, u8 b) +{ + PHL_RGB c = { r, g, b }; + return c; +} + +void PHL_SetColorKey(PHL_Surface surf, u8 r, u8 g, u8 b) +{ + PHL_RGB key = { r, g, b }; + surf.colorKey = key; +} + +PHL_Surface PHL_NewSurface(u16 w, u16 h) +{ + PHL_Surface surf; + + surf.width = w / 2; + surf.height = h / 2; + //printf("\nWidth: %d", surf.width); + surf.pxdata = malloc(surf.width * surf.height * 3 * sizeof(u8)); + surf.colorKey = PHL_NewRGB(0xFF, 0x00, 0xFF); + + return surf; +} + +void PHL_FreeSurface(PHL_Surface surf) +{ + if (surf.pxdata != NULL) { + free(surf.pxdata); + surf.pxdata = NULL; + } +} + +PHL_Surface PHL_LoadBMP(int index) +{ + PHL_Surface surf; + + FILE* f; + if ( (f = fopen("romfs:/bmp.qda", "rb")) ) + { + //Save bmp data + u8* bmpFile = malloc(headers[index].size * sizeof(u8)); + + fseek(f, headers[index].offset, SEEK_SET); + fread(bmpFile, headers[index].size, 1, f); + fclose(f); + + //Create surface + u16 w, h; + memcpy(&w, &bmpFile[18], 2); + memcpy(&h, &bmpFile[22], 2); + + surf = PHL_NewSurface(w * 2, h * 2); + + //Load Palette + PHL_RGB palette[20][18]; + + int count = 0; + for (int dx = 0; dx < 20; dx++) + { + for (int dy = 0; dy < 16; dy++) + { + palette[dx][dy].b = bmpFile[54 + count]; + palette[dx][dy].g = bmpFile[54 + count + 1]; + palette[dx][dy].r = bmpFile[54 + count + 2]; + + count += 4; + } + } + + //Fill pixels + count = 0; + for (int dx = w; dx > 0; dx--) + { + for (int dy = 0; dy < h; dy++) + { + int pix = w - dx + w * dy; + int px = bmpFile[1078 + pix] / 16; + int py = bmpFile[1078 + pix] % 16; + + //Get transparency from first palette color + if (dx == w && dy == 0) { + //Darkness special case + if (index == 27) { + surf.colorKey = PHL_NewRGB(0x00, 0x00, 0x00); + }else{ + surf.colorKey = palette[0][0]; + } + } + + PHL_RGB c = palette[px][py]; + surf.pxdata[count] = c.b; + surf.pxdata[count+1] = c.g; + surf.pxdata[count+2] = c.r; + + count += 3; + } + } + + //Cleanup + free(bmpFile); + } + + return surf; +} + +void PHL_DrawRect(s16 x, s16 y, u16 w, u16 h, PHL_RGB col) +{ + //Quake Shake + if (quakeTimer > 0) { + int val = quakeTimer % 4; + if (val == 0) { + y -= 2; + }else if (val == 2) { + y += 2; + } + } + + //Shrink values for small 3ds screen + x /= 2; + y /= 2; + + x += offset.x; + y += offset.y; + + w /= 2; + h /= 2; + + s16 x2 = x + w; + s16 y2 = y + h; + + //Keep drawing within screen + if (x < offset.x) { x = offset.x; } + if (y < offset.y) { y = offset.y; } + if (x2 > offset.x + offset.w) { x2 = offset.x + offset.w; } + if (y2 > offset.y + offset.h) { y2 = offset.y + offset.h; } + + w = x2 - x; + h = y2 - y; + + u32 p = ((db.height - h - y) + (x * db.height)) * 3; + + for (int i = 0; i < w; i++) + { + for (int a = 0; a < h; a++) + { + db.pxdata[p] = col.b; + db.pxdata[p+1] = col.g; + db.pxdata[p+2] = col.r; + + p += 3; + } + p += (db.height - h) * 3; + } +} + +void PHL_DrawSurface(s16 x, s16 y, PHL_Surface surf) +{ + PHL_DrawSurfacePart(x, y, 0, 0, surf.width * 2, surf.height * 2, surf); +} + +void PHL_DrawSurfacePart(s16 x, s16 y, s16 cropx, s16 cropy, s16 cropw, s16 croph, PHL_Surface surf) +{ + if (surf.pxdata != NULL) + { + //Quake Shake + if (quakeTimer > 0) { + int val = quakeTimer % 4; + if (val == 0) { + y -= 2; + }else if (val == 2) { + y += 2; + } + } + + //Shrink values for small 3ds screen + x = (int)x / 2; + y = (int)y / 2; + + cropx = cropx / 2; + cropy = cropy / 2; + cropw /= 2; + croph /= 2; + + if (x > offset.w || y > offset.h || x + cropw < 0 || y + croph < 0) { + //image is outside of screen, so don't bother drawing + } + else{ + //Crop pixels that are outside of screen + if (x < 0) { + cropx += -(x); + cropw -= -(x); + x = 0; + } + if (y < 0) { + cropy += -(y); + croph -= -(y); + y = 0; + } + + //3DS exclusive optimization + if (roomDarkness == 1) { + //if (1) { + int cornerX = (herox / 2) - 80; + int cornerY = (heroy / 2) + 10 - 80; + + if (x < cornerX) { + cropx += cornerX - x; + cropw -= cornerX - x; + x = cornerX; + } + if (y < cornerY) { + cropy += cornerY - y; + croph -= cornerY - y; + y = cornerY; + } + if (x + cropw > cornerX + 160) { + cropw -= (x + cropw) - (cornerX + 160); + } + if (y + croph > cornerY + 160) { + croph -= (y + croph) - (cornerY + 160); + } + } + + if (x + cropw > offset.w) { + cropw -= (x + cropw) - (offset.w); + } + if (y + croph > offset.h) { + croph -= (y + croph) - (offset.h); + } + + x += offset.x; + y += offset.y; + + u32 p = ((offset.h - croph - y) + (x * offset.h)) * 3; + u32 c = ((surf.height - cropy - croph) + surf.height * cropx) * 3; + + for (int i = 0; i < cropw; i++) + { + for (int a = 0; a < croph; a++) + { + if (surf.colorKey.r != surf.pxdata[c+2] || + surf.colorKey.g != surf.pxdata[c+1] || + surf.colorKey.b != surf.pxdata[c] ) + { + + db.pxdata[p] = surf.pxdata[c]; + db.pxdata[p+1] = surf.pxdata[c+1]; + db.pxdata[p+2] = surf.pxdata[c+2]; + } + + c += 3; + p += 3; + } + + p += (offset.h - croph) * 3; + c += (surf.height - croph) * 3; + } + } + } +} + +void PHL_DrawBackground(PHL_Background back, PHL_Background fore) +{ + PHL_DrawSurface(0, 0, backBuffer); +} + +void PHL_UpdateBackground(PHL_Background back, PHL_Background fore) +{ + int tempDarkness = roomDarkness; + roomDarkness = 0; + + PHL_SetDrawbuffer(backBuffer); + + int xx, yy; + + for (yy = 0; yy < 12; yy++) + { + for (xx = 0; xx < 16; xx++) + { + //Draw Background tiles + PHL_DrawSurfacePart(xx * 40, yy * 40, back.tileX[xx][yy] * 40, back.tileY[xx][yy] * 40, 40, 40, images[imgTiles]); + + //Only draw foreground tile if not a blank tile + if (fore.tileX[xx][yy] != 0 || fore.tileY[xx][yy] != 0) { + PHL_DrawSurfacePart(xx * 40, yy * 40, fore.tileX[xx][yy] * 40, fore.tileY[xx][yy] * 40, 40, 40, images[imgTiles]); + } + } + } + + PHL_ResetDrawbuffer(); + + roomDarkness = tempDarkness; +} + +//3DS exclusive. Changes which screen to draw on +void swapScreen(gfxScreen_t screen, gfx3dSide_t side) +{ + //Clear old screen + PHL_StartDrawing(); + PHL_DrawRect(0, 0, 640, 480, PHL_NewRGB(0, 0, 0)); + PHL_EndDrawing(); + PHL_StartDrawing(); + PHL_DrawRect(0, 0, 640, 480, PHL_NewRGB(0, 0, 0)); + PHL_EndDrawing(); + + if (screen == GFX_TOP) + { + activeScreen = &scrnTop; + } + else{ + activeScreen = &scrnBottom; + } + + PHL_ResetDrawbuffer(); +} \ No newline at end of file diff --git a/src/3ds/graphics.h b/src/3ds/graphics.h new file mode 100644 index 0000000..badd898 --- /dev/null +++ b/src/3ds/graphics.h @@ -0,0 +1,67 @@ +//3DS graphics.h +#ifndef GRAPHICS_H +#define GRAPHICS_H + +#include <3ds.h> + +typedef struct { + u8 r; + u8 g; + u8 b; +} PHL_RGB; + +typedef struct { + u8* pxdata; + u16 width; + u16 height; + PHL_RGB colorKey; +} PHL_Surface; + +typedef struct { + gfxScreen_t screen; + gfx3dSide_t side; + u16 width; + u16 height; +} Screen; + +typedef struct { + int tileX[16][12]; + int tileY[16][12]; +} PHL_Background; + +PHL_Surface db; +Screen* activeScreen; + +PHL_Surface backBuffer; + +void PHL_GraphicsInit(); +void PHL_GraphicsExit(); + +void PHL_StartDrawing(); +void PHL_EndDrawing(); + +void PHL_ForceScreenUpdate(); + +void PHL_SetDrawbuffer(PHL_Surface surf); +void PHL_ResetDrawbuffer(); + +PHL_RGB PHL_NewRGB(u8 r, u8 g, u8 b); +void PHL_SetColorKey(PHL_Surface surf, u8 r, u8 g, u8 b); + +PHL_Surface PHL_NewSurface(u16 w, u16 h); +void PHL_FreeSurface(PHL_Surface surf); + +PHL_Surface PHL_LoadBMP_Debug(int index); +PHL_Surface PHL_LoadBMP(int index); + +void PHL_DrawRect(s16 x, s16 y, u16 w, u16 h, PHL_RGB col); + +void PHL_DrawSurface(s16 x, s16 y, PHL_Surface surf); +void PHL_DrawSurfacePart(s16 x, s16 y, s16 cropx, s16 cropy, s16 cropw, s16 croph, PHL_Surface surf); + +void PHL_DrawBackground(PHL_Background back, PHL_Background fore); +void PHL_UpdateBackground(PHL_Background back, PHL_Background fore); + +void swapScreen(gfxScreen_t screen, gfx3dSide_t side); + +#endif \ No newline at end of file diff --git a/src/3ds/input.c b/src/3ds/input.c new file mode 100644 index 0000000..0d99b24 --- /dev/null +++ b/src/3ds/input.c @@ -0,0 +1,56 @@ +#include "input.h" + +void updateKey(Button* btn, int state); + +void PHL_ScanInput() +{ + hidScanInput(); + u32 kDown = hidKeysHeld(); + + updateKey(&btnUp, kDown & KEY_UP); + updateKey(&btnDown, kDown & KEY_DOWN); + updateKey(&btnLeft, kDown & KEY_LEFT); + updateKey(&btnRight, kDown & KEY_RIGHT); + + updateKey(&btnStart, kDown & KEY_START); + updateKey(&btnSelect, kDown & KEY_SELECT); + + updateKey(&btnFaceRight, kDown & KEY_A); + updateKey(&btnFaceDown, kDown & KEY_B); + updateKey(&btnFaceLeft, kDown & KEY_Y); + + updateKey(&btnL, kDown & KEY_L); + updateKey(&btnR, kDown & KEY_R); + + updateKey(&btnAccept, kDown & KEY_A); + updateKey(&btnDecline, kDown & KEY_B); + /* + //Start button + if (kDown & KEY_START) { + btnStart.pressed = 1; + }else{ + btnStart.pressed = 0; + } + */ +} + +void updateKey(Button* btn, int state) +{ + if (state) { + if (btn->held == 1) { + btn->pressed = 0; + }else{ + btn->pressed = 1; + } + btn->held = 1; + btn->released = 0; + }else{ + if (btn->held == 1) { + btn->released = 1; + }else{ + btn->released = 0; + } + btn->held = 0; + btn->pressed = 0; + } +} \ No newline at end of file diff --git a/src/3ds/input.h b/src/3ds/input.h new file mode 100644 index 0000000..7b7076e --- /dev/null +++ b/src/3ds/input.h @@ -0,0 +1,25 @@ +#ifndef INPUT_H +#define INPUT_H + +#include <3ds.h> + +typedef struct { + int pressed, + held, + released; +} Button; + +Button btnUp, btnDown, btnLeft, btnRight; +Button btnFaceUp, btnFaceDown, btnFaceLeft, btnFaceRight; + +Button btnL, btnR; +Button btnStart, btnSelect; + +Button btnAccept; +Button btnDecline; + +int axisX, axisY; + +void PHL_ScanInput(); + +#endif \ No newline at end of file diff --git a/src/3ds/system.c b/src/3ds/system.c new file mode 100644 index 0000000..9b824cb --- /dev/null +++ b/src/3ds/system.c @@ -0,0 +1,48 @@ +#include "system.h" +#include <3ds.h> +#include "../PHL.h" +#include +#include + +int quitGame = 0; + +int PHL_MainLoop() +{ + if (quitGame == 1) { + return 0; + } + + return aptMainLoop(); +} + +void PHL_ConsoleInit() +{ + consoleInit(activeScreen->screen, NULL); +} + +void PHL_GameQuit() +{ + quitGame = 1; +} + +void PHL_ErrorScreen(char* message) +{ + consoleInit(GFX_TOP, NULL); + + printf(message); + printf("\n\nPress START to close"); + + u32 kDown; + while (PHL_MainLoop()) { + //gfxFlushBuffers(); + //gfxSwapBuffers(); + hidScanInput(); + kDown = hidKeysHeld(); + + if (kDown & KEY_START) { break; } + + gspWaitForVBlank(); + } + + exit(1); +} \ No newline at end of file diff --git a/src/3ds/system.h b/src/3ds/system.h new file mode 100644 index 0000000..c29d25f --- /dev/null +++ b/src/3ds/system.h @@ -0,0 +1,12 @@ +#ifndef SYSTEM_H +#define SYSTEM_H + +//int quitGame; + +int PHL_MainLoop(); +void PHL_ConsoleInit(); +void PHL_GameQuit(); + +void PHL_ErrorScreen(char* message); + +#endif \ No newline at end of file diff --git a/src/PHL.c b/src/PHL.c new file mode 100644 index 0000000..8db7e32 --- /dev/null +++ b/src/PHL.c @@ -0,0 +1,83 @@ +#include "PHL.h" +#include +#include +#include "qda.h" +#include "game.h" + +void PHL_Init() +{ + PHL_GraphicsInit(); + PHL_AudioInit(); + + #ifdef _3DS + Result rc = romfsInit(); + /*if (rc) { + printf("romfsInit: %08lX\n", rc); + //while(1){} + } + else + { + printf("\nromfs Init Successful!\n"); + }*/ + #endif + + WHITE = 0; + RED = 1; + YELLOW = 2; +} + +void PHL_Deinit() +{ + PHL_AudioClose(); + PHL_GraphicsExit(); + + #ifdef _3DS + romfsExit(); + #endif +} + +//Extracts bmps from the bmp.qda archive file +PHL_Surface PHL_LoadQDA(char* fname) +{ + PHL_Surface surf; + + int numofsheets = 29; + + for (int i = 0; i < numofsheets; i++) + { + if (strcmp(fname, (char*)headers[i].fileName) == 0) { //Match found + //printf("\nMatch Found: %s", fname); + surf = PHL_LoadBMP(i); + i = numofsheets; //End search + } + } + + return surf; +} + +void PHL_DrawTextBold(char* txt, int dx, int dy, int col) +{ + int i, cx, cy; + + for (i = 0; i < strlen(txt); i++) + { + cx = (txt[i] - 32) * 16; + cy = 32 * col; + + while (cx >= 512) { + cx -= 512; + cy += 16; + } + + PHL_DrawSurfacePart(dx + (16 * i), dy, cx, cy, 16, 16, images[imgBoldFont]); + } +} + +void PHL_DrawTextBoldCentered(char* txt, int dx, int dy, int col) +{ + if (dy < 640 && dy > -16) { + int stringW = strlen(txt) * 16; + + PHL_DrawTextBold(txt, dx - (stringW / 2), dy, col); + } +} \ No newline at end of file diff --git a/src/PHL.h b/src/PHL.h new file mode 100644 index 0000000..4b70691 --- /dev/null +++ b/src/PHL.h @@ -0,0 +1,50 @@ +/* +PHL stands for Portable Homebrew Library +*/ +#ifndef PHL_H +#define PHL_H + +#ifdef _3DS + #include "3ds/system.h" + #include "3ds/graphics.h" + #include "3ds/input.h" + #include "3ds/audio.h" +#endif + +#ifdef _WII + #include "wii/system.h" + #include "wii/graphics.h" + #include "wii/input.h" + #include "wii/audio.h" +#endif + +#ifdef _PSP + #include "psp/system.h" + #include "psp/graphics.h" + #include "psp/input.h" + #include "psp/audio.h" +#endif + +#ifdef _SDL + #include "sdl/system.h" + #include "sdl/graphics.h" + #include "sdl/input.h" + #include "sdl/audio.h" +#endif + +typedef struct { + int x, y, w, h; +} PHL_Rect; + +void PHL_Init(); +void PHL_Deinit(); + +int WHITE, + RED, + YELLOW; + +PHL_Surface PHL_LoadQDA(char* fname); +void PHL_DrawTextBold(char* txt, int dx, int dy, int col); +void PHL_DrawTextBoldCentered(char* txt, int dx, int dy, int col); + +#endif \ No newline at end of file diff --git a/src/collision.c b/src/collision.c new file mode 100644 index 0000000..34d4ddb --- /dev/null +++ b/src/collision.c @@ -0,0 +1,336 @@ +#include "collision.h" +#include "math.h" +#include "game.h" +#include "PHL.h" +#include "object.h" + +int checkMix(Mask r, Mask c); +int checkRect(Mask r1, Mask r2); +int checkCircle(Mask c1, Mask c2); + +int checkCollision(Mask m1, Mask m2) +{ + if (m1.unused != 1 && m2.unused != 1) { + if (m1.circle == 0 && m2.circle == 0) { + return checkRect(m1, m2); + }else if (m1.circle == 1 && m2.circle == 1) { + return checkCircle(m1, m2); + }else if (m1.circle == 1 && m2.circle == 0) { + return checkMix(m2, m1); + }else if (m1.circle == 0 && m2.circle == 1) { + return checkMix(m1, m2); + } + } + + return 0; +} + +int checkCollisionXY(Mask m, int x, int y) +{ + int result = 0; + + if (m.unused != 1) { + if (m.circle == 1) { + if (sqrt( pow(x - m.x, 2) + pow(y - m.y, 2) ) <= m.w) { + result = 1; + } + }else{ + if (x < m.x || x > m.x + m.w || y < m.y || y > m.y + m.h) { + }else{ + result = 1; + } + } + } + + return result; +} + +//Returns 1 or 0 depending on if there is a collision with a type of tile +int checkTileCollision(int type, Mask m) +{ + int result = 0; + + if (m.x < 0) { + m.x = 0; + }else if (m.x + m.w > 640) { + m.x = 640 - m.w; + } + + if (m.y < 0) { + m.y = 0; + }else if (m.y + m.h > 480) { + m.y = 480 - m.h; + } + + int i; + for (i = 0; i < 4; i++) { + int tileX = (int)m.x / 40; + int tileY = (int)m.y / 40; + + if (i == 1) { + tileX = (int)((m.x + m.w - 1) / 40); + }else if (i == 2) { + tileY = (int)((m.y + m.h - 1) / 40); + }else if (i == 3) { + tileX = (int)((m.x + m.w - 1) / 40); + tileY = (int)((m.y + m.h - 1) / 40); + } + + if (collisionTiles[tileX][tileY] == type) { + result = 1; + i = 4; + } + } + + return result; +} + +//Returns a tile's demension. Overkill for a lot of situations. +PHL_Rect getTileCollision(int type, Mask m) +{ + PHL_Rect result; + result.x = -1; + result.y = -1; + result.w = 40; + result.h = 40; + + //updateMask(); + + if (m.x < 0) { + m.x = 0; + }else if (m.x + m.w > 640) { + m.x = 640 - m.w; + } + + if (m.y < 0) { + m.y = 0; + }else if (m.y + m.h > 480) { + m.y = 480 - m.h; + } + + //PHL_DrawRect(mask.x, mask.y, mask.w, mask.h, PHL_NewRGB(0x00, 0x00, 0xFF)); + + int i; + for (i = 0; i < 4; i++) { + int tileX = (int)m.x / 40; + int tileY = (int)m.y / 40; + + if (i == 1) { + tileX = (int)((m.x + m.w - 1) / 40); + }else if (i == 2) { + tileY = (int)((m.y + m.h - 1) / 40); + }else if (i == 3) { + tileX = (int)((m.x + m.w - 1) / 40); + tileY = (int)((m.y + m.h - 1) / 40); + } + + if (collisionTiles[tileX][tileY] == type) { + result.x = tileX * 40; + result.y = tileY * 40; + i = 4; + //PHL_DrawRect(result.x, result.y, 40, 40, PHL_NewRGB(0xFF, 0x00, 0x00)); + } + //PHL_DrawRect(tileX * 40, tileY * 40, 40, 40, PHL_NewRGB(0x00, 0xFF, 0x00)); + } + + //updateMask(); + return result; +} + +int checkTileCollisionXY(int type, int x, int y) +{ + int result = 0; + + if (x < 0) { + x = 0; + }else if (x > 640) { + x = 640; + } + + if (y < 0) { + y = 0; + }else if (y > 480) { + y = 480; + } + + int tileX = (int)x / 40; + int tileY = (int)y / 40; + + if (collisionTiles[tileX][tileY] == type) { + result = 1; + } + + return result; +} + +PHL_Rect getTileCollisionXY(int type, int x, int y) +{ + PHL_Rect result; + result.x = -1; + result.y = -1; + result.w = 40; + result.h = 40; + + if (x < 0) { + x = 0; + }else if (x > 640) { + x = 640; + } + + if (y < 0) { + y = 0; + }else if (y > 480) { + y = 480; + } + + int tileX = (int)x / 40; + int tileY = (int)y / 40; + + if (collisionTiles[tileX][tileY] == type) { + result.x = tileX * 40; + result.y = tileY * 40; + } + + return result; +} + +void PHL_DrawMask(Mask m) +{ + if (m.circle == 0) { + PHL_DrawRect(m.x, m.y, m.w, m.h, PHL_NewRGB(255, 255, 255)); + }else if (m.circle == 1) { + PHL_DrawRect(m.x - m.w, m.y - m.w, m.w * 2, m.w * 2, PHL_NewRGB(255, 255, 255)); + } +} + +int checkMix(Mask r, Mask c) +{ + int insidex = 0, insidey = 0; + + if (c.x >= r.x && c.x <= r.x + r.w) { + insidex = 1; + } + if (c.y >= r.y && c.y <= r.y + r.h) { + insidey = 1; + } + + //Check if circle center is inside rectangle + if (insidex == 1 && insidey == 1) { + } + else if (insidex == 1) { + if ((c.y < r.y && r.y - c.y <= c.w) || + (c.y > (r.y + r.h) && c.y - (r.y + r.h) <= c.w)) { + }else{ + return 0; + } + }else if (insidey == 1) { + if ((c.x < r.x && r.x - c.x <= c.w) || + (c.x > (r.x + r.w) && c.x - (r.x + r.w) <= c.w)) { + }else{ + return 0; + } + }else{ + //Check points + if (sqrt( pow(r.x - c.x, 2) + pow(r.y - c.y, 2) ) <= c.w) { + }else if (sqrt( pow(r.x + r.w - c.x, 2) + pow(r.y - c.y, 2) ) <= c.w) { + }else if (sqrt( pow(r.x - c.x, 2) + pow(r.y + r.h - c.y, 2) ) <= c.w) { + }else if (sqrt( pow(r.x + r.w - c.x, 2) + pow( r.y + r.h - c.y, 2) ) <= c.w) { + }else{ + return 0; + } + } + + return 1; +} + +int checkRect(Mask r1, Mask r2) +{ + if (r1.x > r2.x + r2.w || + r1.x + r1.w < r2.x || + r1.y > r2.y + r2.h || + r1.y + r1.h < r2.y) + { + return 0; + } + + return 1; +} + +int checkCircle(Mask c1, Mask c2) +{ + int maxdis = c1.w + c2.w; + int dis = sqrt(pow(c2.x - c1.x, 2) + pow(c2.y - c1.y, 2)); + + if (dis <= maxdis) { + return 1; + } + + return 0; +} + +//Heavier tile collision that omits destroyable blocks +PHL_Rect getTileCollisionWeapon(int type, Mask m) +{ + PHL_Rect result; + result.x = -1; + result.y = -1; + result.w = 40; + result.h = 40; + + //updateMask(); + + if (m.x < 0) { + m.x = 0; + }else if (m.x + m.w > 640) { + m.x = 640 - m.w; + } + + if (m.y < 0) { + m.y = 0; + }else if (m.y + m.h > 480) { + m.y = 480 - m.h; + } + + //PHL_DrawRect(mask.x, mask.y, mask.w, mask.h, PHL_NewRGB(0x00, 0x00, 0xFF)); + + int i; + for (i = 0; i < 4; i++) { + int tileX = (int)m.x / 40; + int tileY = (int)m.y / 40; + + if (i == 1) { + tileX = (int)((m.x + m.w - 1) / 40); + }else if (i == 2) { + tileY = (int)((m.y + m.h - 1) / 40); + }else if (i == 3) { + tileX = (int)((m.x + m.w - 1) / 40); + tileY = (int)((m.y + m.h - 1) / 40); + } + + if (collisionTiles[tileX][tileY] == type) { + result.x = tileX * 40; + result.y = tileY * 40; + + //Check if destroyable block + int a; + for (a = 0; a < MAX_OBJECTS; a++) { + if (objects[a] != NULL) { + if (objects[a]->type == 3) { + Destroyable* d = objects[a]->data; + if (result.x == d->x && result.y == d->y) { + result.x = -1; + result.y = -1; + } + } + } + } + + if (result.x != -1) { + i = 4; + } + } + } + + //updateMask(); + return result; +} \ No newline at end of file diff --git a/src/collision.h b/src/collision.h new file mode 100644 index 0000000..d13fa17 --- /dev/null +++ b/src/collision.h @@ -0,0 +1,27 @@ +#ifndef COLLISION_H +#define COLLISION_H + +#include "PHL.h" + +typedef struct { + int circle; //1 if circle, 0 is rectangle + int x, y; + int w, h; //width is the radius if it's a circle + int unused; +} Mask; + +void PHL_DrawMask(Mask m); + +int checkCollision(Mask m1, Mask m2); + +int checkTileCollision(int type, Mask m); +PHL_Rect getTileCollision(int type, Mask m); + +int checkCollisionXY(Mask m, int x, int y); + +int checkTileCollisionXY(int type, int x, int y); +PHL_Rect getTileCollisionXY(int type, int x, int y); + +PHL_Rect getTileCollisionWeapon(int type, Mask m); + +#endif \ No newline at end of file diff --git a/src/effect.c b/src/effect.c new file mode 100644 index 0000000..e375a7c --- /dev/null +++ b/src/effect.c @@ -0,0 +1,384 @@ +#include "effect.h" +#include "game.h" +#include "PHL.h" +#include "hero.h" +#include "collision.h" +#include +#include + +void createEffect(int type, int x, int y) +{ + createEffectExtra(type, x, y, 0, 0, 0); +} + +void createEffectExtra(int t, int x, int y, double hsp, double vsp, int val) +{ + int i; + for (i = 0; i < MAX_EFFECTS; i++) { + if (effects[i] == NULL) { + Effect* e = malloc(sizeof *e); + + e->id = i; + e->type = t; + + e->x = x; + e->y = y; + + e->vsp = vsp; + e->hsp = hsp; + e->grav = 0; + + e->imageIndex = 0; + e->imageSpeed = 0; + + e->cropx = 0; + e->cropy = 0; + + e->width = 40; + e->height = 40; + + e->image = imgMisc20; + e->timer = 60; + + + e->visible = 1; + e->val1 = 0; + + e->loop = 0; + e->frames = 0; + + e->depth = 1; + + //Sword collision + if (e->type == 1) { + e->cropx = 440; + e->cropy = 40; + e->imageSpeed = 0.25; + e->timer = 19; + } + + //Enemy poof + else if (e->type == 2) { + PHL_PlaySound(sounds[sndBom01], CHN_EFFECTS); + e->width = 64; + e->height = 64; + e->imageSpeed = 0.33; + e->timer = 30; + e->image = imgMisc32; + } + + //Dust after landing from a fall - left/right + else if (e->type == 3) { + e->cropx = 320; + e->cropy = 80; + e->hsp = -1; + if (hsp > 0) { + e->hsp = 1; + e->cropx = 0; + } + e->imageSpeed = 0.33; + e->timer = 8 * (1 / e->imageSpeed); + } + + //Block destroy/debris + else if (e->type == 4) { + e->grav = 0.2; + e->loop = 1; + e->frames = 4; + e->timer = 60; + + //Set flash offset + if ((e->hsp > 0 && val == 0) || (e->hsp > 0 && val == 1)) { + e->timer -= 1; + } + + e->imageSpeed = 0.34; + + int size = (rand() % 2) + 1; + + e->cropx = 0; + if (e->hsp < 0) { + e->cropx = 160; + } + if (size == 1) { //Big + e->cropy = 40; + }else{ //Small + e->cropy = 440; + } + + } + + //Chest sparkle + else if (e->type == 5) { + /*e->x -= 20; + e->y -= 20; + e->x += -20 + (rand() % 40) + 1; + e->y += -20 + (rand() % 40) + 1; + */ + e->x -= 20; + e->y -= 20; + e->cropx = 440; + e->cropy = 120; + e->imageSpeed = 0.3; + e->timer = 16; + e->depth = 0; + } + + //Charge orbs + else if (e->type == 6) { + e->x -= 20; + e->y -= 20; + e->cropx = 0; + e->cropy = 200; + + e->val1 = (rand() % 360) + 1; + e->imageSpeed = 0.3; + e->timer = 20; + e->depth = 0; + } + + //Poison bubble + else if (e->type == 7) { + PHL_PlaySound(sounds[sndPi02], CHN_EFFECTS); + e->x -= 30; + e->x += (rand() % 20) + 1; + e->cropx = 280; + e->cropy = 120; + e->timer = 35; + + e->vsp = -2; + e->imageIndex = 0; + e->imageSpeed = 0.16; + e->depth = 0; + } + + //Stone break free + else if (e->type == 8) { + e->image = imgMisc32; + e->cropy = 64; + e->width = 64; + e->height = 64; + + e->imageSpeed = 0.32; + e->timer = 18; + } + + //Tiny stone debris + else if (e->type == 9) { + e->x -= 20; + e->y -= 20; + e->image = imgMisc20; + e->cropy = 40; + e->cropx = 320 + ((rand() % 3) * 40); + e->imageSpeed = 0; + + e->vsp = -2 - (0.25 * (rand() % 8)); + e->hsp = -1 + (0.25 * (rand() % 8)); + e->grav = 0.1; + + e->timer = 60; + e->depth = 0; + } + + //Lava top animation + else if (e->type == 10) { + e->cropy = 40; + e->cropx = 80; + + e->imageSpeed = 0.125; + e->depth = -1; + e->loop = 1; + e->frames = 3; + e->timer = 100; + + e->image = imgTiles; + } + + //Water top animation + else if (e->type == 11) { + e->cropy = 40; + e->cropx = 240; + + e->imageSpeed = 0.125; + e->depth = -1; + e->loop = 1; + e->frames = 4; + e->timer = 100; + + e->image = imgTiles; + } + + //Hero Air Bubble + else if (e->type == 12) { + e->x -= 20; + e->val1 = e->x; //Start x + e->y -= 20; + e->cropx = 440; + e->loop = 1; + e->frames = 2; + e->imageSpeed = 0.2; + e->timer = 120; + e->vsp = -0.5; + e->depth = 0; + } + + //Water splash + else if (e->type == 13) { + e->cropx = 200; + e->imageSpeed = 0.1; + e->timer = 55; + e->grav = 0.1; + } + + //Lava splash + else if (e->type == 14) { + e->cropx = 400; + e->cropy = 200; + e->imageSpeed = 0.1; + e->timer = 55; + e->grav = 0.1; + } + + effects[i] = e; + i = MAX_EFFECTS; + } + } +} + +void effectStep(Effect* e) +{ + e->x += e->hsp; + e->y += e->vsp; + e->vsp += e->grav; + e->imageIndex += e->imageSpeed; + + if (e->loop == 1) { + if (e->imageIndex >= e->frames) { + e->imageIndex -= e->frames; + } + } + + if (e->type == 12) { //Hero Air Bubble + e->x = e->val1 + 5 * sin((e->timer * 5) * 3.14159 / 180); + if (checkTileCollisionXY(4, e->x + 20, e->y + 20) == 0) { + e->timer = 0; + } + } + + if (e->type == 10 || e->type == 11) { //Lava top + e->timer = 100; + } + + if (e->type == 4 || e->type == 9 || e->type == 12) { //Stone Rubble + if (e->timer <= 30 && e->timer % 2 != 0) { + e->visible = 0; + }else{ + e->visible = 1; + } + } + else if (e->type == 6) { //Charge orb + if (e->timer % 2 == 0) { + e->visible = 1; + e->x = herox + ((e->timer * 3) * sin(e->val1 * 3.14159 / 180)) - 20; + e->y = heroy + ((e->timer * 3) * cos(e->val1 * 3.14159 / 180)); + }else{ + e->visible = 0; + } + } + + e->timer -= 1; + if (e->timer <= 0) { + effectDestroy(e->id); + } +} + +void effectDraw(Effect* e) +{ + //if (e->type != 4 || (e->timer > 30 || e->timer % 2 == 0)) { + if (e->visible == 1) { + if (e->type == 7) { //Poison Bubble + int animation[6] = {0, 1, 2, 1, 0, 3}; + PHL_DrawSurfacePart(e->x, e->y, e->cropx + (e->width * (animation[(int)e->imageIndex])), e->cropy, e->width, e->height, images[e->image]); + }else{ + PHL_DrawSurfacePart(e->x, e->y, e->cropx + (e->width * ((int)e->imageIndex)), e->cropy, e->width, e->height, images[e->image]); + } + } +} + +void effectDestroy(int id) +{ + if (effects[id] != NULL) { + free(effects[id]); + } + effects[id] = NULL; +} + +void createRockSmash(int x, int y) +{ + x -= 20; + + int randvsp = (rand() % 3) + 1; + createEffectExtra(4, x, y, -1.5, -2 - randvsp, 0); + + randvsp = (rand() % 3) + 1; + createEffectExtra(4, x, y, -1, -5 - randvsp, 1); + + randvsp = (rand() % 3) + 1; + createEffectExtra(4, x, y, 1.5, -2 - randvsp, 0); + + randvsp = (rand() % 3) + 1; + createEffectExtra(4, x, y, 1, -5 - randvsp, 1); + PHL_PlaySound(sounds[sndBom02], 2); +} + +void createSplash(int x, int y) +{ + double chsp = 0, + cvsp = 0; + + x -= 20; + + chsp = -0.25 - ((rand() % 9) * 0.25); + cvsp = -1.5 - ((rand() % 9) * 0.25); + createEffectExtra(13, x, y, chsp, cvsp, 0); + + chsp = 0.25 + ((rand() % 9) * 0.25); + cvsp = -1.5 - ((rand() % 9) * 0.25); + createEffectExtra(13, x, y, chsp, cvsp, 0); + + chsp = -0.25 - ((rand() % 9) * 0.25); + cvsp = -0.5 - ((rand() % 4) * 0.25); + createEffectExtra(13, x, y, chsp, cvsp, 0); + + chsp = 0.25 + ((rand() % 9) * 0.25); + cvsp = -0.5 - ((rand() % 4) * 0.25); + createEffectExtra(13, x, y, chsp, cvsp, 0); + + PHL_PlaySound(sounds[sndWater01], CHN_EFFECTS); +} + +void createLavaSplash(int x, int y) +{ + double chsp = 0, + cvsp = 0; + + x -= 20; + + chsp = -0.25 - ((rand() % 9) * 0.25); + cvsp = -1.5 - ((rand() % 9) * 0.25); + createEffectExtra(14, x, y, chsp, cvsp, 0); + + chsp = 0.25 + ((rand() % 9) * 0.25); + cvsp = -1.5 - ((rand() % 9) * 0.25); + createEffectExtra(14, x, y, chsp, cvsp, 0); + + chsp = -0.25 - ((rand() % 9) * 0.25); + cvsp = -0.5 - ((rand() % 4) * 0.25); + createEffectExtra(14, x, y, chsp, cvsp, 0); + + chsp = 0.25 + ((rand() % 9) * 0.25); + cvsp = -0.5 - ((rand() % 4) * 0.25); + createEffectExtra(14, x, y, chsp, cvsp, 0); + + PHL_PlaySound(sounds[sndShot07], CHN_EFFECTS); +} \ No newline at end of file diff --git a/src/effect.h b/src/effect.h new file mode 100644 index 0000000..afbc630 --- /dev/null +++ b/src/effect.h @@ -0,0 +1,31 @@ +#ifndef EFFECTS_H +#define EFFECTS_H + +typedef struct { + int id, type; + double x, y, + vsp, hsp, grav, + imageIndex, imageSpeed; + + int cropx, cropy; + int width, height; + int image, timer; + + int visible; + int val1; + int loop, frames; + int depth; +} Effect; + +void createEffect(int type, int x, int y); +void createEffectExtra(int t, int x, int y, double hsp, double vsp, int val); + +void effectStep(Effect* e); +void effectDraw(Effect* e); +void effectDestroy(int id); + +void createRockSmash(int x, int y); +void createSplash(int x, int y); +void createLavaSplash(int x, int y); + +#endif \ No newline at end of file diff --git a/src/enemies/bat.c b/src/enemies/bat.c new file mode 100644 index 0000000..90ff12f --- /dev/null +++ b/src/enemies/bat.c @@ -0,0 +1,172 @@ +#include "bat.h" +#include "../game.h" +#include "../PHL.h" +#include "../hero.h" +#include +#include + +void batStep(Bat* b); +void batDraw(Bat* b); + +void createBat(int x, int y, int type) +{ + int i; + for (i = 0; i < MAX_ENEMIES; i++) { + if (enemies[i] == NULL) { + Enemy* result = /*(Enemy*)*/malloc(sizeof *result); + Bat* b = /*(Bat*)*/malloc(sizeof *b); + b->id = i; + + b->x = b->xstart = x; + b->y = b->ystart = y; + b->type = type; + + b->imageIndex = 5; + b->counter = 0; + b->timer = 0; + b->state = 0; + b->dir = 1; + + result->data = b; + result->enemyStep = batStep; + result->enemyDraw = batDraw; + result->type = 1; + + enemies[i] = result; + i = MAX_ENEMIES; + } + } +} + +void batStep(Bat* b) +{ + //Wait + if (b->state == 0) + { + //Animate + { + b->imageIndex = 5; + } + + //wait for hero to get near + { + if (b->timer <= 0) { + Mask area; + area.circle = 0; + area.unused = 0; + area.x = b->xstart - 60; + area.y = b->ystart; + area.w = 160; area.h = 100; + + if (checkCollisionXY(area, herox, heroy + 20)) { + PHL_PlaySound(sounds[sndPi07], CHN_ENEMIES); + b->state = 1; + b->timer = 270; + if (b->type == 1) { + b->counter = 1; + if (herox < b->x + 20) { + b->dir = -1; + }else{ + b->dir = 1; + } + } + } + }else{ + b->timer -= 1; + } + } + } + //Fly + else if (b->state == 1) + { + //Animate + { + b->imageIndex += 0.25; + if (b->imageIndex >= 5) { + b->imageIndex -= 5; + } + } + + //Rotation angle + { + b->timer += 4; + if (b->timer >= 360) { + b->timer -= 360; + } + } + + //Movement + { + b->y = b->ystart + 30 + (30 * sin(b->timer * 3.14159 / 180)); + //Red bat + if (b->type == 1) { + b->x += 2 * b->dir; + } + } + + //Return to perch + { + if (b->timer == 270) { + if (b->type == 1 && b->counter > 0) { + b->dir *= -1; + b->timer = 270; + b->counter -= 1; + }else{ + b->state = 0; + b->timer = 70; + } + } + } + } + + //Setup Mask + Mask mask; + { + mask.circle = mask.unused = 0; + mask.w = 32; + mask.h = 28; + mask.x = b->x + ((40 - mask.w) / 2); + mask.y = b->y; + } + + //Hit Player + { + if (checkCollision(mask, heroMask)) { + heroHit(10, mask.x + (mask.w / 2)); + } + } + + //Weapon collision + { + int i; + for (i = 0; i < MAX_WEAPONS; i++) { + if (weapons[i] != NULL) { + if (checkCollision(mask, weapons[i]->weaponMask)) { + weaponHit(weapons[i]); + //Death + createEffect(2, b->x - 12, b->y - 6); + spawnCollectable(b->x + 20, b->y); + enemyDestroy(b->id); + + i = MAX_WEAPONS; + } + } + } + } + +} + +void batDraw(Bat* b) +{ + int cropX = 0, + cropY = 120; + + if (b->type == 1) { + cropX = 400; + cropY = 280; + } + + cropX += (int)b->imageIndex * 40; + + PHL_DrawSurfacePart(b->x, b->y - 4, cropX, cropY, 40, 40, images[imgEnemies]); +} \ No newline at end of file diff --git a/src/enemies/bat.h b/src/enemies/bat.h new file mode 100644 index 0000000..bd42156 --- /dev/null +++ b/src/enemies/bat.h @@ -0,0 +1,16 @@ +#ifndef BAT_H +#define BAT_H + +typedef struct { + int id; + double x, y; + int xstart, ystart; + int type; //0 = gray | 1 = red + int dir; + double imageIndex; + int counter, timer, state; +} Bat; + +void createBat(int x, int y, int type); + +#endif \ No newline at end of file diff --git a/src/enemies/batboss.c b/src/enemies/batboss.c new file mode 100644 index 0000000..8ea408d --- /dev/null +++ b/src/enemies/batboss.c @@ -0,0 +1,319 @@ +#include "batboss.h" +#include "../game.h" +#include "../PHL.h" +#include "../hero.h" +#include "heads.h" +#include +#include + +int boss2flag = 5; + +//void updateBatMask(Batboss* b); +void batbossStep(Batboss* b); +void batbossDraw(Batboss* b); + +void createBatboss(int x, int y) +{ + if (flags[boss2flag] == 0) { //have not beaten boss 2 + PHL_FreeSurface(images[imgBoss]); + images[imgBoss] = PHL_LoadQDA("boss03.bmp"); + + int i; + for (i = 0; i < MAX_ENEMIES; i++) { + if (enemies[i] == NULL) { + setBossRoom(); + + Enemy* e = /*(Enemy*)*/malloc(sizeof *e); + Batboss* b = /*(Batboss*)*/malloc(sizeof *b); + b->id = i; + + b->x = x; + b->y = y; + + b->vsp = 0; + b->hsp = 0; + b->grav = 0.1; + + b->imageIndex = 0; + + b->ypos = y; + b->rot = 0; + + b->hp = 35; + + b->invincible = 0; + b->state = 0; + b->timer = 0; + b->mode = 0; //0 for flame, 1 for tornado stomp + + /* + b->mask.unused = b->mask.circle = 0; + b->mask.w = 100; + b->mask.h = 68; + updateBatMask(b); + */ + //Setup phase + b->state = 0; + b->hsp = 2; + b->ypos = b->y - 24; + b->timer = 60; + + e->data = b; + e->enemyStep = batbossStep; + e->enemyDraw = batbossDraw; + e->type = 41; + + enemies[i] = e; + i = MAX_ENEMIES; + } + } + } +} + +void batbossStep(Batboss* b) +{ + char dead = 0; + + //Animate + { + //Wing flap + if (b->state == 0 || b->state == 1 || b->state == 2 || b->state == 5 || b->state == 6) { + b->imageIndex += 0.1; + if (b->imageIndex >= 2) { + b->imageIndex -= 2; + } + } + //Twister + if (b->state == 3 || b->state == 4) { + b->imageIndex += 0.2; + if (b->imageIndex >= 5) { + b->imageIndex -= 3; + } + } + } + + //Counters + { + if (b->timer > 0) { + b->timer -= 1; + } + + if (b->invincible > 0) { + b->invincible -= 1; + } + } + + //Large vertical movement + { + if (b->state == 0 || b->state == 1) { + b->rot += 2; + if (b->rot >= 360) { b->rot -= 360; } + + b->y = b->ypos - (40 * sin(b->rot * 3.14159 / 180)); + } + } + + //Small vertical movement + { + if (b->state == 2) { + b->rot += 2; + if (b->rot >= 360) { b->rot -= 360; } + + b->y = b->ypos - (20 * sin(b->rot * 3.14159 / 180)); + } + } + + //Horizontal movement + if (b->state == 0) { + b->x += b->hsp; + + if (b->x >= 520 || b->x <= 120) { //Hit walls + b->hsp *= -1; + } + + if (b->timer <= 0) { + b->state = 1; + } + } + //Slow to halt + else if (b->state == 1) { + b->x += b->hsp; + + if (b->x >= 520 || b->x <= 120) { //Hit walls + b->hsp *= -1; + } + + double rate = 0.03; + if (b->hsp > 0) { + b->hsp -= rate; + if (b->hsp <= 0) { b->hsp = 0; } + } + else if (b->hsp < 0) { + b->hsp += rate; + if (b->hsp >= 0) { b->hsp = 0; } + } + + if (b->hsp == 0 && b->rot <= 2) { + b->state = 2; + b->timer = 60; + } + } + else if (b->state == 2) { + if (b->timer == 1) { + //Shoot flame + int fx = b->x; + int fy = b->y + 24; + int fangle = (atan2(heroy - fy, fx - (herox - 20)) * 180 / 3.14159) + 270; + createFireball(fx, fy, fangle, b->id); + createFireball(fx, fy, fangle - 15, b->id); + createFireball(fx, fy, fangle + 15, b->id); + PHL_PlaySound(sounds[sndShot03], CHN_ENEMIES); + } + + if (b->timer <= 0 && b->rot <= 2) { + if (b->mode == 0) { + b->state = 0; + b->timer = 60; + b->hsp = 2; + b->mode = 1; + } + else{ + b->mode = 0; + b->state = 3; + b->imageIndex = 2; + b->vsp = -4; + PHL_PlaySound(sounds[sndShot06], CHN_ENEMIES); + } + + if (herox < b->x) { + b->hsp *= -1; + } + } + } + //Stomp + else if (b->state == 3) { + b->y += b->vsp; + b->vsp += b->grav; + if (b->vsp >= 6) { b->vsp = 6; } + + //Hit floor + if (b->y >= 480 - 176) { + b->y = 480 - 176; + b->state = 4; + b->timer = 120; + quakeTimer = 30; + PHL_PlaySound(sounds[sndHit04], CHN_ENEMIES); + b->hsp = 1; + if (b->x > herox) { + b->hsp *= -1; + } + } + } + //Chase + else if (b->state == 4) { + b->x += b->hsp; + + if (b->timer <= 0 || b->x >= 520 || b->x <= 120) { + b->state = 5; + b->timer = 80 + (rand() % 61); + } + } + //Rise + else if (b->state == 5) { + b->y -= 1; + + if (b->timer <= 0) { + b->state = 0; + b->ypos = b->y; + b->rot = 0; + b->timer = 60; + + b->hsp = 2; + if (b->x > herox) { + b->hsp *= -1; + } + } + } + //Death + else if (b->state == 6) { + b->y += 0.2; + + if (b->timer % 12 == 0) { + createEffect(2, b->x - 64 + (rand() % 100), b->y + (rand() % 80)); + } + + if (b->timer <= 0) { + dead = 1; + } + } + + if (b->state != 6) { + //Setup Mask + Mask mask; + { + mask.unused = mask.circle = 0; + if (b->state == 3 || b->state == 4) { + mask.w = 64; + mask.h = 96; + mask.y = b->y; + }else{ + mask.w = 100; + mask.h = 68; + mask.y = b->y + 18; + } + mask.x = b->x - (mask.w / 2); + } + + //Hit Player + { + if (checkCollision(mask, getHeroMask())) { + heroHit(30, b->x); + } + } + + //Weapon collision + { + int i; + for (i = 0; i < MAX_WEAPONS; i++) { + if (weapons[i] != NULL) { + if (weapons[i]->cooldown == 0) { + if (checkCollision(mask, weapons[i]->weaponMask)) { + weaponHit(weapons[i]); + //Hit + b->invincible = 15; + b->hp -= 1; + + i = MAX_WEAPONS; + } + } + } + } + } + + if (b->hp <= 0) { + b->state = 6; + b->timer = 180; + b->invincible = 200; + } + } + + //Destroy object + { + if (dead == 1) { + enemyDestroy(b->id); + bossDefeatedFlag = 1; + roomSecret = 1; + + flags[boss2flag] = 1; + PHL_StopMusic(); + } + } + +} + +void batbossDraw(Batboss* b) +{ + if (b->invincible % 2 == 0) { + PHL_DrawSurfacePart(b->x - 64, b->y, (int)b->imageIndex * 128, 0, 128, 96, images[imgBoss]); + } +} \ No newline at end of file diff --git a/src/enemies/batboss.h b/src/enemies/batboss.h new file mode 100644 index 0000000..efac963 --- /dev/null +++ b/src/enemies/batboss.h @@ -0,0 +1,18 @@ +#ifndef BATBOSS_H +#define BATBOSS_H + +typedef struct { + int id; + double x, y; + double hsp, vsp, grav; + double imageIndex; + double ypos; + double rot; + int hp; + int state, timer, mode; + int invincible; +} Batboss; + +void createBatboss(int x, int y); + +#endif \ No newline at end of file diff --git a/src/enemies/bee.c b/src/enemies/bee.c new file mode 100644 index 0000000..1a6206f --- /dev/null +++ b/src/enemies/bee.c @@ -0,0 +1,214 @@ +#include "bee.h" +#include "../enemy.h" +#include "../game.h" +#include "../hero.h" +#include +#include + +void beeStep(Bee* b); +void beeDraw(Bee* b); + +void createBee(int x, int y, int dir) +{ + int i; + for (i = 0; i < MAX_ENEMIES; i++) { + if (enemies[i] == NULL) { + Enemy* e = /*(Enemy*)*/malloc(sizeof *e); + Bee* b = /*(Bee*)*/malloc(sizeof *b); + b->id = i; + + b->x = x; + b->y = y; + b->xstart = b->x; + b->ystart = b->y; + + b->hsp = 0; + b->vsp = 0; + + b->timer = 0; + b->imageIndex = 0; + b->dir = 1; + b->state = 0; + + b->hoverdir = 180; + + if (dir == 1) { + b->hoverdir = 0; + b->dir = -1; + } + + e->data = b; + e->enemyStep = beeStep; + e->enemyDraw = beeDraw; + e->type = 24; + + enemies[i] = e; + i = MAX_ENEMIES; + } + } +} + +void beeStep(Bee* b) +{ + //Animate + { + b->imageIndex += 0.33; + if (b->imageIndex >= 3) { + b->imageIndex -= 3; + } + } + + //Mindless hovering + if (b->state == 0) + { + b->hoverdir += 2.6; + if (b->hoverdir >= 360) { + b->hoverdir -= 360; + } + + b->dir = 1; + if (b->hoverdir <= 180) { + b->dir = -1; + } + + b->x = b->xstart + (20 * cos(b->hoverdir * 3.14159 /180)); + + //If player is within range + Mask area; + area.unused = area.circle = 0; + area.x = b->x - 80; + area.y = b->y; + area.w = 200; + area.h = 100; + + if (checkCollision(area, getHeroMask())) { + b->state = 1; + + b->dir = 1; + if (b->x + 20 > herox) { + b->dir = -1; + } + + b->hsp = -5.5 * b->dir; + + PHL_PlaySound(sounds[sndBee01], CHN_ENEMIES); + } + } + //Fly backwards + else if (b->state == 1) + { + b->hsp += 0.25 * b->dir; + + if ((b->dir == 1 && b->hsp >= 0) || (b->dir == -1 && b->hsp <= 0)) { + b->hsp = 0; + b->state = 2; + b->vsp = 3.75; + } + } + //Fly downwards + else if (b->state == 2) + { + b->vsp -= 0.1; + if (b->vsp <= 0) { + b->state = 3; + b->vsp = 0; + + b->dir = 1; + if (b->x + 20 > herox) { + b->dir = -1; + } + b->hsp = 3 * b->dir; + } + } + //Fly diaganal + else if (b->state == 3) + { + b->vsp -= 0.1; + + if (b->vsp < -3) { + b->vsp = -3; + } + + if (b->y <= b->ystart) { + b->state = 4; + + b->vsp = 0; + b->y = b->ystart; + + if (b->x < b->xstart) { + b->dir = 1; + }else{ + b->dir = -1; + } + b->hsp = b->dir; + } + } + //Fly back to start + else if (b->state == 4) + { + if ((b->dir == 1 && b->x >= b->xstart) || (b->dir == -1 && b->x <= b->xstart)) { + b->state = 0; + b->hsp = 0; + + b->hoverdir = 0; + } + } + + //Movement + { + b->x += b->hsp; + b->y += b->vsp; + } + + //Setup Mask + Mask mask; + { + mask.circle = mask.unused = 0; + mask.w = 24; + mask.h = 32; + mask.y = b->y + 6; + mask.x = b->x + 14; + if (b->dir == -1) { + mask.x = b->x + 2; + } + } + + //Hit Player + { + if (checkCollision(mask, getHeroMask())) { + heroHit(15, mask.x + (mask.w / 2)); + } + } + + //Weapon Collision + { + int i; + for (i = 0; i < MAX_WEAPONS; i++) { + if (weapons[i] != NULL) { + if (checkCollision(mask, weapons[i]->weaponMask)) { + weaponHit(weapons[i]); + + createEffect(2, b->x - 12, b->y - 6); + spawnCollectable(b->x + 20, b->y); + enemyDestroy(b->id); + + i = MAX_WEAPONS; + } + } + } + } + +} + +void beeDraw(Bee* b) +{ + int cropx = 280; + + if (b->dir == -1) { + cropx += 120; + } + + cropx += (int)b->imageIndex * 40; + + PHL_DrawSurfacePart(b->x, b->y, cropx, 480, 40, 40, images[imgEnemies]); +} \ No newline at end of file diff --git a/src/enemies/bee.h b/src/enemies/bee.h new file mode 100644 index 0000000..1230f71 --- /dev/null +++ b/src/enemies/bee.h @@ -0,0 +1,16 @@ +#ifndef BEE_H +#define BEE_H + +typedef struct { + int id; + double x, y; + int xstart, ystart; + double hsp, vsp; + double imageIndex; + int dir, state, timer; + double hoverdir; +} Bee; + +void createBee(int x, int y, int dir); + +#endif \ No newline at end of file diff --git a/src/enemies/boar.c b/src/enemies/boar.c new file mode 100644 index 0000000..d9582f5 --- /dev/null +++ b/src/enemies/boar.c @@ -0,0 +1,221 @@ +#include "boar.h" +#include "../game.h" +#include "../hero.h" +#include + +void boarStep(Boar* b); +void boarDraw(Boar* b); + +void createBoar(int x, int y) +{ + int i; + for (i = 0; i < MAX_ENEMIES; i++) { + if (enemies[i] == NULL) { + Enemy* e = /*(Enemy*)*/malloc(sizeof *e); + Boar* b = /*(Boar*)*/malloc(sizeof *b); + + b->id = i; + + b->hp = 3; + + b->x = x; + b->y = y; + + b->hsp = 0; + + b->imageIndex = 0; + b->dir = 1; + + b->blink = 0; + + b->state = 0; + b->timer = 0; + + e->data = b; + e->enemyStep = boarStep; + e->enemyDraw = boarDraw; + e->type = 26; + + enemies[i] = e; + i = MAX_ENEMIES; + } + } +} + +void boarStep(Boar* b) +{ + //Setup mask + Mask mask; + { + mask.unused = mask.circle = 0; + mask.w = 32; + mask.h = 28; + mask.x = b->x + ((40 - mask.w) / 2); + mask.y = b->y + 40 - mask.h; + } + + //Blink animation + { + if (b->blink > 0) { + b->blink -= 1; + } + } + + //Patterns + { + //Dance + if (b->state == 0) + { + //Animate + b->imageIndex += 0.15; + if (b->imageIndex >= 8) { + b->imageIndex -= 8; + } + + //if player gets near + Mask area; + area.unused = area.circle = 0; + area.x = b->x - 80; + area.y = b->y - 40; + area.w = 200; + area.h = 80; + + if (checkCollision(area, getHeroMask()) == 1) { + b->state = 1; + b->timer = -1; + b->imageIndex = 0; + b->dir = 1; + if (herox < b->x + 20) { + b->dir = -1; + } + } + } + //Rev up + else if (b->state == 1) + { + b->timer += 1; + + //Play sound + if (b->timer % 10 == 0) { + PHL_PlaySound(sounds[sndShot01], CHN_ENEMIES); + } + + //Create effect + if (b->timer % 16 == 0) { + if (b->dir == 1) { + createEffectExtra(3, b->x + 20 - 30, b->y + 8, -1, 0, 0); + } + if (b->dir == -1) { + createEffectExtra(3, b->x + 20 - 10, b->y + 8, 1, 0, 0); + } + } + + if (b->timer >= 60) { + b->state = 2; + b->hsp = 3; + } + } + //Running + else if (b->state == 2) + { + b->x += b->hsp * b->dir; + mask.x = b->x + ((40 - mask.w) / 2); + + //Collide with wall + if (checkTileCollision(1, mask) == 1) { + b->x -= b->hsp * b->dir; + b->dir *= -1; + } + + //On edge + { + mask.x = b->x + ((40 - mask.w) / 2); + + mask.x += mask.w * b->dir; + mask.y += 1; + + PHL_Rect collide = getTileCollision(1, mask); + if (collide.x == -1) { + collide = getTileCollision(3, mask); + } + if (collide.x == -1) { + b->dir *= -1; + } + + mask.y -= 1; + mask.x = b->x + ((40 - mask.w) / 2); + } + + b->hsp -= 0.05; + if (b->hsp <= 0) { + b->state = 0; + } + } + + //Running animation + if (b->state == 1 || b->state == 2) { + //Animate + b->imageIndex += 0.2; + if (b->imageIndex >= 2) { + b->imageIndex -= 2; + } + } + } + + //Collide with hero + { + if (checkCollision(mask, getHeroMask())) { + heroHit(30, mask.x + (mask.w / 2)); + } + } + + //Weapon collision + { + int i; + for (i = 0; i < MAX_WEAPONS; i++) { + if (weapons[i] != NULL) { + if (weapons[i]->cooldown == 0) { + if (checkCollision(mask, weapons[i]->weaponMask)) { + weaponHit(weapons[i]); + b->hp -= 1; + b->blink = 15; + + //Death + if (b->hp <= 0) { + createEffect(2, b->x - 12, b->y - 12); + spawnCollectable(b->x + 20, b->y); + enemyDestroy(b->id); + } + + i = MAX_WEAPONS; + } + } + } + } + } +} + +void boarDraw(Boar* b) +{ + if (b->blink % 2 == 0) + { + int cropx = 0, cropy = 360; + int drawx = b->x, drawy = b->y; + + //Dance + if (b->state == 0) { + int animation[8] = {0, 1, 2, 1, 0, 3, 4, 3}; + cropx = 160 + (animation[(int)b->imageIndex] * 40); + } + //Charge + else{ + cropx = (int)b->imageIndex * 40; + + if (b->dir == -1) { + cropx += 80; + } + } + + PHL_DrawSurfacePart(drawx, drawy, cropx, cropy, 40, 40, images[imgEnemies]); + } +} \ No newline at end of file diff --git a/src/enemies/boar.h b/src/enemies/boar.h new file mode 100644 index 0000000..8f4a61c --- /dev/null +++ b/src/enemies/boar.h @@ -0,0 +1,18 @@ +#ifndef BOAR_H +#define BOAR_H + +typedef struct { + int id; + int hp; + double x, y; + double hsp; + double imageIndex; + int blink; + int dir; + int state; + int timer; +} Boar; + +void createBoar(int x, int y); + +#endif \ No newline at end of file diff --git a/src/enemies/boomknight.c b/src/enemies/boomknight.c new file mode 100644 index 0000000..3fc3e83 --- /dev/null +++ b/src/enemies/boomknight.c @@ -0,0 +1,285 @@ +#include "boomknight.h" +#include "../game.h" +#include "../hero.h" +#include + +void boomknightStep(Boomknight* b); +void boomknightDraw(Boomknight* b); + +void boomStep(Boom* b); +void boomDraw(Boom* b); + +void createBoomknight(int x, int y) +{ + int i; + for (i = 0; i < MAX_ENEMIES; i++) { + if (enemies[i] == NULL) { + Enemy* e = malloc(sizeof *e); + Boomknight* b = malloc(sizeof *b); + b->id = i; + + b->hp = 2; + b->blink = 0; + + b->x = x; + b->y = y; + + b->dir = 1; + if (herox < b->x + 20) { + b->dir = -1; + } + + b->imageIndex = 0; + + b->state = 0; + b->timer = 0; + + e->data = b; + e->enemyStep = boomknightStep; + e->enemyDraw = boomknightDraw; + e->type = 31; + + enemies[i] = e; + i = MAX_ENEMIES; + } + } +} + +void boomknightStep(Boomknight* b) +{ + //Animate + { + b->imageIndex += 0.1; + if (b->imageIndex >= 2) { + b->imageIndex -= 2; + } + + if (b->blink > 0) { + b->blink -= 1; + } + } + + //Setup Mask + Mask mask; + { + mask.circle = mask.unused = 0; + mask.w = 30; + mask.h = 32; + mask.x = b->x + ((40 - mask.w) / 2); + mask.y = b->y + (40 - mask.h); + } + + //Walk + if (b->state == 0) { + //Movement + { + double hsp = 0.5; + b->x += hsp * b->dir; + mask.x = b->x + ((40 - mask.w) / 2); + } + + //Hit wall + { + if (checkTileCollision(1, mask) == 1) { + b->dir *= -1; + } + } + + //On edge + { + mask.x += mask.w * b->dir; + mask.y += 20; + + PHL_Rect collide = getTileCollision(1, mask); + if (collide.x == -1) { + collide = getTileCollision(3, mask); + } + + if (collide.x == -1) { + b->dir *= -1; + } + } + + //Player is close + { + if (b->timer <= 0) { + Mask area; + { + area.circle = area.unused = 0; + area.w = 120; + area.h = 40; + area.x = b->x + 20; + if (b->dir == -1) { + area.x -= area.w; + } + area.y = b->y; + } + if (checkCollision(area, getHeroMask()) == 1) { + b->state = 1; + b->timer = 0; + } + + }else{ + b->timer -= 1; + } + } + + } + + //Throw + else if (b->state == 1) { + //Animate + { + b->imageIndex = 0; + if (b->timer >= 15) { + b->imageIndex = 2; + } + } + + b->timer += 1; + if (b->timer == 15) { + createBoom(b->x, b->y, b->dir); + PHL_PlaySound(sounds[sndPi05], CHN_ENEMIES); + } + + if (b->timer >= 110) { + b->state = 0; + b->imageIndex = 0; + b->timer = 120; + } + } + + //Update Mask + mask.x = b->x + ((40 - mask.w) / 2); + mask.y = b->y + (40 - mask.h); + + //Hero Collision + { + if (checkCollision(mask, getHeroMask()) == 1) { + heroHit(15, mask.x + (mask.w / 2)); + } + } + + //Weapon Collision + { + int i; + for (i = 0; i < MAX_WEAPONS; i++) { + if (weapons[i] != NULL) { + if (weapons[i]->cooldown == 0) { + if (checkCollision(mask, weapons[i]->weaponMask)) { + weaponHit(weapons[i]); + + b->hp -= 1; + b->blink = 15; + + //Death + if (b->hp <= 0) { + createEffect(2, b->x - 12, b->y - 6); + spawnCollectable(b->x + 20, b->y); + enemyDestroy(b->id); + } + + i = MAX_WEAPONS; + } + } + } + } + } + +} + +void boomknightDraw(Boomknight* b) +{ + if (b->blink % 2 == 0) { + int cropX = 400 + ((int)b->imageIndex * 40); + + if (b->dir == -1) { + cropX += 120; + } + + PHL_DrawSurfacePart(b->x, b->y, cropX, 400, 40, 40, images[imgEnemies]); + } +} + + +//Enemy boomerang +void createBoom(int x, int y, int dir) +{ + int i; + for (i = 0; i < MAX_ENEMIES; i++) { + if (enemies[i] == NULL) { + Enemy* e = malloc(sizeof *e); + Boom* b = malloc(sizeof *b); + b->id = i; + + b->dir = dir; + b->x = x; + b->y = y; + + b->hsp = 6 * b->dir; + b->imageIndex = 0; + + b->timer = 90; + + e->data = b; + e->enemyStep = boomStep; + e->enemyDraw = boomDraw; + e->type = -1; + + enemies[i] = e; + i = MAX_ENEMIES; + } + } +} + +void boomStep(Boom* b) +{ + //Animate + { + b->imageIndex += 0.33; + if (b->imageIndex >= 8) { + b->imageIndex -= 8; + } + } + + //Movement + { + b->x += b->hsp; + + double fric = 0.125; + b->hsp -= fric * b->dir; + } + + //Hero collision + { + Mask mask; + { + mask.circle = mask.unused = 0; + mask.w = 24; + mask.h = 24; + mask.x = b->x + ((40 - mask.w) / 2); + mask.y = b->y + ((40 - mask.h) / 2); + } + + if (checkCollision(mask, getHeroMask()) == 1) { + heroHit(10, mask.x + (mask.w / 2)); + } + } + + b->timer -= 1; + if (b->timer <= 0) { + createEffectExtra(5, b->x + 20, b->y + 20, 0, 0, 0); + enemyDestroy(b->id); + } +} + +void boomDraw(Boom* b) +{ + int cropX = (int)b->imageIndex * 40; + + if (b->dir == -1) { + cropX += 320; + } + + PHL_DrawSurfacePart(b->x, b->y, cropX, 360, 40, 40, images[imgMisc20]); +} \ No newline at end of file diff --git a/src/enemies/boomknight.h b/src/enemies/boomknight.h new file mode 100644 index 0000000..b93d31f --- /dev/null +++ b/src/enemies/boomknight.h @@ -0,0 +1,27 @@ +#ifndef BOOMKNIGHT_H +#define BOOMKNIGHT_H + +typedef struct { + int id; + int hp; + int blink; + double x, y; + int dir; + double imageIndex; + int state, timer; +} Boomknight; + +void createBoomknight(int x, int y); + +typedef struct { + int id; + int dir; + double x, y; + double hsp; + double imageIndex; + int timer; +} Boom; + +void createBoom(int x, int y, int dir); + +#endif \ No newline at end of file diff --git a/src/enemies/crab.c b/src/enemies/crab.c new file mode 100644 index 0000000..b12860d --- /dev/null +++ b/src/enemies/crab.c @@ -0,0 +1,509 @@ +#include "crab.h" +#include "../PHL.h" +#include "../enemy.h" +#include "../game.h" +#include "../hero.h" +#include +#include + +int boss3flag = 13; + +void crabStep(Crab* c); +void crabDraw(Crab* c); +void updateCrabMask(Crab* c); +void crabDestroy(Crab* c); + +void electricityStep(Electricity* e); +void electricityDraw(Electricity* e); + +void createCrab(int x, int y) +{ + if (flags[boss3flag] == 0) { //have not beaten boss 3 + PHL_FreeSurface(images[imgBoss]); + images[imgBoss] = PHL_LoadQDA("boss06.bmp"); + + int i; + for (i = 0; i < MAX_ENEMIES; i++) { + if (enemies[i] == NULL) { + //Boss start + setBossRoom(); + + Enemy* e = /*(Enemy*)*/malloc(sizeof(Enemy)); + Crab* c = /*(Crab*)*/malloc(sizeof(Crab)); + c->id = i; + + //c->hp = 1; + c->hp = 35; + c->invincible = 0; + + c->x = x; + c->y = y; + + c->vsp = 0; + c->hsp = 0; + + c->imageIndex = 0; + + c->state = 0; + c->timer = 0; + c->counter = 0; + + c->mask.unused = 0; + c->mask.circle = 1; + c->mask.w = 33; + c->mask.h = 33; + updateCrabMask(c); + + //Setup phase + c->timer = 60; + + e->data = c; + e->enemyStep = crabStep; + e->enemyDraw = crabDraw; + e->type = 42; + + enemies[i] = e; + i = MAX_ENEMIES; + } + } + } +} + +void crabStep(Crab* c) +{ + char dead = 0; + double grav = 0.15; + + if (c->invincible > 0) { + c->invincible -= 1; + } + + //Wait + if (c->state == 0) + { + c->imageIndex = 0; + + if (c->timer <= 0) { + c->timer = 0; + if (c->counter == 2 || c->counter == 5) { //Goto roll + c->state = 3; + if (c->counter == 5) { + c->counter = 0; + }else{ + c->counter = 3; + } + }else if (c->counter == 3) { + c->state = 2; + }else{ + c->state = 1; //Goto shoot + } + }else{ + c->timer -= 1; + } + } + //Shoot Electric orbs + else if (c->state == 1) + { + //Create orbs + if (c->timer == 0) { + PHL_PlaySound(sounds[sndPi05], CHN_ENEMIES); + + double angle = (atan2(heroy + 20 - (c->y + 60), c->x - (herox - 20)) * 180 / 3.14159) + 270; + createElectricity(c->x, c->y + 60, angle - 45, c->id); + createElectricity(c->x, c->y + 60, angle - 22.5, c->id); + createElectricity(c->x, c->y + 60, angle, c->id); + createElectricity(c->x, c->y + 60, angle + 22.5, c->id); + createElectricity(c->x, c->y + 60, angle + 45, c->id); + } + + if (c->timer >= 20) { + c->state = 2; + c->timer = 0; + }else{ + c->timer += 1; + } + } + //Leap + else if (c->state == 2) + { + c->imageIndex = 1; + + //Hopping down or hopping up + int hopup = 1; + if (c->counter > 2) { + hopup = 0; + } + + //Jump + if (c->timer == 0) { + PHL_PlaySound(sounds[sndJump02], CHN_ENEMIES); + + c->vsp = -6.5; + if (hopup == 0) { + c->vsp = -1.5; + } + c->timer = 1; + } + + //Vertical velocity + c->y += c->vsp; + c->vsp += grav; + + if (c->vsp >= 6) { + c->vsp = 6; + } + + //Check if onground + if ((hopup == 1 && c->vsp > 0) || (hopup == 0 && c->vsp >= 6)) { + Mask area; + area.unused = area.circle = 0; + area.w = 40; + area.h = 10; + area.x = c->x - (area.w / 2); + area.y = c->y + (80 - area.h); + + PHL_Rect collide = getTileCollision(1, area); + if (collide.x != -1) { + c->y = collide.y - 80; + c->state = 0; + c->counter += 1; + c->timer = 25; + if (c->counter == 2 || c->counter == 5) { + c->timer = 3; + } + } + } + } + //Roll hop + else if (c->state == 3) + { + //Animate + if (c->hsp > 0) { + c->imageIndex += 0.25; + } + if (c->hsp < 0) { + c->imageIndex -= 0.25; + } + if (c->imageIndex < 2) { c->imageIndex += 4; } + if (c->imageIndex >= 6) { c->imageIndex -= 4; } + + + if (c->timer == 0) { + PHL_PlaySound(sounds[sndHit04], CHN_ENEMIES); + + c->timer = 1; + c->vsp = -1.5; + c->imageIndex = 2; + if (c->x > 320) { + c->hsp = -8; + }else{ + c->hsp = 8; + } + } + + //Movement + c->y += c->vsp; + c->vsp += grav; + + //Check if onground + if (c->vsp > 0) { + Mask area; + area.unused = area.circle = 0; + area.w = 40; + area.h = 10; + area.x = c->x - (area.w / 2); + area.y = c->y + (80 - area.h); + + PHL_Rect collide = getTileCollision(1, area); + if (collide.x != -1) { + c->y = collide.y - 80; + c->state = 4; + } + } + } + //Roll + if (c->state == 4) + { + //Animate + if (c->hsp > 0) { + c->imageIndex += 0.25; + } + if (c->hsp < 0) { + c->imageIndex -= 0.25; + } + if (c->imageIndex < 2) { c->imageIndex += 4; } + if (c->imageIndex >= 6) { c->imageIndex -= 4; } + + //Movement + c->x += c->hsp; + + //Collide with wall + Mask area; + area.unused = area.circle = 0; + area.w = area.h = c->mask.w * 2; + area.x = c->x - c->mask.w; + area.y = c->y + (40 - c->mask.h); + + if (checkTileCollision(1, area) == 1) { + c->state = 5; + c->timer = 0; + } + } + //Bounce off wall + if (c->state == 5) + { + if (c->timer == 0) { + PHL_PlaySound(sounds[sndHit04], CHN_ENEMIES); + + c->timer = 1; + c->vsp = -2; + c->hsp = 2; + if (c->x > 320) { + c->hsp *= -1; + } + } + + c->imageIndex = 1; + + c->x += c->hsp; + + c->y += c->vsp; + c->vsp += grav; + + //Check if onground + if (c->vsp > 0) { + Mask area; + area.unused = area.circle = 0; + area.w = 40; + area.h = 10; + area.x = c->x - (area.w / 2); + area.y = c->y + (80 - area.h); + + PHL_Rect collide = getTileCollision(1, area); + if (collide.x != -1) { + c->y = collide.y - 80; + c->state = 0; + c->timer = 65; + } + } + } + //Death + else if (c->state == 6) + { + c->imageIndex = 1; + + c->y += 0.2; + + c->timer -= 1; + + if (c->timer % 12 == 0) { + createEffect(2, c->x - 64 + (rand() % 100), c->y + (rand() % 80)); + } + + if (c->timer <= 0) { + crabDestroy(c); + dead = 1; + } + } + + if (dead == 0) { + if (c->state != 6) { + //Update Mask + c->mask.x = c->x; + c->mask.y = c->y + 40; + + //Weapon collision + int i; + for (i = 0; i < MAX_WEAPONS; i++) { + if (weapons[i] != NULL) { + if (weapons[i]->cooldown == 0) { + if (checkCollision(c->mask, weapons[i]->weaponMask)) { + weaponHit(weapons[i]); + c->invincible = 15; + c->hp -= 1; + i = MAX_WEAPONS; + } + } + } + } + + //Hit Player + if (checkCollision(c->mask, getHeroMask())) { + heroHit(30, c->x); + } + + //Die + if (c->hp <= 0) { + c->state = 6; + c->timer = 180; + c->invincible = 200; + } + } + } +} + +void crabDraw(Crab* c) +{ + if (c->invincible % 2 == 0) { + PHL_DrawSurfacePart(c->x - 40, c->y, (int)c->imageIndex * 80, 0, 80, 80, images[imgBoss]); + } +} + +void updateCrabMask(Crab* c) +{ + c->mask.x = c->x; + c->mask.y = c->y + 40; +} + +void crabDestroy(Crab* c) +{ + enemyDestroy(c->id); + bossDefeatedFlag = 1; + roomSecret = 1; + + flags[boss3flag] = 1; + PHL_StopMusic(); +} + + +void createElectricity(int x, int y, double angle, int minid) +{ + int i; + for (i = minid; i < MAX_ENEMIES; i++) { + if (enemies[i] == NULL) { + Enemy* e = /*(Enemy*)*/malloc(sizeof *e); + Electricity* el = /*(Electricity*)*/malloc(sizeof *el); + el->id = i; + + el->x = x; + el->y = y; + + //Fix angle + if (angle < 0) { + angle += 360; + } + if (angle >= 360) { + angle -= 360; + } + + el->angle = angle; + el->imageIndex = 0; + + el->mask.unused = 0; + el->mask.circle = 1; + el->mask.w = 16; + el->mask.h = 16; + el->mask.x = x; + el->mask.y = y; + + e->data = el; + e->enemyStep = electricityStep; + e->enemyDraw = electricityDraw; + e->type = -1; + + enemies[i] = e; + i = MAX_ENEMIES; + } + } + /* + int thisid = -1; + int i; + for (i = 0; i < MAX_ENEMIES; i++) { + if (enemies[i] == NULL) { + if (i <= minid) { + thisid = i; + i = MAX_ENEMIES; + }else{ + i = MAX_ENEMIES; + } + } + } + + if (thisid == -1) { + for (i = minid + 1; i < MAX_ENEMIES; i++) { + if (enemies[i] == NULL) { + enemies[i] = enemies[minid]; + Crab* c = enemies[i]->data; + c->id = i; + thisid = minid; + i = MAX_ENEMIES; + } + } + } + + if (thisid != -1) { + Enemy* e = (Enemy*)malloc(sizeof(Enemy)); + Electricity* el = (Electricity*)malloc(sizeof(Electricity)); + el->id = thisid; + + el->x = x; + el->y = y; + + //Fix angle + if (angle < 0) { + angle += 360; + } + if (angle >= 360) { + angle -= 360; + } + + el->angle = angle; + el->imageIndex = 0; + + el->mask.unused = 0; + el->mask.circle = 1; + el->mask.w = 16; + el->mask.h = 16; + el->mask.x = x; + el->mask.y = y; + + e->data = el; + e->enemyStep = electricityStep; + e->enemyDraw = electricityDraw; + e->type = -1; + + enemies[thisid] = e; + } + */ +} + +void electricityStep(Electricity* e) +{ + double spd = 3; + e->x += spd * sin(e->angle * 3.14159 / 180); + e->y += spd * cos(e->angle * 3.14159 / 180); + + //Update Mask + e->mask.x = e->x; + e->mask.y = e->y; + + //Collide with Shield + if (checkCollision(shieldMask, e->mask) == 1) { + createEffect(1, e->x - 20, e->y - 20); + PHL_PlaySound(sounds[sndHit07], CHN_EFFECTS); + enemyDestroy(e->id); + }else{ + //Collide with Hero + if (checkCollision(getHeroMask(), e->mask) == 1) { + if (heroHit(25, e->x) == 1) { + heroStun(); + } + } + } + + //Animate + e->imageIndex += 0.25; + if (e->imageIndex >= 3) { + e->imageIndex -= 3; + } + + //Outside of screen + if (e->x < -20 || e->x > 660 || e->y < -20 || e->y > 500) { + enemyDestroy(e->id); + } +} + +void electricityDraw(Electricity* e) +{ + PHL_DrawSurfacePart(e->x - 20, e->y - 20, 40 + ((int)e->imageIndex * 40), 0, 40, 40, images[imgMisc20]); +} \ No newline at end of file diff --git a/src/enemies/crab.h b/src/enemies/crab.h new file mode 100644 index 0000000..15e2b59 --- /dev/null +++ b/src/enemies/crab.h @@ -0,0 +1,30 @@ +#ifndef CRAB_H +#define CRAB_H + +#include "../collision.h" + +typedef struct { + int id; + int hp, invincible; + double x, y; + double hsp, vsp; + double imageIndex; + int state, timer, counter; + + Mask mask; +} Crab; + +void createCrab(int x, int y); + +typedef struct { + int id; + double x, y; + double angle; + double imageIndex; + + Mask mask; +} Electricity; + +void createElectricity(int x, int y, double angle, int minid); + +#endif \ No newline at end of file diff --git a/src/enemies/devil.c b/src/enemies/devil.c new file mode 100644 index 0000000..ae22e98 --- /dev/null +++ b/src/enemies/devil.c @@ -0,0 +1,458 @@ +#include "devil.h" +#include "../game.h" +#include "../PHL.h" +#include "../hero.h" +#include +#include + +int boss6flag = 31; + +void devilStep(Devil* d); +void devilDraw(Devil* d); + +void orbStep(Orb* o); +void orbDraw(Orb* o); + +void createDevil(int x, int y) +{ + if (flags[boss6flag] == 0) { //have not beaten boss 6 + PHL_FreeSurface(images[imgBoss]); + images[imgBoss] = PHL_LoadQDA("boss04.bmp"); + + int i; + for (i = 0; i < MAX_ENEMIES; i++) { + if (enemies[i] == NULL) { + setBossRoom(); + + Enemy* e = /*(Enemy*)*/malloc(sizeof *e); + Devil* d = /*(Devil*)*/malloc(sizeof *d); + d->id = i; + + d->x = x; + d->y = y; + + d->ystart = d->y; + d->newystart = d->ystart; + d->hsp = -2.5; + + d->hp = 100; + //d->hp = 1; + + d->state = 0; + d->timer = 0; + + d->blink = 0; + d->boblen = 32; + d->bobspd = 3; + + d->tailangle = 90; + + d->rotcounter = 0; + d->bobcounter = 0; + + d->bobspd = 3; + d->rotspd = 1; + + d->imageIndex = 0; + + e->data = d; + e->enemyStep = devilStep; + e->enemyDraw = devilDraw; + e->type = 45; + + enemies[i] = e; + i = MAX_ENEMIES; + } + } + } +} + +void devilStep(Devil* d) +{ + char dead = 0; + + //Animate + { + d->imageIndex += 0.1; + if (d->imageIndex >= 2) { + d->imageIndex -= 2; + } + + if (d->blink > 0) { + d->blink -= 1; + } + } + + //Bob + { + if (d->state != 4) { + d->bobcounter += d->bobspd; + if (d->bobcounter >= 360) { + d->bobcounter -= 360; + } + + d->y = d->ystart + (d->boblen * cos(d->bobcounter * 3.14159 / 180)); + } + } + + //Swing tail + { + d->rotcounter += d->rotspd; + if (d->rotcounter >= 360) { + d->rotcounter -= 360; + } + + d->tailangle = 90 + (55 * cos(d->rotcounter * 3.14159 / 180)); + } + + //Patterns + { + //movement + if (d->state == 0 || d->state == 2) + { + d->rotspd = 1; + d->boblen = 32; + d->bobspd = 3; + + //Re-align ystart + if (d->ystart > d->newystart) { + d->ystart -= 1; + } + if (d->ystart < d->newystart) { + d->ystart += 1; + } + + d->x += d->hsp; + + //Slow Down + double rate = 0.016; + if (d->hsp < 0) { + d->hsp += rate; + if (d->hsp >= 0) { + d->hsp = 0; + } + } + + if (d->hsp > 0) { + d->hsp -= rate; + if (d->hsp <= 0) { + d->hsp = 0; + } + } + + if (d->hsp == 0) { + d->timer = 0; + if (d->state == 0) { + d->state = 1; + } + + if (d->state == 2) { + if ((d->rotcounter >= 90 && d->rotcounter <= 90 + d->rotspd) || (d->rotcounter >= 270 && d->rotcounter <= 270 + d->rotspd)) { + d->state = 3; + } + } + } + } + //mid room pause + else if (d->state == 1) + { + d->timer += 1; + if (d->timer >= 60) { + if (d->state == 1) { + d->hsp = 2.5; + if (herox < d->x) { + d->hsp *= -1; + } + } + d->state = 2; + } + } + //Shoot + else if (d->state == 3) + { + d->rotspd = 3; + d->boblen = 10; + d->bobspd = 10; + + d->timer += 1; + + //Shoot orbs + if (d->timer == 120 || d->timer == 240 || d->timer == 360) { + int aim = (atan2((heroy + 20) - d->y, d->x - herox) * 180 / 3.14159) + 270; + + int spawnY = d->y + 20; + createOrb(d->x, spawnY, aim + 22); + createOrb(d->x, spawnY, aim + 11); + createOrb(d->x, spawnY, aim); + createOrb(d->x, spawnY, aim - 11); + createOrb(d->x, spawnY, aim - 22); + + PHL_PlaySound(sounds[sndShot03], CHN_ENEMIES); + } + + if (d->timer == 360) { + d->state = 0; + d->hsp = 2.5; + + if (d->x > 320) { + d->hsp *= -1; + } + + int chaseY = heroy - d->ystart; + if (chaseY > 52) { chaseY = 52; } + if (chaseY < -52) { chaseY = -52; } + + d->newystart = d->ystart + chaseY; + } + } + + //Death + if (d->state == 4) { + d->rotspd = 3; + d->y += 0.2; + d->timer -= 1; + + if (d->timer % 12 == 0) { + createEffect(2, d->x - 64 + (rand() % 100), d->y - 64 + (rand() % 80)); + } + + if (d->timer <= 0) { + dead = 1; + } + } + } + + //Collisions + if (d->state != 4) { + //Setup masks + Mask masks[6]; + + //Head mask + masks[0].unused = masks[0].circle = 0; + masks[0].w = 100; + masks[0].h = 104; + masks[0].x = d->x - (masks[0].w / 2); + masks[0].y = d->y - (masks[0].h / 2); + + //Link masks + for (int i = 1; i < 5; i++) { + int taildis[4] = {54, 80, 108, 134}; + int taillag[4] = {10, 15, 10, 5}; + + double newtailangle = 90 + (55 * cos((d->rotcounter - taillag[i-1]) * 3.14159 / 180)); + + masks[i].unused = 0; + masks[i].circle = 1; + masks[i].w = 16; + masks[i].h = 16; + masks[i].x = d->x + (taildis[i-1] * cos(newtailangle * 3.14159 / 180)); + masks[i].y = d->y + (taildis[i-1] * sin(newtailangle * 3.14159 / 180)); + } + + //Barb mask + masks[5].unused = masks[5].circle = 0; + masks[5].w = 40; + masks[5].h = 40; + masks[5].x = (d->x + (160 * cos(d->tailangle * 3.14159 / 180))) - (masks[5].w / 2); + masks[5].y = (d->y + (160 * sin(d->tailangle * 3.14159 / 180))) - (masks[5].h / 2); + + //Collisions + int hitHead = 0; + for (int a = 0; a < 6; a++) + { + if (a == 0 || a == 5) { + //Hit Player + if (checkCollision(masks[a], getHeroMask())) { + + int damage = 25; + if (a == 0) { + damage = 50; + } + + if (heroHit(damage, masks[a].x + (masks[a].w / 2)) == 1) { + if (a == 5) { //Barb + heroStun(); + } + } + } + } + + //Weapon collision + if (hitHead == 0) { + for (int i = 0; i < MAX_WEAPONS; i++) { + if (weapons[i] != NULL) { + if (weapons[i]->cooldown == 0) { + if (checkCollision(masks[a], weapons[i]->weaponMask)) { + weaponHit(weapons[i]); + + //Head + if (a == 0) { + d->hp -= 1; + d->blink = 15; + hitHead = 1; + + if (d->hp <= 0) { + d->state = 4; + d->timer = 180; + d->blink = 200; + } + }else{ + PHL_PlaySound(sounds[sndHit03], CHN_WEAPONS); + } + + i = MAX_WEAPONS; + } + } + } + } + } + } + } + + //Destroy + if (dead == 1) { + enemyDestroy(d->id); + bossDefeatedFlag = 1; + roomSecret = 1; + + flags[boss6flag] = 1; + PHL_StopMusic(); + } +} + +void devilDraw(Devil* d) +{ + if (d->blink % 2 == 0) + { + int dx, dy; + + //Draw tail + int taildis[4] = {54, 80, 108, 134}; + int taillag[4] = {10, 15, 10, 5}; + for (int i = 0; i < 4; i++) { + double newtailangle = 90 + (55 * cos((d->rotcounter - taillag[i]) * 3.14159 / 180)); + + dx = d->x + (taildis[i] * cos(newtailangle * 3.14159 / 180)) - 32; + dy = d->y + (taildis[i] * sin(newtailangle * 3.14159 / 180)) - 32; + PHL_DrawSurfacePart(dx, dy, 0, 128, 64, 64, images[imgBoss]); + } + + //Draw Head + dx = d->x - 64; + dy = d->y - 64; + PHL_DrawSurfacePart(dx, dy, (int)d->imageIndex * 128, 0, 128, 128, images[imgBoss]); + + //Draw Tail Tip + dx = d->x + (160 * cos(d->tailangle * 3.14159 / 180)) - 32; + dy = d->y + (160 * sin(d->tailangle * 3.14159 / 180)) - 32; + PHL_DrawSurfacePart(dx, dy, 64, 128, 64, 64, images[imgBoss]); + + } +} + + +//Stone Orbs +void createOrb(int x, int y, double dir) +{ + int i; + for (i = 0; i < MAX_ENEMIES; i++) { + if (enemies[i] == NULL) { + Enemy* e = /*(Enemy*)*/malloc(sizeof *e); + Orb* o = /*(Orb*)*/malloc(sizeof *o); + o->id = i; + + o->x = x; + o->y = y; + + o->dir = dir; + + o->imageIndex = 0; + + e->data = o; + e->enemyStep = orbStep; + e->enemyDraw = orbDraw; + e->type = -1; + + enemies[i] = e; + i = MAX_ENEMIES; + } + } +} + +void orbStep(Orb* o) +{ + char dead = 0; + + //Animate + { + o->imageIndex += 0.33; + if (o->imageIndex >= 4) { + o->imageIndex -= 4; + } + } + + //Movement + { + int spd = 4; + o->x += spd * sin(o->dir * 3.14159 / 180); + o->y += spd * cos(o->dir * 3.14159 / 180); + } + + //Collision + { + Mask mask; + mask.unused = 0; + mask.circle = 1; + mask.w = 6; + mask.x = o->x; + mask.y = o->y; + + //Collide with shield + /*if (checkCollision(mask, shieldMask)) { + createEffect(1, o->x - 20, o->y - 20); + PHL_PlaySound(sounds[sndHit07], CHN_EFFECTS); + dead = 1; + }else{*/ + //Hit player + if (checkCollision(getHeroMask(), mask)) { + heroStone(); + heroHit(25, mask.x); + } + //} + + //Collide with weapon + int i; + for (i = 0; i < MAX_WEAPONS; i++) { + if (weapons[i] != NULL) { + if (checkCollision(mask, weapons[i]->weaponMask)) { + weaponHit(weapons[i]); + + createEffect(2, o->x - 32, o->y - 32); + dead = 1; + + i = MAX_WEAPONS; + } + } + } + } + + //Destroy if outside of room + { + if (o->x < -20 || o->x > 660 || o->y < -20 || o->y > 500) { + dead = 1; + } + } + + //Finally erase object + { + if (dead == 1) { + enemyDestroy(o->id); + } + } +} + +void orbDraw(Orb* o) +{ + int animation[4] = {0, 1, 0, 2}; + PHL_DrawSurfacePart(o->x - 20, o->y - 20, 440 + (animation[(int)o->imageIndex] * 40), 480, 40, 40, images[imgMisc20]); +} \ No newline at end of file diff --git a/src/enemies/devil.h b/src/enemies/devil.h new file mode 100644 index 0000000..3b6d67d --- /dev/null +++ b/src/enemies/devil.h @@ -0,0 +1,28 @@ +#ifndef DEVIL_H +#define DEVIL_H + +typedef struct { + int id; + double x, y; + double ystart, newystart; + double hsp; + int hp; + int state, timer; + int blink; + int boblen, bobspd; + double tailangle, bobcounter, rotcounter, rotspd; + double imageIndex; +} Devil; + +void createDevil(int x, int y); + +typedef struct { + int id; + double x, y; + double dir; + double imageIndex; +} Orb; + +void createOrb(int x, int y, double dir); + +#endif \ No newline at end of file diff --git a/src/enemies/dodo.c b/src/enemies/dodo.c new file mode 100644 index 0000000..0fc26a4 --- /dev/null +++ b/src/enemies/dodo.c @@ -0,0 +1,587 @@ +#include "dodo.h" +#include "../game.h" +#include "../hero.h" +#include "../PHL.h" +#include "../enemy.h" +#include + +void dodoStep(Dodo* d); +void dodoDraw(Dodo* d); + +Mask updateDodoMask(Dodo* d, Mask mask); +int dodoWallCollision(Dodo* d, Mask mask); + +int boss1flag = 1; + +void createDodo(int x, int y, int flag) +{ + char miniboss = 0; + + if (flag != 0) { + miniboss = 1; + }else{ + flag = boss1flag; + } + + if (flags[flag] == 0) { + PHL_FreeSurface(images[imgBoss]); + images[imgBoss] = PHL_LoadQDA("boss01.bmp"); + + int i; + for (i = 0; i < MAX_ENEMIES; i++) { + if (enemies[i] == NULL) { + + if (miniboss == 0) { + setBossRoom(); + } + + Enemy* e = malloc(sizeof *e); + Dodo* d = malloc(sizeof *d); + d->id = i; + + d->x = x; + d->y = y; + + d->vsp = -6; + d->hsp = 0; + d->grav = 0.2; + + d->onground = 0; + d->dir = -1; + if (herox > d->x) { + d->dir = 1; + } + + d->imageIndex = 0; + + d->timer = 0; + d->state = 2; + + d->hp = 45; + d->blink = 0; + + d->tojump = 1; + d->jumptoggle = 0; + + d->flag = flag; + + e->data = d; + e->enemyStep = dodoStep; + e->enemyDraw = dodoDraw; + e->type = 40; + + enemies[i] = e; + i = MAX_ENEMIES; + } + } + } +} + +void dodoStep(Dodo* d) +{ + char dead = 0; + + //Constants + double fric = 0.06; + + //Animation vars + double imgspd = 0; + int frames = 0; + + //timers + { + if (d->timer > 0) { + d->timer -= 1; + } + + if (d->blink > 0) { + d->blink -= 1; + } + } + + //Setup Mask + Mask mask; + { + mask.circle = mask.unused = 0; + mask.w = 56; + mask.h = 56; + mask = updateDodoMask(d, mask); + } + + //Idle + if (d->state == 0) + { + d->hsp = 0; + d->vsp = 0; + + //Animate + imgspd = 0.1; + frames = 4; + + //End state + if (d->timer <= 0) { + //Go to chase + if (d->tojump == 0) { + d->state = 1; + d->timer = 260; + d->tojump = 1; + } + //Go to windup + else { + d->state = 3; + d->timer = 30; + } + } + + //Fall + { + if (d->onground == 0) { + d->state = 6; + d->imageIndex = 1; + } + } + } + + //Chase + else if (d->state == 1) + { + //Animate + imgspd = 0.2; + frames = 4; + + //Chase + if ( (d->dir == -1 && herox < d->x) || (d->dir == 1 && herox > d->x) ) { + d->hsp += (fric / 2) * d->dir; + + //limit speed + if (d->hsp > 3) { + d->hsp = 3; + } + if (d->hsp < -3) { + d->hsp = -3; + } + } + + //Turn around + else{ + d->hsp -= fric * d->dir; + + //Done slowing down + if ( (d->dir == 1 && d->hsp <= 0) || (d->dir == -1 && d->hsp >= 0) ) { + d->hsp = 0; + d->state = 4; + d->imageIndex = 0; + } + } + + //Stop running + { + if (d->timer <= 0) { + if (d->hsp >= 1 || d->hsp <= -1) { + d->state = 5; + } + } + } + + //Fall + { + if (d->onground == 0) { + d->state = 6; + d->imageIndex = 1; + } + } + + } + + //Turn around + else if (d->state == 4) + { + //Animate + imgspd = 0; + d->imageIndex += 0.2; + + //Done turning around + if (d->imageIndex >= 3) { + d->dir *= -1; + d->state = 1; + d->imageIndex = 0; + } + + //Fall + { + if (d->onground == 0) { + d->state = 6; + d->imageIndex = 1; + } + } + } + + //Jump + if (d->state == 2) + { + //Set image + imgspd = 0; + { + //Fall + d->imageIndex = 0; + + //Jump + if (d->vsp < 0) { + d->imageIndex = 1; + } + } + + //Face hsp + { + if (d->hsp > 0) { + d->dir = 1; + } + if (d->hsp < 0) { + d->dir = -1; + } + } + + //Land + { + if (d->onground == 1) { + d->state = 5; + d->tojump = 0; + + PHL_PlaySound(sounds[sndHit04], CHN_ENEMIES); + quakeTimer = 30; + createEffectExtra(3, d->x - 30, d->y + 50, -1, 0, 0); + createEffectExtra(3, d->x - 10, d->y + 50, 1, 0, 0); + } + } + + } + + //Windup + if (d->state == 3) + { + //Set image + imgspd = 0; + d->imageIndex = 0; + + //Jump + if (d->timer <= 0) { + d->state = 2; + PHL_PlaySound(sounds[sndJump01], CHN_ENEMIES); + if (d->jumptoggle == 0) { + d->jumptoggle = 1; + d->vsp = -3; + d->hsp = 2 * d->dir; + }else{ + d->jumptoggle = 0; + d->hsp = 1.5 * d->dir; + d->vsp = -6; + } + } + } + + //Slide to a stop + else if (d->state == 5) + { + //Friction + { + if (d->hsp > 0) { + d->hsp -= fric; + if (d->hsp <= 0) { + d->hsp = 0; + } + } + else if (d->hsp < 0) { + d->hsp += fric; + if (d->hsp >= 0) { + d->hsp = 0; + } + } + } + + //Go to idle + { + if (d->hsp == 0) { + d->state = 0; + d->timer = 140; + } + } + + } + + //Fall + if (d->state == 6) + { + //Set image + imgspd = 0; + d->imageIndex = 0; + + //Face hsp + { + if (d->hsp > 0) { + d->dir = 1; + } + if (d->hsp < 0) { + d->dir = -1; + } + } + + //Land + { + if (d->onground == 1) { + d->state = 5; + d->tojump = 1; + } + } + + } + + //Death + if (d->state == 7) + { + imgspd = 0.2; + frames = 4; + + //Movement + d->y += 0.2; + + if (d->blink % 12 == 0) { + createEffect(2, d->x - 72 + (rand() % 80), d->y - 12 + (rand() % 76)); + } + + if (d->blink <= 0) { + dead = 1; + } + } + + else{ + //Horizontal movement + { + if (d->hsp != 0) { + d->x += d->hsp; + + //Wall collision + if (d->state != 6) { + if (dodoWallCollision(d, mask) == 1) { + d->hsp *= -1; + if (d->state == 1) { + d->state = 5; + } + } + } + + } + } + + //Vertical movement + { + if (d->vsp != 0) { + d->y += d->vsp; + + mask = updateDodoMask(d, mask); + PHL_Rect collide = getTileCollision(1, mask); + if (collide.x != -1) { + if (d->vsp < 0) { + d->y = collide.y + 40 - (96 - 14 - mask.h); + } + else if (d->vsp > 0) { + d->y = collide.y - 96 + 14; + } + } + } + } + + //Check if onground + mask = updateDodoMask(d, mask); + mask.y += 1; + if (!checkTileCollision(1, mask)) { + d->onground = 0; + }else{ + d->onground = 1; + } + mask.y -= 1; + + //Gravity + { + if (d->onground == 0) { + d->vsp += d->grav; + } + } + + //Weapon collision + { + int i; + for (i = 0; i < MAX_WEAPONS; i++) { + if (weapons[i] != NULL) { + if (weapons[i]->cooldown == 0) { + if (checkCollision(mask, weapons[i]->weaponMask)) { + weaponHit(weapons[i]); + //Hit + d->blink = 15; + d->hp -= 1; + + //Death + if (d->hp <= 0) { + d->state = 7; + d->blink = 180; + } + + i = MAX_WEAPONS; + } + } + } + } + } + + //Hit Player + { + if (checkCollision(mask, getHeroMask())) { + heroHit(30, d->x); + } + } + } + + //Animate + { + if (imgspd != 0) { + d->imageIndex += imgspd; + + if (d->imageIndex >= frames) { + d->imageIndex -= frames; + } + } + } + + //Destroy object + { + if (dead == 1) { + //Is the level 1 boss + if (d->flag == boss1flag) { + bossDefeatedFlag = 1; + PHL_StopMusic(); + } + + roomSecret = 1; + flags[d->flag] = 1; + enemyDestroy(d->id); + } + } + +} + +void dodoDraw(Dodo* d) +{ + //PHL_DrawMask(d->mask); + if (d->blink % 2 == 0) { + int cropX = 0, + cropY = 0; + + int dirW = 0; + + //Idle + if (d->state == 0) { + dirW = 0; + int frame = 0; + + if (d->dir == 1) { + int animation[4] = {0, 6, 7, 6}; + frame = animation[(int)d->imageIndex]; + }else{ + int animation[4] = {3, 8, 9, 8}; + frame = animation[(int)d->imageIndex]; + } + + cropX = frame * 96; + + while (cropX >= 576) { + cropX -= 576; + cropY += 96; + } + } + + //Chase + else if (d->state == 1 || d->state == 7) { + dirW = 288; + int animation[4] = {0, 1, 0, 2}; + + cropX = animation[(int)d->imageIndex] * 96; + } + + //Jump + else if (d->state == 2) { + dirW = 192; + + cropY = 192; + cropX = (int)d->imageIndex * 96; + } + + //Turn around + else if (d->state == 4) { + dirW = 0; + cropY = 288; + + if (d->dir == 1) { + int animation[3] = {0, 1, 2}; + cropX = animation[(int)d->imageIndex] * 96; + }else{ + int animation[3] = {2, 1, 0}; + cropX = animation[(int)d->imageIndex] * 96; + } + } + + //Duck + else if (d->state == 3 || d->state == 5 || d->state == 6) { + dirW = 192; + + cropX = 0; + cropY = 192; + } + + //Direction offset + { + if (dirW != 0 && d->dir == -1) { + cropX += dirW; + } + } + + PHL_DrawSurfacePart(d->x - 48, d->y, cropX, cropY, 96, 96, images[imgBoss]); + } +} + +Mask updateDodoMask(Dodo* d, Mask mask) +{ + mask.x = d->x - (mask.w / 2); + mask.y = d->y + (96 - 14 - mask.h); + + return mask; +} + +int dodoWallCollision(Dodo* d, Mask mask) +{ + int result = 0; + + mask = updateDodoMask(d, mask); + + //Stay inside of room + if (d->x < 24) { + result = 1; + d->x = 24; + } + else if (d->x > 616) { + result = 1; + d->x = 616; + } + else{ + //Tile collision + PHL_Rect collide = getTileCollision(1, mask); + if (collide.x != -1) { + result = 1; + if (d->hsp > 0) { + d->x = collide.x - (mask.w / 2); + }else{ + d->x = collide.x + 40 + (mask.w / 2); + } + } + } + + return result; +} \ No newline at end of file diff --git a/src/enemies/dodo.h b/src/enemies/dodo.h new file mode 100644 index 0000000..8797116 --- /dev/null +++ b/src/enemies/dodo.h @@ -0,0 +1,22 @@ +#ifndef DODO_H +#define DODO_H + +#include "../collision.h" + +typedef struct { + int id; + double x, y; + double vsp, hsp, grav; + int dir, onground; + double imageIndex; + int state, timer, hp; + int blink; + int tojump, jumptoggle; + int flag; + + //Mask mask; +} Dodo; + +void createDodo(int x, int y, int flag); + +#endif \ No newline at end of file diff --git a/src/enemies/dog.c b/src/enemies/dog.c new file mode 100644 index 0000000..6d0783a --- /dev/null +++ b/src/enemies/dog.c @@ -0,0 +1,294 @@ +#include "dog.h" +#include "../game.h" +#include "../hero.h" +#include + +void dogStep(Dog* d); +void dogDraw(Dog* d); + +int hitWall(Dog* d, Mask mask); + +void createDog(int x, int y) +{ + int i; + for (i = 0; i < MAX_ENEMIES; i++) { + if (enemies[i] == NULL) { + Enemy* e = malloc(sizeof *e); + Dog* d = malloc(sizeof *d); + d->id = i; + d->hp = 3; + d->blink = 0; + + d->x = x; + d->y = y; + + d->hsp = 0; + d->vsp = 0; + + d->imageIndex = 0; + + d->dir = 1; + if (herox < d->x) { + d->dir = -1; + } + + d->state = 0; + d->timer = 0; + d->counter = 0; + + e->data = d; + e->enemyStep = dogStep; + e->enemyDraw = dogDraw; + e->type = 30; + + enemies[i] = e; + i = MAX_ENEMIES; + } + } +} + +void dogStep(Dog* d) +{ + double grav = 0.175; + + char onground = 0; + char wallhit = 0; + + //Setup Mask + Mask mask; + { + mask.circle = mask.unused = 0; + mask.w = 32; + mask.h = 32; + mask.x = d->x + ((40 - mask.w) / 2); + mask.y = d->y + (40 - mask.h); + } + + //Blink animation + { + if (d->blink > 0) { + d->blink -= 1; + } + } + + //Horizontal movement + { + d->x += d->hsp; + mask.x = d->x + ((40 - mask.w) / 2); + + //Wall collision + if (hitWall(d, mask) == 1) { + wallhit = 1; + mask.x = d->x + ((40 - mask.w) / 2); + } + } + + //Vertical Movement + { + d->vsp += grav; + d->y += d->vsp; + mask.y = d->y + (40 - mask.h); + + PHL_Rect collide = getTileCollision(1, mask); + if (collide.x == -1) { + collide = getTileCollision(3, mask); + } + if (collide.x != -1) { + //Floor + if (d->vsp >= 0) { + onground = 1; + d->vsp = 0; + d->y = collide.y - 40; + } + //Ceiling + if (d->vsp < 0) { + d->y = collide.y + 40 - (40 - mask.h); + } + mask.y = d->y + (40 - mask.h); + } + } + + //Wait + if (d->state == 0) + { + double fric = 0.1; + + //Animate + { + d->imageIndex += 0.1; + if (d->imageIndex >= 2) { + d->imageIndex -= 2; + } + } + + //Collide with wall + { + if (wallhit == 1 && onground == 1) { + d->hsp *= -1; + } + } + + //Slide to hault + if (d->hsp > 0) { + d->dir = 1; + + d->hsp -= fric; + if (d->hsp <= 0) { + d->hsp = 0; + } + } + if (d->hsp < 0) { + d->dir = -1; + + d->hsp += fric; + if (d->hsp >= 0) { + d->hsp = 0; + } + } + + //Player is close + { + if (d->hsp == 0) { + Mask area; + area.unused = area.circle = 0; + area.w = 220; + area.h = 60; + area.x = d->x - 90; + area.y = d->y - 20; + + if (checkCollision(area, getHeroMask()) == 1) { + d->state = 1; + d->counter = 0; + d->vsp = 1; + } + } + } + + } + + //Hopping + else if (d->state == 1) + { + int spd = 2; + + d->hsp = spd * d->dir; + + //Land on floor + { + if (onground == 1) { + + //Landed + d->counter += 1; + d->vsp = -1.5; + if (d->counter == 3) { + d->vsp = -4; + } + if (d->counter == 4) { + d->state = 0; + d->counter = 0; + d->vsp = 0; + d->hsp = spd * d->dir; + }else{ + PHL_PlaySound(sounds[sndPi05], CHN_ENEMIES); + d->dir = 1; + if (herox < d->x + 20) { + d->dir = -1; + } + } + } + } + + //Animate + { + d->imageIndex = 1; + if (d->vsp < 0) { + d->imageIndex = 2; + } + } + + } + + //Update mask to be safe + mask.x = d->x + ((40 - mask.w) / 2); + mask.y = d->y + (40 - mask.h); + + //Hit Player + { + if (checkCollision(mask, getHeroMask())) { + if (heroHit(10, mask.x + (mask.w / 2)) == 1) { + heroStun(); + } + } + } + + //Weapon collision + { + int i; + for (i = 0; i < MAX_WEAPONS; i++) { + if (weapons[i] != NULL) { + if (weapons[i]->cooldown == 0) { + if (checkCollision(mask, weapons[i]->weaponMask)) { + weaponHit(weapons[i]); + //Hit + d->blink = 15; + d->hp -= 1; + + //Death + if (d->hp <= 0) { + createEffect(2, d->x - 12, d->y - 6); + spawnCollectable(d->x + 20, d->y); + enemyDestroy(d->id); + } + + i = MAX_WEAPONS; + } + } + } + } + } + +} + +void dogDraw(Dog* d) +{ + if (d->blink % 2 == 0) { + int cropX = 240 + ((int)d->imageIndex * 40); + + if (d->dir == -1) { + cropX += 120; + } + + PHL_DrawSurfacePart(d->x, d->y, cropX, 40, 40, 40, images[imgEnemies]); + } +} + +int hitWall(Dog* d, Mask mask) +{ + PHL_Rect collide = getTileCollision(1, mask); + + if (collide.x == -1) { + collide = getTileCollision(3, mask); + } + + if (collide.x != -1) { + int dir = 1; + if (d->hsp < 0) { + dir = -1; + } + d->x = collide.x + 20 - ((20 + (mask.w / 2)) * dir) - 20; + + return 1; + }else{ + if (d->x < -20) { + d->x = -20; + return 1; + } + + if (d->x > 620) { + d->x = 620; + return 1; + } + } + + return 0; +} \ No newline at end of file diff --git a/src/enemies/dog.h b/src/enemies/dog.h new file mode 100644 index 0000000..ee93ce7 --- /dev/null +++ b/src/enemies/dog.h @@ -0,0 +1,17 @@ +#ifndef DOG_H +#define DOG_H + +typedef struct { + int id; + int hp; + int blink; + double x, y; + double vsp, hsp; + double imageIndex; + int dir; + int state, timer, counter; +} Dog; + +void createDog(int x, int y); + +#endif \ No newline at end of file diff --git a/src/enemies/firewheel.c b/src/enemies/firewheel.c new file mode 100644 index 0000000..7368761 --- /dev/null +++ b/src/enemies/firewheel.c @@ -0,0 +1,272 @@ +#include "firewheel.h" +#include "../game.h" +#include "../PHL.h" +#include "../hero.h" +#include + +void firewheelRotate(Firewheel* f, int clockwise); + +void firewheelStep(Firewheel* f); +void firewheelDraw(Firewheel* f); + +void createFirewheel(int x, int y, int dir) +{ + int i; + for (i = 0; i < MAX_ENEMIES; i++) { + if (enemies[i] == NULL) { + Enemy* e = malloc(sizeof *e); + Firewheel* f = malloc(sizeof *f); + + f->id = i; + + f->x = x; + f->y = y; + + f->imageIndex = 0; + + f->hp = 2; + f->blink = 0; + + f->hsp = 1; + f->vsp = 0; + + f->wallx = 0; + f->wally = 1; + + f->timer = 0; + if (x % 40 != 0) { + f->timer = 20; + } + + //Start on ceiling + { + Mask mask; + mask.circle = mask.unused = 0; + mask.w = 40; + mask.h = 40; + mask.x = f->x; + mask.y = f->y + 10; + + PHL_Rect collide = getTileCollision(1, mask); + if (collide.x == -1) { + collide = getTileCollision(3, mask); + } + + if (collide.x == -1) { + f->wally = -1; + f->hsp *= -1; + } + } + + f->dir = 1; + if (dir == 1) { + f->hsp *= -1; + f->dir = -1; + } + + e->data = f; + e->enemyStep = firewheelStep; + e->enemyDraw = firewheelDraw; + e->type = 27; + + enemies[i] = e; + i = MAX_ENEMIES; + } + } +} + +void firewheelStep(Firewheel* f) +{ + //Animate + { + f->imageIndex += 0.33; + if (f->imageIndex >= 4) { + f->imageIndex -= 4; + } + + if (f->blink > 0) { + f->blink -= 1; + } + } + + //Movement + int spd = 2; + f->x += spd * f->hsp; + f->y += spd * f->vsp; + + //Setup mask + Mask mask; + mask.circle = mask.unused = 0; + mask.w = 40; + mask.h = 40; + mask.x = f->x; + mask.y = f->y; + + //Check if ready to change angle + if ( (f->hsp != 0 && (int)f->x % 20 == 0) || (f->vsp != 0 && (int)f->y % 20 == 0) ) + { + int doCheck = 1; + while (doCheck == 1) { + doCheck = 0; + + //Check on edge + mask.x += (f->wallx * 10); + mask.y += (f->wally * 10); + + PHL_Rect collide = getTileCollision(1, mask); + if (collide.x == -1) { + collide = getTileCollision(3, mask); + } + + //Outside of room + if (f->y <= -40) { + collide.x = 1; + } + + //On edge + if (collide.x == -1) { + int tempHsp = f->hsp; + int tempVsp = f->vsp; + f->hsp = f->wallx; + f->vsp = f->wally; + + f->wallx = -tempHsp; + f->wally = -tempVsp; + doCheck = 1; + } + //Hit wall + else { + mask.x = f->x; + mask.y = f->y; + mask.x += f->hsp * 10; + mask.y += f->vsp * 10; + + collide = getTileCollision(1, mask); + if (collide.x == -1) { + collide = getTileCollision(3, mask); + } + + //Outside of room + if (collide.x == -1) { + if (f->y <= -40 && f->vsp != 1) { + collide.x = 1; + } + } + + //Did collide with wall + if (collide.x != -1) { + int tempWallx = f->wallx; + int tempWally = f->wally; + f->wallx = f->hsp; + f->wally = f->vsp; + + f->hsp = -tempWallx; + f->vsp = -tempWally; + + doCheck = 1; + } + + } + } + + /* + mask.x += f->hsp * 10; + mask.y += f->vsp * 10; + + //Collide with wall + PHL_Rect collide = getTileCollision(1, mask); + if (collide.x == -1) { + collide = getTileCollision(3, mask); + } + + //Outside of room + if (collide.x == -1) { + if (mask.y <= 0 && f->vsp < 0) { + collide.x = f->x; + collide.y = -40; + collide.w = 40; + collide.h = 40; + } + } + + //Did collide with wall + if (collide.x != -1) { + int tempWallx = f->wallx; + int tempWally = f->wally; + f->wallx = f->hsp; + f->wally = f->vsp; + + f->hsp = -tempWallx; + f->vsp = -tempWally; + } + //Edge rotate + else{ + mask.x = f->x; + mask.y = f->y; + mask.x += (f->wallx * 10); + mask.y += (f->wally * 10); + + collide = getTileCollision(1, mask); + if (collide.x == -1) { + collide = getTileCollision(3, mask); + } + + if (collide.x == -1) { + int tempHsp = f->hsp; + int tempVsp = f->vsp; + f->hsp = f->wallx; + f->vsp = f->wally; + + f->wallx = -tempHsp; + f->wally = -tempVsp; + } + } +*/ + } + + //Update Mask + mask.w = 30; + mask.h = 30; + mask.x = f->x + 5; + mask.y = f->y + 5; + + //Collide with hero + if (checkCollision(mask, getHeroMask())) { + heroHit(20, mask.x + (mask.w / 2)); + } + + //Weapon collision + for (int i = 0; i < MAX_WEAPONS; i++) { + if (weapons[i] != NULL) { + if (weapons[i]->cooldown == 0) { + if (checkCollision(mask, weapons[i]->weaponMask)) { + weaponHit(weapons[i]); + f->hp -= 1; + f->blink = 15; + + //Death + if (f->hp <= 0) { + createEffect(2, f->x - 12, f->y - 12); + spawnCollectable(f->x + 20, f->y); + enemyDestroy(f->id); + } + + i = MAX_WEAPONS; + } + } + } + } + +} + +void firewheelDraw(Firewheel* f) +{ + if (f->blink % 2 == 0) { + int cy = 80; + if (f->dir == -1) { + cy += 40; + } + + PHL_DrawSurfacePart(f->x, f->y, 480 + ((int)f->imageIndex * 40), cy, 40, 40, images[imgEnemies]); + } +} \ No newline at end of file diff --git a/src/enemies/firewheel.h b/src/enemies/firewheel.h new file mode 100644 index 0000000..7a5c6e4 --- /dev/null +++ b/src/enemies/firewheel.h @@ -0,0 +1,18 @@ +#ifndef FIREWHEEL_H +#define FIREWHEEL_H + +typedef struct { + int id; + double x, y; + double imageIndex; + int hp; + int blink; + int dir; + int hsp, vsp; + int wallx, wally; + int timer; +} Firewheel; + +void createFirewheel(int x, int y, int dir); + +#endif \ No newline at end of file diff --git a/src/enemies/fish.c b/src/enemies/fish.c new file mode 100644 index 0000000..54a9971 --- /dev/null +++ b/src/enemies/fish.c @@ -0,0 +1,139 @@ +#include "fish.h" +#include "../game.h" +#include "../enemy.h" +#include "../PHL.h" +#include "../collision.h" +#include "../hero.h" +#include + +void createFish(int x, int y, int dir) +{ + int i; + for (i = 0; i < MAX_ENEMIES; i++) { + if (enemies[i] == NULL) { + Enemy* e = malloc(sizeof *e); + Fish* f = malloc(sizeof *f); + f->id = i; + + f->x = f->xstart = x; + f->y = y; + + f->imageIndex = 0; + + f->spd = 1; + + f->turning = 0; + f->dir = 1; + if (dir == 1) { + f->dir = -1; + f->spd = -1; + } + + f->mask.circle = f->mask.unused = 0; + f->mask.x = x + 3; + f->mask.y = y + 6; + f->mask.w = 34; + f->mask.h = 32; + + e->data = f; + e->enemyStep = fishStep; + e->enemyDraw = fishDraw; + e->type = 13; + + enemies[i] = e; + i = MAX_ENEMIES; + } + } +} + +void fishStep(Fish* f) +{ + double fric = 0.02; + + f->x += f->spd; + f->mask.x = f->x + 3; + + if (f->turning == 0) { + f->imageIndex += 0.1; + if (f->imageIndex >= 2) { + f->imageIndex -= 2; + } + }else{ + f->imageIndex += 0.25; + if (f->imageIndex >= 3) { + f->turning = 0; + } + } + + if (f->dir == 1) { + if (f->x > f->xstart + 25) { + f->spd -= fric; + + if (f->spd < 0) { + f->dir = -1; + f->turning = 1; + f->imageIndex = 0; + } + }else{ + f->spd += fric; + if (f->spd > 1) { + f->spd = 1; + } + } + }else if (f->dir == -1) { + if (f->x < f->xstart - 25) { + f->spd += fric; + + if (f->spd > 0) { + f->dir = 1; + f->turning = 1; + f->imageIndex = 0; + } + }else{ + f->spd -= fric; + if (f->spd < -1) { + f->spd = -1; + } + } + } + + if (checkCollision(f->mask, getHeroMask())) { + heroHit(15, f->x + 20); + } + + //Weapon collision + int i; + for (i = 0; i < MAX_WEAPONS; i++) { + if (weapons[i] != NULL) { + if (checkCollision(f->mask, weapons[i]->weaponMask)) { + weaponHit(weapons[i]); + createEffect(2, f->x - 12, f->y - 12); + spawnCollectable(f->x + 20, f->y); + enemyDestroy(f->id); + + i = MAX_WEAPONS; + } + } + } +} + +void fishDraw(Fish* f) +{ + int thisImage = 0; + if (f->turning == 1) { + if (f->dir == -1) { + int animation[3] = {4, 6, 5}; + thisImage = animation[(int)f->imageIndex]; + }else{ + int animation[3] = {5, 6, 4}; + thisImage = animation[(int)f->imageIndex]; + } + }else{ + thisImage = f->imageIndex; + if (f->spd < 0) { + thisImage += 2; + } + } + + PHL_DrawSurfacePart(f->x, f->y, 360 + (thisImage * 40), 360, 40, 40, images[imgEnemies]); +} \ No newline at end of file diff --git a/src/enemies/fish.h b/src/enemies/fish.h new file mode 100644 index 0000000..9648bcd --- /dev/null +++ b/src/enemies/fish.h @@ -0,0 +1,24 @@ +#ifndef FISH_H +#define FISH_H + +#include "../enemy.h" +#include "../collision.h" + +typedef struct { + int id; + + double x, y; + int xstart; + double imageIndex; + double spd; + int dir, turning; + + Mask mask; +} Fish; + +void createFish(int x, int y, int dir); + +void fishStep(Fish* f); +void fishDraw(Fish* f); + +#endif \ No newline at end of file diff --git a/src/enemies/garm.c b/src/enemies/garm.c new file mode 100644 index 0000000..b9c59ec --- /dev/null +++ b/src/enemies/garm.c @@ -0,0 +1,578 @@ +#include "garm.h" +#include "../game.h" +#include "../PHL.h" +#include "../hero.h" +#include + +int boss7flag = 47; + +void garmStep(Garm* g); +void garmDraw(Garm* g); + +void garmrockStep(Garmrock* g); +void garmrockDraw(Garmrock* g); + +void createGarm(int x, int y) +{ + if (flags[boss7flag] == 0) { //have not beaten boss 7 + PHL_FreeSurface(images[imgBoss]); + images[imgBoss] = PHL_LoadQDA("boss07.bmp"); + + int i; + for (i = 0; i < MAX_ENEMIES; i++) { + if (enemies[i] == NULL) { + setBossRoom(); + + Enemy* e = malloc(sizeof *e); + Garm* g = malloc(sizeof *g); + + g->id = i; + + g->hp = 105; + //g->hp = 1; + + g->x = x; + g->y = y; + + g->hsp = 0; + g->vsp = 0; + + g->dir = -1; + + g->imageIndex = 0; + + g->state = 0; + g->timer = 0; + + g->blink = 0; + + g->substate = 0; + g->wallcounter = 0; + g->targx = 0; + + e->data = g; + e->enemyStep = garmStep; + e->enemyDraw = garmDraw; + e->type = 46; + + enemies[i] = e; + i = MAX_ENEMIES; + } + } + } +} + +void garmStep(Garm* g) +{ + char dead = 0; + + //Blink animation + { + if (g->blink > 0) { + g->blink -= 1; + } + } + + //Setup Mask + Mask mask; + { + mask.circle = mask.unused = 0; + mask.w = 88; + mask.h = 104; + mask.x = g->x - (mask.w / 2); + mask.y = g->y + (120 - mask.h); + } + + //Stand still + if (g->state == 0) + { + //Animate + { + g->imageIndex += 0.0625; + if (g->imageIndex >= 2) { + g->imageIndex -= 2; + } + } + + //End state + { + g->timer += 1; + if (g->timer >= 60) { + g->state = 1; + //g->vsp = -4.5; + g->counter = 0; + g->timer = 0; + //PHL_PlaySound(sounds[sndPi09], CHN_ENEMIES); + } + } + } + + //Bounce + else if (g->state == 1) + { + //Animate + { + g->imageIndex += 0.33; + if (g->imageIndex >= 3) { + g->imageIndex -= 3; + } + } + + if (g->timer > 0) { + g->vsp = 0; + g->imageIndex = 0; + g->timer -= 1; + if (g->timer <= 0) { + //End state + if (g->counter >= 3) { + g->state = 2; + g->counter = 0; + g->imageIndex = 0; + g->vsp = -6; + g->hsp = 8; + if (g->x > herox) { + g->hsp *= -1; + } + + if (g->substate == 0) { + g->wallcounter = 1; + g->substate = 1; + }else{ + g->wallcounter = 2; + g->substate = 0; + } + }else{ + g->vsp = -5; + } + } + } + + else if (g->timer == 0) { + double grav = 0.25; + + //Movement + if (g->timer == 0) { + g->y += g->vsp; + g->vsp += grav; + mask.y = g->y + (120 - mask.h); + } + + //Land on ground + if (g->vsp >= 0 && g->timer == 0) { + PHL_Rect collide = getTileCollision(1, mask); + if (collide.x != -1) { + g->y = collide.y - 120; + mask.y = g->y + (120 - mask.h); + g->vsp = 0; + g->timer = 3; + g->counter += 1; + PHL_PlaySound(sounds[sndPi09], CHN_ENEMIES); + } + } + } + } + + //Leap towards wall + else if (g->state == 2) + { + double grav = 0.25; + + //Set image + { + if (g->hsp > 0) { + g->imageIndex = 0; + } + + if (g->hsp < 0) { + g->imageIndex = 1; + } + } + + //Movement + { + g->y += g->vsp; + g->vsp += grav; + mask.y = g->y + (120 - mask.h); + + g->x += g->hsp; + mask.x = g->x - (mask.w / 2); + } + + if (g->wallcounter > 0) + { + PHL_Rect collide = getTileCollision(1, mask); + if (collide.x != -1) { + g->wallcounter -= 1; + if (g->hsp < 0) { + g->x = collide.x + 40 + (mask.w / 2); + } + if (g->hsp > 0) { + g->x = collide.x - (mask.w / 2); + } + g->state = 3; + g->timer = 0; + } + } + + //Ground pound + else { + char action = 0; + + if ( (g->hsp > 0 && g->x > g->targx) || (g->hsp < 0 && g->x < g->targx) ) { + action = 1; + } + //Wall collision backup + else{ + PHL_Rect collide = getTileCollision(1, mask); + if (collide.x != -1) { + if (g->hsp < 0) { + g->x = collide.x + 40 + (mask.w / 2); + } + if (g->hsp > 0) { + g->x = collide.x - (mask.w / 2); + } + action = 1; + } + } + + if (action == 1) { + g->state = 4; + g->vsp = -4; + PHL_PlaySound(sounds[sndWolf01], CHN_ENEMIES); + } + } + } + + //Grab wall + else if (g->state == 3) + { + g->timer += 1; + if (g->timer > 5) { + g->state = 2; + g->vsp = -6; + g->hsp *= -1; + PHL_PlaySound(sounds[sndPi09], CHN_ENEMIES); + + g->targx = herox; + + if (g->wallcounter <= 0) { + //Get distance from player + int dis = g->x - g->targx; + { + if (dis < 0) { + dis *= -1; + } + } + + if (dis < 200 || g->substate == 1) { + g->hsp /= 2; + } + } + } + } + + //Ground pound + else if (g->state == 4) + { + double grav = 0.2; + + //Animate + { + g->imageIndex += 0.33; + if (g->imageIndex >= 3) { + g->imageIndex -= 3; + } + } + + g->y += g->vsp; + g->vsp += grav; + mask.y = g->y + (120 - mask.h); + + //Collide with floor + { + PHL_Rect collide = getTileCollision(1, mask); + + if (collide.x != -1) { + g->y = collide.y - 120; + PHL_PlaySound(sounds[sndHit04], CHN_ENEMIES); + quakeTimer = 30; + g->state = 0; + g->timer = -20; + //Create rocks + createGarmrock(g->x + 64, g->y + 100, 2, -4); + createGarmrock(g->x + 34, g->y + 100, 1, -5); + createGarmrock(g->x - 34, g->y + 100, -1, -5); + createGarmrock(g->x - 64, g->y + 100, -2, -4); + + createEffectExtra(3, g->x - 50, g->y + 90, -1, 0, 0); + createEffectExtra(3, g->x + 10, g->y + 90, 1, 0, 0); + } + } + } + + //Dead + if (g->state == 5) { + //Animate + { + g->imageIndex += 0.33; + if (g->imageIndex >= 3) { + g->imageIndex -= 3; + } + } + + g->y += 0.2; + + if (g->blink % 12 == 0) { + createEffect(2, g->x - 64 + (rand() % 100), g->y + 60 - 64 + (rand() % 80)); + } + + if (g->blink <= 0) { + dead = 1; + } + } + + else{ + if (dead == 0) { + //Update Mask + { + mask.x = g->x - (mask.w / 2); + mask.y = g->y + (120 - mask.h); + } + + //Hero collision + { + if (checkCollision(getHeroMask(), mask) == 1) { + heroHit(40, mask.x + (mask.w / 2)); + } + } + + //Weapon collision + { + int i; + for (i = 0; i < MAX_WEAPONS; i++) { + if (weapons[i] != NULL) { + if (weapons[i]->cooldown == 0) { + if (checkCollision(mask, weapons[i]->weaponMask)) { + weaponHit(weapons[i]); + g->hp -= 1; + g->blink = 15; + //Dead + if (g->hp <= 0) { + g->state = 5; + g->blink = 200; + } + + i = MAX_WEAPONS; + } + } + } + } + } + + } + } + + if (dead == 1) { + //Destroy + { + enemyDestroy(g->id); + bossDefeatedFlag = 1; + roomSecret = 1; + + flags[boss7flag] = 1; + PHL_StopMusic(); + } + } + +} + +void garmDraw(Garm* g) +{ + if (g->blink % 2 == 0) { + int cropX = 0, + cropY = 0; + + //Jump Spinning + if ((g->state == 1 && g->timer == 0) || g->state == 4 || g->state == 5) { + cropY = 128; + cropX = 256; + } + + //Jump + if (g->state == 2) { + cropY = 128; + } + + //Wall grab + if (g->state == 3) { + cropX = 384; + } + + cropX += (int)g->imageIndex * 128; + + PHL_DrawSurfacePart(g->x - 64, g->y - 8, cropX, cropY, 128, 128, images[imgBoss]); + } +} + +//Rocks +void createGarmrock(int x, int y, double hsp, double vsp) +{ + int i; + for (i = 0; i < MAX_ENEMIES; i++) { + if (enemies[i] == NULL) { + Enemy* e = malloc(sizeof *e); + Garmrock* g = malloc(sizeof *g); + + g->id = i; + g->hp = 3; + + g->x = x; + g->y = y; + + g->hsp = hsp; + g->vsp = vsp; + + g->imageIndex = 0; + + g->counter = 0; + g->inwall = 0; + g->blink = 0; + + e->data = g; + e->enemyStep = garmrockStep; + e->enemyDraw = garmrockDraw; + e->type = -1; + + enemies[i] = e; + i = MAX_ENEMIES; + } + } +} + +void garmrockStep(Garmrock* g) +{ + char dead = 0; + + //Setup Mask + Mask mask; + { + mask.circle = mask.unused = 0; + mask.w = 44; + mask.h = 44; + mask.x = g->x - (mask.w / 2); + mask.y = g->y - (mask.h / 2); + } + + //Animate + { + g->imageIndex += 0.2; + if (g->imageIndex >= 4) { + g->imageIndex -= 4; + } + + if (g->blink > 0) { + g->blink -= 1; + } + } + + //Horizontal movement + { + g->x += g->hsp; + mask.x = g->x - (mask.w / 2); + + g->inwall = 0; + if (checkTileCollision(1, mask) == 1) { + g->inwall = 1; + } + } + + //Vertical movement + { + double grav = 0.1; + + g->y += g->vsp; + g->vsp += grav; + mask.y = g->y - (mask.h / 2); + + if (g->inwall == 0 && g->counter == 0) { + PHL_Rect collide = getTileCollision(1, mask); + if (collide.x != -1) { + g->counter = 1; + g->y = collide.y - (mask.h / 2); + g->vsp = -2; + PHL_PlaySound(sounds[sndHit06], CHN_ENEMIES); + } + } + } + + //Update mask + { + mask.x = g->x - (mask.w / 2); + mask.y = g->y - (mask.h / 2); + } + + //Hero collision + { + if (checkCollision(getHeroMask(), mask) == 1) { + heroHit(30, mask.x + (mask.w / 2)); + } + } + + //Weapon collision + { + int i; + for (i = 0; i < MAX_WEAPONS; i++) { + if (weapons[i] != NULL) { + if (weapons[i]->cooldown == 0) { + if (checkCollision(mask, weapons[i]->weaponMask)) { + weaponHit(weapons[i]); + g->hp -= 1; + g->blink = 15; + + if (g->hp <= 0) { + dead = 1; + createRockSmash(g->x, g->y + 20); + } + + i = MAX_WEAPONS; + } + } + } + } + } + + //Destroy when out of room + { + if (mask.y > 480) { + dead = 1; + } + } + + //Destroy object + { + if (dead == 1) { + enemyDestroy(g->id); + } + } +} + +void garmrockDraw(Garmrock* g) +{ + if (g->blink % 2 == 0) { + int cropX = 256, + cropY = 192; + + if (g->hsp < 0) { + cropX = 512; + } + + cropX += (int)g->imageIndex * 64; + + while (cropX >= 640) { + cropX -= 640; + cropY += 64; + } + + PHL_DrawSurfacePart(g->x - 32, g->y - 32, cropX, cropY, 64, 64, images[imgMisc32]); + } +} \ No newline at end of file diff --git a/src/enemies/garm.h b/src/enemies/garm.h new file mode 100644 index 0000000..e44a998 --- /dev/null +++ b/src/enemies/garm.h @@ -0,0 +1,31 @@ +#ifndef GARM_H +#define GARM_H + +typedef struct { + int id; + int hp; + double x, y; + double hsp, vsp; + int dir; + double imageIndex; + int state, timer, blink, counter; + int wallcounter, substate; + int targx; +} Garm; + +void createGarm(int x, int y); + +typedef struct { + int id; + int hp; + double x, y; + double vsp, hsp; + double imageIndex; + int counter; + int blink; + int inwall; +} Garmrock; + +void createGarmrock(int x, int y, double hsp, double vsp); + +#endif \ No newline at end of file diff --git a/src/enemies/gas.c b/src/enemies/gas.c new file mode 100644 index 0000000..aee6dbc --- /dev/null +++ b/src/enemies/gas.c @@ -0,0 +1,116 @@ +#include "gas.h" +#include "../PHL.h" +#include "../game.h" +#include "../hero.h" +#include + +void gasStep(Gas* g); +void gasDraw(Gas* g); + +void createGas(int x, int y, int temp) +{ + if (temp == 0 || hasKey[7] == 0) { + int i; + for (i = 0; i < MAX_ENEMIES; i++) { + if (enemies[i] == NULL) { + Enemy* e = malloc(sizeof *e); + Gas* g = malloc(sizeof *g); + g->id = i; + + g->x = x; + g->y = y; + + g->state = 0; + g->timer = 0; + g->imageIndex = 0; + + /* + g->mask.unused = g->mask.circle = 0; + g->mask.w = g->mask.h = 24; + g->mask.x = x + 20 - (g->mask.w / 2); + g->mask.y = y + 40 - g->mask.h; + */ + + e->data = g; + e->enemyStep = gasStep; + e->enemyDraw = gasDraw; + e->type = -1; + + enemies[i] = e; + i = MAX_ENEMIES; + } + } + } +} + +void gasStep(Gas* g) +{ + if (g->state != 0) { + g->imageIndex += 0.2; + } + + if (g->state == 0) { //Wait + Mask tempMask; + tempMask.circle = tempMask.unused = 0; + tempMask.x = g->x - 100; + tempMask.y = g->y - 20; + tempMask.w = 240; + tempMask.h = 60; + + if (checkCollisionXY(tempMask, herox, heroy + 20)) { + g->state = 1; + g->imageIndex = 3; + g->timer = 32; + PHL_PlaySound(sounds[sndGas01], CHN_ENEMIES); + } + } + else if (g->state == 1 || g->state == 3) { //Small puff + if (g->imageIndex >= 5) { + g->imageIndex -= 2; + } + + g->timer -= 1; + if (g->timer <= 0) { + if (g->state == 3) { + g->state = 0; + }else{ + g->state = 2; + g->imageIndex = 0; + g->timer = 175; + } + } + } + else if (g->state == 2) { //Big puff + if (g->imageIndex >= 3) { + g->imageIndex -= 3; + } + + g->timer -= 1; + if (g->timer <= 0) { + g->state = 3; + g->timer = 120; + g->imageIndex = 3; + } + + if (hasItem[7] != 1) { //Does not have gas mask + Mask mask; + mask.unused = mask.circle = 0; + mask.w = mask.h = 24; + mask.x = g->x + 20 - (mask.w / 2); + mask.y = g->y + 40 - mask.h; + + if (checkCollision(getHeroMask(), mask)) { + if (heroHit(15, g->x + 20)) { + heroPoison(); + } + } + } + } +} + +void gasDraw(Gas* g) +{ + if (g->state != 0) { + PHL_DrawSurfacePart(g->x, g->y, (int)g->imageIndex * 40, 400, 40, 40, images[imgEnemies]); + } +} \ No newline at end of file diff --git a/src/enemies/gas.h b/src/enemies/gas.h new file mode 100644 index 0000000..7bd1ebf --- /dev/null +++ b/src/enemies/gas.h @@ -0,0 +1,18 @@ +#ifndef GAS_H +#define GAS_H + +//#include "../enemy.h" +//#include "../collision.h" + +typedef struct { + int id; + int x, y; + int state, timer; + double imageIndex; + + //Mask mask; +} Gas; + +void createGas(int x, int y, int temp); + +#endif \ No newline at end of file diff --git a/src/enemies/ghoul.c b/src/enemies/ghoul.c new file mode 100644 index 0000000..fca1fbd --- /dev/null +++ b/src/enemies/ghoul.c @@ -0,0 +1,218 @@ +#include "ghoul.h" +#include "../game.h" +#include "../enemy.h" +#include "../PHL.h" +#include "../hero.h" +#include + +void ghoulStep(Ghoul* g); +void ghoulDraw(Ghoul* g); + +void createGhoul(int x, int y, int type) +{ + int i; + for (i = 0; i < MAX_ENEMIES; i++) { + if (enemies[i] == NULL) { + Enemy* e = malloc(sizeof *e); + Ghoul* g = malloc(sizeof *g); + g->id = i; + g->hp = 2; + + g->x = x; + g->y = y; + + g->vsp = 0; + g->grav = 0.1; + + g->dir = 0; + g->type = type; + g->onground = 0; + + g->timer = 0; + g->state = 0; + g->invincible = 0; + + g->imageIndex = 0; + + g->mask.circle = 0; + g->mask.unused = 1; + g->mask.w = 24; + g->mask.h = 32; + g->mask.x = g->x + ((40 - g->mask.w) / 2); + g->mask.y = g->y + (40 - g->mask.h); + + e->data = g; + e->enemyStep = ghoulStep; + e->enemyDraw = ghoulDraw; + e->type = 18; + + enemies[i] = e; + i = MAX_ENEMIES; + } + } + +} + +void ghoulStep(Ghoul* g) +{ + if (g->invincible > 0) { + g->invincible -= 1; + } + + if (g->state == 0) { //Wait + Mask area; + area.unused = area.circle = 0; + area.w = 280; + area.h = 80; + area.x = g->x - 120; + area.y = g->y - 20; + + if (checkCollisionXY(area, herox, heroy + 20) == 1) { + g->state = 1; + g->mask.unused = 0; + g->imageIndex = 0; + + g->dir = 1; + if (herox < g->x + 20) { + g->dir = -1; + } + } + } + else if (g->state == 1) { //Pop-up + g->imageIndex += 0.16; + + if (g->imageIndex >= 4) { + g->state = 2; + g->vsp = -1; + g->imageIndex = 0; + PHL_PlaySound(sounds[sndPi05],CHN_ENEMIES); + } + } + else if (g->state == 2) { //Walking + g->mask.unused = 0; + if (g->onground == 0) { + //Vertical movement + g->y += g->vsp; + g->vsp += g->grav; + + g->mask.y = g->y + (40 - g->mask.h); + + PHL_Rect collide = getTileCollision(1, g->mask); + if (collide.x == -1) { + collide = getTileCollision(3, g->mask); + } + if (collide.x != -1) { + g->onground = 1; + g->vsp = 0; + g->y = collide.y - 40; + g->mask.y = g->y + (40 - g->mask.h); + } + } + + g->imageIndex += 0.1; + if (g->imageIndex >= 2) { + g->imageIndex -= 2; + } + + double hsp = 1; + + if ((int)g->imageIndex == 0) { + hsp = 0.5; + } + + //Purple + if (g->type == 1) { + hsp *= 2; + } + + g->x += hsp * g->dir; + g->mask.x = g->x + ((40 - g->mask.w) / 2); + + if (g->onground == 1) { + if ((g->x < -20 || g->x > 660) || checkTileCollision(1, g->mask) == 1) { + g->dir *= -1; + + PHL_Rect collide = getTileCollision(1, g->mask); + if (collide.x != -1) { + g->x = collide.x + (40 * g->dir); + } + } + else { + //check on ledge + g->mask.w = 5; + if (g->dir == 1) { + g->mask.x = g->x + 30; + } + if (g->dir == -1) { + g->mask.x = g->x + 5; + } + g->mask.y += 20; + + if (checkTileCollision(1, g->mask) == 0 && checkTileCollision(3, g->mask) == 0) { + g->dir *= -1; + } + g->mask.w = 24; + g->mask.x = g->x + ((40 - g->mask.w) / 2); + g->mask.y = g->y + (40 - g->mask.h); + } + } + } + + g->mask.x = g->x + ((40 - g->mask.w) / 2); + g->mask.y = g->y + (40 - g->mask.h); + + //Hit Player + { + if (checkCollision(g->mask, getHeroMask())) { + if (heroHit(10, g->x + 20) == 1 && g->type == 1) { + heroPoison(); + } + } + } + + //Weapon Collision + { + int i; + for (i = 0; i < MAX_WEAPONS; i++) { + if (weapons[i] != NULL) { + if (weapons[i]->cooldown == 0) { + if (checkCollision(g->mask, weapons[i]->weaponMask)) { + weaponHit(weapons[i]); + + g->hp -= 1; + g->invincible = 15; + //Death + if (g->hp <= 0) { + createEffect(2, g->x - 12, g->y - 6); + spawnCollectable(g->x + 20, g->y); + enemyDestroy(g->id); + } + i = MAX_WEAPONS; + } + } + } + } + } + +} + +void ghoulDraw(Ghoul* g) +{ + if (g->state != 0 && g->invincible % 2 == 0) { + int cx = (int)g->imageIndex * 40, + cy = 160; + + if (g->state == 1) { + cx += 160; + }else{ + if (g->dir == -1) { + cx += 80; + } + } + + //Purple palette + cy += 160 * g->type; + + PHL_DrawSurfacePart(g->x, g->y, cx, cy, 40, 40, images[imgEnemies]); + } +} \ No newline at end of file diff --git a/src/enemies/ghoul.h b/src/enemies/ghoul.h new file mode 100644 index 0000000..fd6db3a --- /dev/null +++ b/src/enemies/ghoul.h @@ -0,0 +1,22 @@ +#ifndef GHOUL_H +#define GHOUL_H + +#include "../collision.h" + +typedef struct { + int id; + int hp; + double x, y; + double vsp, grav; + int type; + int onground; + int dir; + int state, timer, invincible; + double imageIndex; + + Mask mask; +} Ghoul; + +void createGhoul(int x, int y, int type); + +#endif \ No newline at end of file diff --git a/src/enemies/golem.c b/src/enemies/golem.c new file mode 100644 index 0000000..a372368 --- /dev/null +++ b/src/enemies/golem.c @@ -0,0 +1,199 @@ +#include "golem.h" +#include "../PHL.h" +#include "../hero.h" +#include "../game.h" +#include + +void golemStep(Golem* g); +void golemDraw(Golem* g); + +void createGolem(int x, int y, int dir) +{ + int i; + for (i = 0; i < MAX_ENEMIES; i++) { + if (enemies[i] == NULL) { + Enemy* e = malloc(sizeof *e); + Golem* g = malloc(sizeof *g); + g->id = i; + + g->x = x; + g->y = y; + + g->hp = 4; + + g->dir = 1; + if (dir == 1) { + g->dir = -1; + } + + g->imageIndex = 0; + g->state = 0; + g->blink = 0; + + e->data = g; + e->enemyStep = golemStep; + e->enemyDraw = golemDraw; + e->type = 28; + + enemies[i] = e; + i = MAX_ENEMIES; + } + } +} + +void golemStep(Golem* g) +{ + double imageSpeed = 0.2; + + //Timers + { + if (g->blink > 0) { + g->blink -= 1; + } + } + + //Setup Mask + Mask mask; + { + mask.unused = mask.circle = 0; + mask.w = 36; + mask.h = 36; + mask.x = g->x + ((40 - mask.w) / 2); + mask.y = g->y + (40 - mask.h); + } + + //Rolling + if (g->state == 0) + { + //Animate + { + g->imageIndex += imageSpeed * g->dir; + + if (g->imageIndex >= 8) { + g->imageIndex -= 8; + } + + if (g->imageIndex < 0) { + g->imageIndex += 8; + } + } + + //Movement + double hsp = 1; + { + g->x += hsp * g->dir; + mask.x = g->x + ((40 - mask.w) / 2); + } + + char nextState = 0; + + //Check on ledge + { + mask.x += 30 * g->dir; + mask.y += 10; + + if (checkTileCollision(1, mask) == 0 && checkTileCollision(3, mask) == 0) { + nextState = 1; + } + + mask.x = g->x + ((40 - mask.w) / 2); + mask.y = g->y + (40 - mask.h); + } + + //Collide with wall + { + mask.x += hsp * g->dir; + + if (checkTileCollision(1, mask) == 1) { + nextState = 1; + } + + mask.x = g->x + ((40 - mask.w) / 2); + } + + if (nextState == 1) { + PHL_PlaySound(sounds[sndPi10], CHN_ENEMIES); + g->state = 1; + g->imageIndex = 0; + } + } + + //Forming + else if (g->state == 1) + { + //Animate + { + g->imageIndex += imageSpeed; + + if (g->imageIndex >= 12) { + g->imageIndex = 0; + g->state = 0; + g->dir *= -1; + } + } + + } + + //Hero Collision + { + if (checkCollision(mask, getHeroMask())) { + heroHit(15, mask.x + (mask.w / 2)); + } + } + + //Weapon collision + { + int i; + for (i = 0; i < MAX_WEAPONS; i++) { + if (weapons[i] != NULL) { + if (weapons[i]->cooldown == 0) { + if (checkCollision(mask, weapons[i]->weaponMask)) { + weaponHit(weapons[i]); + + //Tink + if (g->state == 0) { + PHL_PlaySound(sounds[sndHit03], CHN_WEAPONS); + }else{ + g->hp -= 1; + g->blink = 15; + } + + i = MAX_WEAPONS; + } + } + } + } + } + + //Death + { + if (g->hp <= 0) { + createRockSmash(mask.x + (mask.w / 2), mask.y + (mask.h / 2)); + spawnCollectable(g->x + 20, g->y); + enemyDestroy(g->id); + } + } +} + +void golemDraw(Golem* g) +{ + if (g->blink % 2 == 0) { + int cropX = 320, + cropY = 160; + + int drawY = g->y; + + if (g->state == 0) { + cropX += (int)g->imageIndex * 40; + drawY += 2; + }else{ + cropY = 280; + cropX = 240; + + int animation[12] = {0, 1, 2, 3, 3, 3, 3, 3, 3, 2, 1, 0}; + cropX += animation[(int)g->imageIndex] * 40; + } + + PHL_DrawSurfacePart(g->x, drawY, cropX, cropY, 40, 40, images[imgEnemies]); + } +} \ No newline at end of file diff --git a/src/enemies/golem.h b/src/enemies/golem.h new file mode 100644 index 0000000..2653605 --- /dev/null +++ b/src/enemies/golem.h @@ -0,0 +1,16 @@ +#ifndef GOLEM_H +#define GOLEM_H + +typedef struct { + int id; + double x, y; + double imageIndex; + int hp; + int dir; + int state; + int blink; +} Golem; + +void createGolem(int x, int y, int dir); + +#endif \ No newline at end of file diff --git a/src/enemies/gyra.c b/src/enemies/gyra.c new file mode 100644 index 0000000..a09ea9d --- /dev/null +++ b/src/enemies/gyra.c @@ -0,0 +1,332 @@ +#include "gyra.h" +#include "../game.h" +#include "../enemy.h" +#include "../hero.h" +#include +#include + +void gyraStep(Gyra* g); +void gyraDraw(Gyra* g); +void gyraDestroy(Gyra* g); + +int boss4flag = 21; + +void createGyra(int x, int y) +{ + if (flags[boss4flag] == 0) { //have not yet beaten boss 4 + PHL_FreeSurface(images[imgBoss]); + images[imgBoss] = PHL_LoadQDA("boss02.bmp"); + + int i; + for (i = 0; i < MAX_ENEMIES; i++) { + if (enemies[i] == NULL) { + //Boss start + setBossRoom(); + + Enemy* e = malloc(sizeof *e); + Gyra* g = malloc(sizeof *g); + + g->id = i; + g->hp = 50; + //g->hp = 1; + + g->x = x; + g->y = y; + + g->targx = g->x; + g->targy = g->y; + + g->state = 0; + g->timer = 260; + g->counter = 0; + + g->invincible = 0; + g->dir = 0; + g->imageIndex = 0; + + //Setup + g->targx = g->x - 32; + g->targy = g->y + 64; + g->dir = 160; + + g->x = g->targx + (80 * sin(g->dir * 3.14159 / 180)); + g->y = g->targy + (80 * cos(g->dir * 3.14159 / 180)); + + int a; + for (a = 0; a < 144; a++) { + g->xrecord[a] = g->x; + g->yrecord[a] = g->y; + } + + e->data = g; + e->enemyStep = gyraStep; + e->enemyDraw = gyraDraw; + e->type = 43; + + enemies[i] = e; + i = MAX_ENEMIES; + + } + } + } +} + +void gyraStep(Gyra* g) +{ + //Animate + g->imageIndex += 0.1; + if (g->imageIndex >= 2) { + g->imageIndex -= 2; + } + + int pattern[6] = {0, 1, 2, 1, 0, 2}; + + //Move in a circle + if (g->state == 0) + { + int len = 80; + + //Setup + if (g->timer == 0) { + g->targx = g->x + (len * sin((g->dir + 90) * 3.14159 / 180)); + g->targy = g->y + (len * cos((g->dir + 90) * 3.14159 / 180)); + g->dir -= 90; + g->timer = 250; + } + + g->dir += 1.5; + if (g->dir >= 360) { + g->dir -= 360; + } + + g->x = g->targx + (len * sin(g->dir * 3.14159 / 180)); + g->y = g->targy + (len * cos(g->dir * 3.14159 / 180)); + + g->timer -= 1; + if (g->timer <= 0) { + g->counter += 1; + g->state = pattern[g->counter]; + /* + if (g->state != 1 && (g->x < 40 || g->x > 600 || g->y < 40 || g->y > 440)) { + g->state = 1; + }*/ + + g->timer = 0; + } + } + //Attack + else if (g->state == 1) + { + //Setup + if (g->timer == 0) { + g->targx = herox; + g->targy = heroy + 20; + g->dir += 90; + g->timer = 320; + } + + double spd = 2; + double diralt = 1.2; + + double targdir = (atan2(g->targy - g->y, g->x - g->targx) * 180 / 3.14159) + 270; + + targdir = g->dir - targdir; + while (targdir >= 360) { targdir -= 360; } + while (targdir < 0) { targdir += 360; } + + if (targdir > 180) { + g->dir += diralt; + } + if (targdir < 180) { + g->dir -= diralt; + } + + //Movement + g->x += spd * sin(g->dir * 3.14159 / 180); + g->y += spd * cos(g->dir * 3.14159 / 180); + + //Get (close) to targ coords + g->timer -= 1; + if (g->timer <= 0 || sqrt( pow(g->x - g->targx, 2) + pow(g->y - g->targy, 2) ) <= spd * 2) { + g->counter += 1; + if (g->counter >= 5) { + g->counter = 0; + } + g->state = pattern[g->counter]; + g->timer = 0; + } + } + //Oval movement + else if (g->state == 2) + { + int wlen = 120, + hlen = 80; + + //Setup + if (g->timer == 0) { + g->targx = g->x + (wlen * sin((g->dir - 90) * 3.14159 / 180)); + g->targy = g->y + (hlen * cos((g->dir - 90) * 3.14159 / 180)); + g->dir += 90; + g->timer = 200; + } + + g->dir -= 1.5; + if (g->dir < 0) { + g->dir += 360; + } + + g->x = g->targx + (wlen * sin(g->dir * 3.14159 / 180)); + g->y = g->targy + (hlen * cos(g->dir * 3.14159 / 180)); + + g->timer -= 1; + if (g->timer <= 0) { + g->counter += 1; + if (g->counter >= 5) { + g->counter = 0; + } + g->state = pattern[g->counter]; + /* + if (g->state != 1 && (g->x < 40 || g->x > 600 || g->y < 40 || g->y > 440)) { + g->state = 1; + g->timer = 0; + }*/ + } + } + + //Death + if (g->state == 3) + { + g->timer -= 1; + if (g->timer <= 0) { + g->timer = 12; + + int cx = g->xrecord[128 - (g->counter * 16)], + cy = g->yrecord[128 - (g->counter * 16)]; + + createEffect(2, cx - 32, cy - 32); + + g->counter += 1; + if (g->counter == 9) { + gyraDestroy(g); + } + } + }else{ + //Update tail record + int i; + for (i = 142; i >= 0; i--) { + g->xrecord[i + 1] = g->xrecord[i]; + g->yrecord[i + 1] = g->yrecord[i]; + } + g->xrecord[0] = g->x; + g->yrecord[0] = g->y; + + //for (i = 8; i >= 0; i--) { + for (i = 0; i <= 8; i++) { + int cx = g->x, cy = g->y; + + if (i != 0) { + cx = g->xrecord[i * 16]; + cy = g->yrecord[i * 16]; + } + + Mask mask; + mask.unused = 0; + mask.circle = 1; + mask.x = cx; + mask.y = cy; + mask.w = mask.h = 28; + + int a; + for (a = 0; a < MAX_WEAPONS; a++) { + if (weapons[a] != NULL) { + if (weapons[a]->cooldown == 0) { + if (checkCollision(mask, weapons[a]->weaponMask)) { + g->invincible = -15; + weaponHit(weapons[a]); + + if (i == 8) { + g->hp -= 1; + g->invincible = 15; + }else{ + PHL_PlaySound(sounds[sndHit03], CHN_WEAPONS); + } + + a = MAX_WEAPONS; + } + } + } + } + + //Hit player + if (checkCollision(getHeroMask(), mask)) { + if (heroHit(30, mask.x) && i == 0) { + heroPoison(); + } + } + } + + //Death + if (g->hp <= 0) { + g->state = 3; + g->timer = 0; + g->counter = 0; + g->invincible = 200; + } + } + + if (g->invincible > 0) { + g->invincible -= 1; + } + if (g->invincible < 0) { + g->invincible += 1; + } + +} + +void gyraDraw(Gyra* g) +{ + if (g->invincible <= 0 || g->invincible % 2 == 0) { + //Draw Tail Tip + if (g->state != 3 || g->counter <= 0) { + PHL_DrawSurfacePart(g->xrecord[126] - 40, g->yrecord[126] - 40, 320 + ((int)g->imageIndex * 80), 0, 80, 80, images[imgBoss]); + } + + //Draw Tail + int i; + for (i = 7; i > 0; i--) { + if (g->state != 3 || g->counter <= (7 - i) + 1) { + PHL_DrawSurfacePart(g->xrecord[i * 16] - 40, g->yrecord[i * 16] - 40, 160 + ((int)g->imageIndex * 80), 0, 80, 80, images[imgBoss]); + } + } + + //Draw Head + PHL_DrawSurfacePart(g->x - 40, g->y - 40, (int)g->imageIndex * 80, 0, 80, 80, images[imgBoss]); + } + + //PHL_DrawRect(g->targx, g->targy, 10, 10, PHL_NewRGB(255, 255, 255)); + //heroAmmo = g->state; + + /* + int i; + for (i = 8; i >= 0; i--) { + int cx = g->x, cy = g->y; + + if (i != 0) { + cx = g->xrecord[i * 16]; + cy = g->yrecord[i * 16]; + } + + PHL_DrawRect(cx, cy, 10, 10, PHL_NewRGB(255, 255, 255)); + } + */ +} + +void gyraDestroy(Gyra* g) +{ + enemyDestroy(g->id); + bossDefeatedFlag = 1; + roomSecret = 1; + + flags[boss4flag] = 1; + PHL_StopMusic(); +} \ No newline at end of file diff --git a/src/enemies/gyra.h b/src/enemies/gyra.h new file mode 100644 index 0000000..badd7e9 --- /dev/null +++ b/src/enemies/gyra.h @@ -0,0 +1,19 @@ +#ifndef GYRA_H +#define GYRA_H + +typedef struct { + int id; + int hp; + double x, y; + double xrecord[144]; + double yrecord[144]; + int state, timer, counter; + int targx, targy; + int invincible; + double dir; + double imageIndex; +} Gyra; + +void createGyra(int x, int y); + +#endif \ No newline at end of file diff --git a/src/enemies/heads.c b/src/enemies/heads.c new file mode 100644 index 0000000..802d2dc --- /dev/null +++ b/src/enemies/heads.c @@ -0,0 +1,838 @@ +#include "heads.h" +#include "../enemy.h" +#include "../game.h" +#include "../PHL.h" +#include "../hero.h" +#include +#include + +void headStep(Head* h); +void headDraw(Head* h); + +void bulletStep(Bullet* b); +void bulletDraw(Bullet* b); + +void fireballStep(Fireball* f); +void fireballDraw(Fireball* f); + +void laserStep(Laser* l); +void laserDraw(Laser* l); + +void flameStep(Flame* f); +void flameDraw(Flame* f); + +void rockStep(Rock* r); +void rockDraw(Rock* r); + +void airStep(Air* a); +void airDraw(Air* a); + +void createHead(int type, int x, int y, int dir, int offset, int cooloff) +{ + int i; + for (i = 0; i < MAX_ENEMIES; i++) { + if (enemies[i] == NULL) { + Enemy* e = malloc(sizeof *e); + Head* h = malloc(sizeof *h); + + h->id = i; + h->type = type; + + h->x = x; + h->y = y; + + h->state = 0; + + h->hp = 5; + h->invincible = 0; + h->counter = 0; + + h->dir = 1; + if (dir == 1) { + h->dir = -1; + } + + h->timer = 30 * offset; + h->cooloff = 60; + if (cooloff != 0) { + h->cooloff = 30 * cooloff; + } + + e->type = -1; + if (h->type == 0) { + e->type = 4; + h->cooloff = 120; + } + else if (h->type == 1) { + e->type = 6; + } + else if (h->type == 2) { + e->type = 5; + } + else if (h->type == 3) { + e->type = 7; + h->cooloff = 120; + } + else if (h->type == 4) { + e->type = 10; + h->dir = 0; + } + else if (h->type == 5) { + e->type = 25; + h->dir = 0; + } + e->data = h; + e->enemyStep = headStep; + e->enemyDraw = headDraw; + + enemies[i] = e; + i = MAX_ENEMIES; + } + } +} + +void headStep(Head* h) +{ + int RHYNO = 0, + MEDUSA = 1, + DRAGON = 2, + DEMON = 3, + FIRE = 4, + JAR = 5; + + //Setup Mask + Mask mask; + { + mask.circle = mask.unused = 0; + mask.x = h->x; + mask.y = h->y + 1; + mask.w = 40; + mask.h = 39; + } + + //Timers + { + if (h->invincible > 0) { + h->invincible -= 1; + } + + if (h->timer > 0) { + h->timer -= 1; + } + } + + //Wait + if (h->state == 0) + { + char endstate = 0; + + if (h->timer <= 0) { + //Proximity + if (h->type == RHYNO || h->type == DEMON) { + Mask area; + area.circle = area.unused = 0; + area.h = 80; + area.w = 400; + area.y = h->y - 20; + area.x = h->x; + if (h->dir == -1) { + area.x -= area.w - 40; + } + + if (checkCollision(area, getHeroMask()) == 1) { + endstate = 1; + } + }else{ + endstate = 1; + } + } + + //Move onto next state + if (endstate == 1) { + h->state = 1; + h->timer = 30; + } + } + + //Blink + else if (h->state == 1) + { + //Shoot projectile + if (h->timer <= 0) { + //Play Sound + { + int soundtoplay[6] = {sndShot03, sndShot04, sndFire01, sndHit06, sndShot03, sndShot06}; + PHL_PlaySound(sounds[soundtoplay[h->type]], CHN_ENEMIES); + } + + //Set vars + { + h->state = 0; + h->timer = h->cooloff; + } + + //Create projectile + { + //Rhyno head + if (h->type == RHYNO) { + createBullet(mask.x + (mask.w / 2), h->y + 24, h->dir, h->id); + } + //Medusa head + if (h->type == MEDUSA) { + createLaser(h->x, h->y, h->dir); + } + //Dragon head + if (h->type == DRAGON) { + createFlame(h->x + 20 + (20 * h->dir), h->y - 10, h->dir); + } + //Demon head + if (h->type == DEMON) { + createRock(h->x + (20 * h->dir), h->y, h->dir); + } + //Fireball Statue + if (h->type == FIRE) { + createFireball(h->x + 20, h->y + 20, (atan2(heroy - h->y, h->x - (herox - 20)) * 180 / 3.14159) + 270, h->id); + } + //Air Jar + if (h->type == JAR) { + h->state = 3; + h->timer = 12; + h->counter = 0; + } + } + + } + } + + //Air Jar + else if (h->state == 3) + { + if (h->timer <= 0) { + h->counter += 1; + h->timer = 12; + createAir(h->x, h->y - 20); + } + + if (h->counter >= 6) { + h->counter = 0; + h->state = 0; + h->timer = h->cooloff; + } + } + + //Hit player + if (h->type != JAR) { + if (checkCollision(getHeroMask(), mask)) { + heroHit(10, mask.x + (mask.w / 2)); + } + } + + //Weapon collision + int i; + for (i = 0; i < MAX_WEAPONS; i++) { + if (weapons[i] != NULL) { + if (weapons[i]->cooldown == 0) { + if (checkCollision(mask, weapons[i]->weaponMask)) { + h->hp -= 1; + h->invincible = 15; + weaponHit(weapons[i]); + //Death + if (h->hp <= 0) { + createRockSmash(h->x + 20, h->y + 20); + spawnCollectable(h->x + 20, h->y); + enemyDestroy(h->id); + } + + i = MAX_WEAPONS; + } + } + } + } +} + +void headDraw(Head* h) +{ + if (h->invincible % 2 == 0) + { + int sheetX[6] = {0, 320, 160, 240, 560, 400}; + int sheetY[6] = {80, 80, 80, 120, 0, 120}; + + int cropX = sheetX[h->type]; + + int addx[6] = {6, 2, 0, 0, 0, 0}; + int frames = 2; + + //Change dir + if (h->dir == 0) { + frames = 1; + }else{ + frames = 2; + if (h->dir == -1) { + cropX += 40; + } + } + + //White flash + if (h->state == 1 && h->timer % 6 < 3) { + cropX += 40 * frames; + } + + PHL_DrawSurfacePart(h->x - (addx[h->type] * h->dir), h->y, cropX, sheetY[h->type], 40, 40, images[imgEnemies]); + } +} + +//Bullets +void createBullet(int x, int y, int dir, int minid) +{ + int i; + for (i = minid; i < MAX_ENEMIES; i++) { + if (enemies[i] == NULL) { + Enemy* e = malloc(sizeof *e); + Bullet* b = malloc(sizeof *b); + b->id = i; + + b->x = x; + b->y = y; + + b->hsp = dir * 4; + + b->imageIndex = 0; + + e->data = b; + e->enemyStep = bulletStep; + e->enemyDraw = bulletDraw; + e->type = -1; + + enemies[i] = e; + i = MAX_ENEMIES; + } + } +} + +void bulletStep(Bullet* b) +{ + char dead = 0; + + //Movement + { + b->x += b->hsp; + } + + //Create Mask + Mask mask; + { + mask.unused = 0; + mask.circle = 1; + mask.w = mask.h = 10; + mask.x = b->x; + mask.y = b->y; + } + + //Animation + { + if (b->hsp > 0) { + b->imageIndex += 0.33; + }else{ + b->imageIndex -= 0.33; + } + + if (b->imageIndex < 0) { + b->imageIndex += 4; + } + if (b->imageIndex >= 4) { + b->imageIndex -= 4; + } + } + + //Collide with wall + { + if (checkTileCollision(1, mask) == 1) { + createEffect(1, b->x - 20, b->y - 20); + dead = 1; + } + } + + //Collide with hero + { + //Shield collision + if (checkCollision(mask, shieldMask) == 1) { + dead = 1; + createEffect(1, b->x - 20, b->y - 20); + PHL_PlaySound(sounds[sndHit07], CHN_EFFECTS); + } + //Collide with hero + else{ + if (checkCollision(getHeroMask(), mask)) { + heroHit(10, mask.x); + } + } + } + + //Destroy if outside of view + { + if (b->x > 660 || b->x < -20 || b->y < -20 || b->y > 520) { + dead = 1; + } + } + + //Destroy + { + if (dead == 1) { + enemyDestroy(b->id); + } + } +} + +void bulletDraw(Bullet* b) +{ + PHL_DrawSurfacePart(b->x - 20, b->y - 20, 160 + (40 * (int)b->imageIndex), 480, 40, 40, images[imgMisc20]); +} + +//Fireball +void createFireball(int x, int y, int angle, int minid) +{ + //General idea: try to place fireball over spawner + int i; + for (i = minid; i < MAX_ENEMIES; i++) { + if (enemies[i] == NULL) { + Enemy* e = malloc(sizeof *e); + Fireball* f = malloc(sizeof *f); + f->id = i; + + f->x = x; + f->y = y; + + f->spd = 3; + + f->imageIndex = 0; + f->angle = angle; + + f->mask.circle = 1; + f->mask.unused = 0; + f->mask.x = x; + f->mask.y = y; + f->mask.w = f->mask.h = 14; + + e->data = f; + e->enemyStep = fireballStep; + e->enemyDraw = fireballDraw; + e->type = -1; + + enemies[i] = e; + + i = MAX_ENEMIES; + } + } +} + +void fireballStep(Fireball* f) +{ + f->x += (f->spd) * sin(f->angle * 3.14159 / 180); + f->y += (f->spd) * cos(f->angle * 3.14159 / 180); + + f->mask.x = f->x; + f->mask.y = f->y; + + f->imageIndex += 0.5; + if (f->imageIndex >= 8) { + f->imageIndex -= 8; + } + + //Collide with shield + if (checkCollision(f->mask, shieldMask)) { + createEffect(1, f->x - 20, f->y - 20); + PHL_PlaySound(sounds[sndHit07], CHN_EFFECTS); + enemyDestroy(f->id); + }else{ + //Hit player + if (checkCollision(getHeroMask(), f->mask)) { + heroHit(10, f->mask.x); + } + //Destroy if outside of view + if (f->x > 660 || f->x < -20 || f->y < -20 || f->y > 520) { + enemyDestroy(f->id); + } + } +} + +void fireballDraw(Fireball* f) +{ + PHL_DrawSurfacePart(f->x - 20, f->y - 20, 320 + (40 * (int)f->imageIndex), 440, 40, 40, images[imgMisc20]); +} + +//Laser +void createLaser(int x, int y, int dir) +{ + int i; + for (i = 0; i < MAX_ENEMIES; i++) { + if (enemies[i] == NULL) { + Enemy* e = malloc(sizeof *e); + Laser* l = malloc(sizeof *l); + l->id = i; + + l->x = x; + l->y = y; + + l->dir = dir; + l->imageIndex = 0; + + l->mask.circle = l->mask.unused = 0; + l->mask.x = x; + l->mask.y = y + 17; + l->mask.w = 40; + l->mask.h = 6; + + e->data = l; + e->enemyStep = laserStep; + e->enemyDraw = laserDraw; + e->type = -1; + + enemies[i] = e; + i = MAX_ENEMIES; + } + } +} + +void laserStep(Laser* l) +{ + char dead = 0; + + l->x += l->dir * 10; + l->mask.x = l->x; + + l->imageIndex += 0.34; + if (l->imageIndex >= 2) { + l->imageIndex -= 2; + } + + if (checkCollision(shieldMask, l->mask)) { //Hit shield + PHL_PlaySound(sounds[sndHit07], CHN_EFFECTS); + createEffect(1, l->x + (20 * l->dir), l->y); + enemyDestroy(l->id); + dead = 1; + }else if (checkCollision(getHeroMask(), l->mask)) { + heroStone(); + heroHit(15, l->x + 20); + } + + if (dead == 0) { + if (checkTileCollision(1, l->mask)) { + createEffect(1, l->x + (20 * l->dir), l->y); + enemyDestroy(l->id); + dead = 1; + } + + if (dead == 0) { + if (l->mask.x > 640 || l->mask.x + l->mask.w <= 0) { + enemyDestroy(l->id); + } + } + } +} + +void laserDraw(Laser* l) +{ + int dx = 0, + dy = 480; + if (l->dir == -1) { + dx += 80; + } + + PHL_DrawSurfacePart(l->x, l->y, dx + (((int)l->imageIndex) * 40), dy, 40, 40, images[imgMisc20]); +} + +//Dragon Flame +void createFlame(int x, int y, int dir) +{ + int i; + for (i = 0; i < MAX_ENEMIES; i++) { + if (enemies[i] == NULL) { + Enemy* e = malloc(sizeof *e); + Flame* f = malloc(sizeof *f); + f->id = i; + + f->x = x; + f->y = y; + + f->dir = dir; + f->timer = 60; + + f->imageIndex = 0; + + e->data = f; + e->enemyStep = flameStep; + e->enemyDraw = flameDraw; + e->type = -1; + + enemies[i] = e; + i = MAX_ENEMIES; + } + } +} + +void flameStep(Flame* f) +{ + f->imageIndex += 0.25; + + if (f->timer > 0) { + if (f->imageIndex >= 3) { + f->imageIndex -= 3; + } + } + + f->timer -= 1; + + if (f->timer == 0) { + f->imageIndex = 3; + } + + //Hero Collision + { + Mask mask; + mask.circle = mask.unused = 0; + mask.x = f->x; + mask.y = f->y + 16; + mask.w = 120; + mask.h = 18; + if (f->dir == -1) { + mask.x -= 120; + } + + if (checkCollision(mask, getHeroMask()) == 1) { + int centerX = mask.x + 60 - (60 * f->dir); + + //Hero is on ladder + if (getHeroState() == 3) { + centerX = herox; + } + + heroHit(30, centerX); + } + } + + if (f->timer < 0 && f->imageIndex >= 6) { + enemyDestroy(f->id); + } +} + +void flameDraw(Flame* f) +{ + int drawX = f->x, + drawY = f->y; + + int cropX = 0, + cropY = 0; + + if (f->dir == -1) { + cropX += 720; + drawX -= 120; + } + + cropX += 120 * (int)f->imageIndex; + + while (cropX >= 600) { + cropX -= 600; + cropY += 40; + } + + PHL_DrawSurfacePart(drawX, drawY, cropX, cropY, 120, 40, images[imgMisc6020]); +} + +//Demon Rock +void createRock(int x, int y, int dir) +{ + int i; + for (i = 0; i < MAX_ENEMIES; i++) { + if (enemies[i] == NULL) { + Enemy* e = malloc(sizeof *e); + Rock* r = malloc(sizeof *r); + r->id = i; + + r->x = x; + r->y = y; + + r->vsp = -3; + r->dir = dir; + + r->imageIndex = 0; + + e->data = r; + e->enemyStep = rockStep; + e->enemyDraw = rockDraw; + e->type = -1; + + enemies[i] = e; + i = MAX_ENEMIES; + } + } +} + +void rockStep(Rock* r) +{ + char dead = 0; + + //Animate + { + r->imageIndex += 0.25 * r->dir; + if (r->imageIndex >= 8) { + r->imageIndex -= 8; + } + if (r->imageIndex < 0) { + r->imageIndex += 8; + } + } + + //Setup Mask + Mask mask; + { + mask.unused = mask.circle = 0; + mask.x = r->x + 2; + mask.y = r->y + 2; + mask.w = 36; + mask.h = 36; + } + + int hsp = 3; + double grav = 0.12; + + //Movement + { + r->y += r->vsp; + r->vsp += grav; + + //Collide with floor + { + mask.y = r->y + 2; + + PHL_Rect collide = getTileCollision(1, mask); + if (collide.x == -1) { + collide = getTileCollision(3, mask); + } + + if (collide.x != -1) { + PHL_PlaySound(sounds[sndHit06], CHN_ENEMIES); + r->y = collide.y - mask.h - 2; + r->vsp = -3; + mask.y = r->y + 2; + } + } + + r->x += hsp * r->dir; + + //Collide with wall + { + mask.x = r->x + 2; + + PHL_Rect collide = getTileCollision(1, mask); + + if (collide.x != -1) { + dead = 1; + } + } + } + + //Collision + { + //Hero collision + if (checkCollision(mask, getHeroMask()) == 1) { + heroHit(20, mask.x + (mask.w / 2)); + } + + //Weapon collision + int i; + for (i = 0; i < MAX_WEAPONS; i++) { + if (weapons[i] != NULL) { + if (weapons[i]->cooldown == 0) { + if (checkCollision(mask, weapons[i]->weaponMask)) { + weaponHit(weapons[i]); + PHL_PlaySound(sounds[sndHit03], CHN_WEAPONS); + + i = MAX_WEAPONS; + } + } + } + } + } + + //Destroy + if (dead == 1) { + createRockSmash(r->x + 20, r->y); + enemyDestroy(r->id); + } +} + +void rockDraw(Rock* r) +{ + PHL_DrawSurfacePart(r->x, r->y, 320 + ((int)r->imageIndex * 40), 160, 40, 40, images[imgEnemies]); +} + +//Air Stream +void createAir(int x, int y) +{ + int i; + for (i = 0; i < MAX_ENEMIES; i++) { + if (enemies[i] == NULL) { + Enemy* e = malloc(sizeof *e); + Air* a = malloc(sizeof *a); + a->id = i; + + a->x = x; + a->y = y; + + a->imageIndex = 0; + + e->data = a; + e->enemyStep = airStep; + e->enemyDraw = airDraw; + e->type = -1; + + enemies[i] = e; + i = MAX_ENEMIES; + } + } +} + +//Air Puff +void airStep(Air* a) +{ + Mask mask; + mask.circle = mask.unused = 0; + mask.w = 36; + mask.h = 30; + mask.x = a->x + ((40 - mask.w) / 2); + + //Animate + a->imageIndex += 0.5; + if (a->imageIndex >= 2) { + a->imageIndex -= 2; + } + + //Movement + a->y -= 6; + mask.y = a->y + (40 - mask.h); + + //Collide with player + if (getHeroState() != 2) { + if (checkCollision(mask, getHeroMask())) { + if (hasItem[27] == 0) { + heroHit(10, mask.x + (mask.w / 2)); + }else{ + //Floating stuff + if (getHeroVsp() > -5) { + setHeroVsp(-5); + setHeroOnground(0); + } + } + } + } + + //destroy if outside of room + if (mask.y + mask.h < 0) { + enemyDestroy(a->id); + } +} + +void airDraw(Air* a) +{ + PHL_DrawSurfacePart(a->x, a->y, (int)a->imageIndex * 40, 560, 40, 40, images[imgMisc20]); +} \ No newline at end of file diff --git a/src/enemies/heads.h b/src/enemies/heads.h new file mode 100644 index 0000000..75fcab0 --- /dev/null +++ b/src/enemies/heads.h @@ -0,0 +1,89 @@ +#ifndef HEADS_H +#define HEADS_H + +#include "../collision.h" + +//Goblin/medusa/dragon head statues +typedef struct { + int id, type; //0 = Rhyno head | 1 = Goblin | 2 = Dragon | 3 = Demon | 4 = Fireball | 5 = Air Jar + int state, timer; + double x, y; + int dir; + int hp, invincible; + int cooloff; + int counter; + + //Mask mask; +} Head; + +void createHead(int type, int x, int y, int dir, int offset, int cooloff); + +//Bullet from Rhyno statues +typedef struct { + int id; + double x, y; + int hsp; + double imageIndex; + + //Mask mask; +} Bullet; + +void createBullet(int x, int y, int dir, int minid); //Minid is the spawner's id + +//Fireball +typedef struct { + int id; + double x, y; + int angle; + int spd; + double imageIndex; + + Mask mask; +} Fireball; + +void createFireball(int x, int y, int angle, int minid); + +//Medusa lazer +typedef struct { + int id; + double x, y; + int dir; + double imageIndex; + + Mask mask; +} Laser; + +void createLaser(int x, int y, int dir); + +//Dragon flame +typedef struct { + int id; + int x, y; + int dir; + int timer; + double imageIndex; +} Flame; + +void createFlame(int x, int y, int dir); + +//Demon Boulder +typedef struct { + int id; + double x, y; + double vsp; + int dir; + double imageIndex; +} Rock; + +void createRock(int x, int y, int dir); + +//Air +typedef struct { + int id; + double x, y; + double imageIndex; +} Air; + +void createAir(int x, int y); + +#endif \ No newline at end of file diff --git a/src/enemies/hydra.c b/src/enemies/hydra.c new file mode 100644 index 0000000..d770c9d --- /dev/null +++ b/src/enemies/hydra.c @@ -0,0 +1,1127 @@ +#include "hydra.h" +#include "../game.h" +#include "../hero.h" +#include +#include + +const double PI = 3.14159; + +double headRot = 0; + +void hydraStep(Hydra* h); +void hydraDraw(Hydra* h); + +void hydraDestroy(Hydra* h); + +void hydraheadStep(Hydrahead* h); +void hydraheadDraw(Hydrahead* h); + +void hydragoopStep(Hydragoop* h); +void hydragoopDraw(Hydragoop* h); + +void hydrarockStep(Hydrarock* h); +void hydrarockDraw(Hydrarock* h); + +void hydrashockStep(Hydrashock* h); +void hydrashockDraw(Hydrashock* h); + +double getHydraX(Hydrahead* h); +double getHydraY(Hydrahead* h); + +Mask getHydraMask(Hydra* h); +int checkWeaponCollision(Mask m); + +double lengthdir_x(double ang, double len); +double lengthdir_y(double ang, double len); + +void setHeadState(int headid, int state); + + +//#hydra +void createHydra(int x) +{ + PHL_FreeSurface(images[imgBoss]); + images[imgBoss] = PHL_LoadQDA("lboss01.bmp"); + + int i; + for (i = 4; i < MAX_ENEMIES; i++) { + if (enemies[i] == NULL) { + setBossRoom(); + + Enemy* e = malloc(sizeof *e); + Hydra* h = malloc(sizeof *h); + h->id = i; + + h->hp = 10; + //h->hp = 1; + h->blink = 0; + + h->x = x; + h->y = -64; + + h->hsp = 0; + h->vsp = 0; + + h->imageIndex = 0; + + h->state = 0; + h->timer = 0; + + h->patternCounter = 0; + + h->onground = 0; + h->noheads = 0; + + e->data = h; + e->enemyStep = hydraStep; + e->enemyDraw = hydraDraw; + e->type = 47; + + enemies[i] = e; + + h->headid[0] = createHydrahead(-1, 0, i); + h->headid[1] = createHydrahead(1, 0, i); + h->headid[2] = createHydrahead(1, 1, i); + h->headid[3] = createHydrahead(-1, 1, i); + + i = MAX_ENEMIES; + } + } +} + +void hydraStep(Hydra* h) +{ + double grav = 0.2; + double fric = 0.1; + + //Death + if (h->state == 6) { + h->y += 0.2; + + h->timer -= 1; + h->blink -= 1; + + if (h->timer % 12 == 0) { + createEffect(2, h->x - 64 + (rand() % 128) - 32, h->y - 64 + (rand() % 128)); + } + + if (h->timer <= 0) { + hydraDestroy(h); + } + } + else{ + //Setup Mask + Mask mask = getHydraMask(h); + + //States with hydra heads + if (h->noheads == 0) { + //Fall in intro + if (h->state == 0) { + h->hsp = 0; + h->timer += 1; + if (h->timer >= 50) { + h->timer = 50; + h->imageIndex = 2; + + if (h->onground == 1) { + h->state = 1; + h->timer = 0; + } + }else{ + grav = 0; + } + } + + //Wait/Pattern activate + else if (h->state == 1) + { + h->timer += 1; + + //Stop speed animation + if (h->timer >= 120) { + int patternSize = 9; + int pattern[9] = {4, 0, 4, 1, 4, 2, 4, 3, 2}; + + //Head seizure + if (pattern[h->patternCounter] == 4) { + h->state = 4; + h->timer = 0; + } + //Small hop + if (pattern[h->patternCounter] == 0) { + h->state = 2; + h->timer = 0; + } + + //Goop + if (pattern[h->patternCounter] == 1) { + h->timer = -120; + setHeadState(h->headid[0], 2); + setHeadState(h->headid[1], 2); + } + + //Big Hop + if (pattern[h->patternCounter] == 2) { + h->state = 3; + h->timer = 0; + } + + //Electricity + if (pattern[h->patternCounter] == 3) { + h->timer = -40; + setHeadState(h->headid[2], 3); + setHeadState(h->headid[3], 3); + } + + h->patternCounter += 1; + if (h->patternCounter >= patternSize) { + h->patternCounter = 0; + } + } + } + + //Head seizure state + else if (h->state == 4) { + //Speed up head animation + if (h->timer == 0) { + setHeadState(h->headid[0], 1); + setHeadState(h->headid[1], 1); + setHeadState(h->headid[2], 1); + setHeadState(h->headid[3], 1); + } + + h->timer += 1; + + //Stop speed animation + if (h->timer == 120) { + setHeadState(h->headid[0], 0); + setHeadState(h->headid[1], 0); + setHeadState(h->headid[2], 0); + setHeadState(h->headid[3], 0); + + //Pattern + h->state = 1; + h->timer = 120; + } + } + + //Switch to noheads mode + if (h->onground == 1 && + enemies[h->headid[0]] == NULL && + enemies[h->headid[1]] == NULL && + enemies[h->headid[2]] == NULL && + enemies[h->headid[3]] == NULL) + { + h->noheads = 1; + h->state = 1; + h->timer = -15; + h->patternCounter = 0; + } + } + + //States without hydra heads + else{ + //Wait/pattern activate + if (h->state == 1) { + h->timer += 1; + + if (h->timer >= 0) { + int patternSize = 3; + int pattern[3] = {0, 0, 2}; + + //Small hop + if (pattern[h->patternCounter] == 0) { + h->state = 2; + h->timer = 0; + } + + //Big Hop + if (pattern[h->patternCounter] == 2) { + h->state = 3; + h->timer = 0; + } + + h->patternCounter += 1; + if (h->patternCounter >= patternSize) { + h->patternCounter = 0; + } + } + } + } + + //States used by both modes + { + //Small hop + if (h->state == 2) { + //Setup + if (h->timer == 0) { + h->vsp = -2; + h->onground = 0; + h->hsp = 2.5; + if (herox < h->x) { + h->hsp *= -1; + } + } + + h->timer += 1; + + if (h->onground == 1) { + if (h->noheads == 0 || h->hsp == 0) { + h->state = 1; + h->timer = 0; + } + } + } + + //Large Hop + else if (h->state == 3) { + h->hsp = 0; + + //Setup + if (h->timer == 0) { + h->timer = 1; + if (h->noheads == 0) { + h->vsp = -8; + }else{ + h->vsp = -5; + } + h->onground = 0; + } + + if (h->onground == 1) { + h->timer += 1; + + if (h->timer % 20 == 0) { + createHydrarock(); + } + + if (h->timer >= 220) { + h->state = 1; + h->timer = -15; + } + } + } + } + + //Animate + { + if (h->onground == 1) { + h->imageIndex += 0.1; + if (h->imageIndex >= 2) { + h->imageIndex -= 2; + } + }else{ + if (h->vsp < 0) { + h->imageIndex = 3; + } + else { + h->imageIndex = 2; + } + } + + //Blink + if (h->blink > 0) { + h->blink -= 1; + } + } + + //Movement + { + //Horizontal + if (h->hsp != 0) { + h->x += h->hsp; + mask = getHydraMask(h); + + PHL_Rect collide = getTileCollision(1, mask); + if (collide.x != -1) { + int dir = 1; + if (h->hsp < 0) { + dir = -1; + } + h->x = collide.x + 20 - ((20 + (mask.w / 2)) * dir); + + h->hsp *= -1; + } + } + + //Friction + { + if (h->onground == 1) { + if (h->hsp > 0) { + h->hsp -= fric; + if (h->hsp < 0) { + h->hsp = 0; + } + } + if (h->hsp < 0) { + h->hsp += fric; + if (h->hsp > 0) { + h->hsp = 0; + } + } + } + } + + //Vertical + { + int maxVsp = 9; + + if (h->onground == 0) { + h->y += h->vsp; + h->vsp += grav; + mask = getHydraMask(h); + + //Limit vsp + { + if (h->vsp > maxVsp) { + h->vsp = maxVsp; + } + } + + //Collide with floor + { + PHL_Rect collide = getTileCollision(1, mask); + if (collide.x != -1) { + h->y = collide.y - 64; + h->vsp = 0; + h->onground = 1; + PHL_PlaySound(sounds[sndHit04], CHN_ENEMIES); + quakeTimer = 30; + createEffectExtra(3, h->x - 30, h->y + 32, -1, 0, 0); + createEffectExtra(3, h->x - 10, h->y + 32, 1, 0, 0); + } + } + } + } + + } + + //Update mask + mask = getHydraMask(h); + + //Hero Collision + { + if (checkCollision(mask, getHeroMask()) == 1) { + heroHit(25, h->x); + } + } + + //Weapon Collision + { + int wid = checkWeaponCollision(mask); + if (wid != -1) { + //Pushed back + if (h->noheads == 0) { + h->hsp = weapons[wid]->dir; + PHL_PlaySound(sounds[sndPi05], CHN_ENEMIES); + }else{ + h->hp -= 1; + h->blink = 15; + } + weaponHit(weapons[wid]); + //Die + if (h->hp <= 0) { + h->state = 6; + h->timer = 180; + h->blink = 200; + } + + } + } + } + +} + +void hydraDraw(Hydra* h) +{ + if (h->blink % 2 == 0) { + int cropX = (int)h->imageIndex * 128; + int cropY = 128; + + if (h->noheads == 1) { + cropY += 128; + } + + PHL_DrawSurfacePart(h->x - 64, h->y - 64, cropX, cropY, 128, 128, images[imgBoss]); + } +} + +void hydraDestroy(Hydra* h) +{ + enemyDestroy(h->id); + bossDefeatedFlag = 1; + roomSecret = 1; + + PHL_StopMusic(); +} + +Mask getHydraMask(Hydra* h) +{ + Mask mask; + + mask.unused = mask.circle = 0; + mask.w = 84; + mask.h = 84; + mask.x = h->x - (mask.w / 2); + mask.y = h->y - 64 + (128 - mask.h); + + return mask; +} + +//#heads +int createHydrahead(int dir, int position, int bodyid) +{ + int result = -1; + + int i; + for (i = 0; i < MAX_ENEMIES; i++) { + if (enemies[i] == NULL) { + Enemy* e = malloc(sizeof *e); + Hydrahead* h = malloc(sizeof *h); + + h->id = i; + result = i; + + h->hp = 25; + //h->hp = 1; + h->blink = 0; + + h->dir = dir; + h->position = position; + + h->imageIndex = 0; + + h->neckRot = 0; + if (position != 0) { + h->neckRot -= 45; + } + + h->state = 0; + h->timer = 0; + h->counter = 0; + + h->bodyid = bodyid; + + int a; + for (a = 0; a < 7; a++) { + h->bodyposX[a] = 0; + h->bodyposY[a] = 0; + } + + e->data = h; + e->enemyStep = hydraheadStep; + e->enemyDraw = hydraheadDraw; + e->type = -1; + + enemies[i] = e; + i = MAX_ENEMIES; + } + } + + return result; +} + +void hydraheadStep(Hydrahead* h) +{ + char dead = 0; + + //Animate + { + h->imageIndex += 0.1; + if (h->imageIndex >= 2) { + h->imageIndex -= 2; + } + + if (h->blink > 0) { + h->blink -= 1; + } + + h->neckRot += 2; + if (h->neckRot >= 360) { + h->neckRot -= 360; + } + } + + //States + { + //Death + if (h->state == 4) { + h->timer += 1; + if (h->timer % 6 == 0) { + createEffect(2, h->bodyposX[6 - h->counter] - 32, h->bodyposY[6 - h->counter] - 32); + h->counter += 1; + } + + if (h->counter >= 7) { + dead = 1; + } + } + else{ + if (h->state == 0) { + //Do nothing special + } + + //Fast movements + else if (h->state == 1) { + h->neckRot += 2; + } + + //Shoot goop + else if (h->state == 2) { + h->neckRot += 2; + h->timer += 1; + + //Create Goop + if (h->timer % 15 == 0) { + int ghsp = -4 + (rand() % 9), + gvsp = -6; + + createHydragoop(h->bodyposX[6], h->bodyposY[6], ghsp, gvsp); + } + + if (h->timer >= 120) { + h->state = 0; + } + } + + //Shoot electricity + else if (h->state == 3) { + if (h->timer == 0) { + h->timer = 1; + } + + if (h->timer % 20 == 0) { + if (h->counter == 0) { + createHydrashock(h->bodyposX[6] + (70 * h->dir), h->bodyposY[6] + 20); + } + h->neckRot -= 2; + h->counter += 1; + if (h->counter >= 20) { + h->counter = 0; + h->timer += 1; + } + }else{ + h->neckRot += 2; + h->timer += 1; + } + + if (h->timer > 80) { + h->state = 0; + /* + Hydra* body = enemies[h->bodyid]->data; + body->state = 1; + body->timer = 239; + */ + } + + } + + Mask mask; + mask.circle = mask.unused = 0; + + //Collide with player + { + int i; + for (i = 0; i < 7; i+=2) { + //Setup mask + { + mask.w = 48; + mask.h = 48; + + //Head + if (i == 6) { + mask.w = 60; + mask.h = 36; + } + + mask.x = h->bodyposX[i] - (mask.w / 2); + mask.y = h->bodyposY[i] - (mask.h / 2); + } + + //Collide + if (checkCollision(getHeroMask(), mask) == 1) { + heroHit(25, getHydraX(h)); + } + } + } + + //Weapon collision + { + //Mask should still be on the head + int wid = checkWeaponCollision(mask); + if (wid != -1) { + h->blink = 15; + h->hp -= 1; + weaponHit(weapons[wid]); + + if (h->hp <= 0) { + h->state = 4; + h->timer = 0; + h->counter = 0; + } + } + } + + + } + + } + + //Destroy object + if (dead == 1) { + enemyDestroy(h->id); + } +} + +void hydraheadDraw(Hydrahead* h) +{ + /* + char c[10]; + sprintf(c, "%02d", h->timer); + PHL_DrawTextBold(c, h->bodyposX[6], 0, 0); + */ + + h->bodyposX[0] = getHydraX(h) + 20; + h->bodyposY[0] = getHydraY(h); + + double drawX = getHydraX(h) + 20; + double drawY = getHydraY(h); + + int dis = 24; + int angle = -25; + + if (h->position == 1) { + angle = -60; + + drawX -= 5; + drawY -= 20; + } + + int i; + for (i = 0; i < 7; i++) { + double wavlen = sin((h->neckRot + (45 * i)) * PI / 180); + + double incang = 45; + + if (h->position != 0) { + incang = 45; + } + + if (i == 6) { + //incang += 15; + incang = 50; + + if (h->position == 1) { + incang = 80; + } + } + + drawX += lengthdir_x(angle + (incang * wavlen), dis); + drawY += lengthdir_y(angle + (incang * wavlen), dis); + + h->bodyposX[i] = drawX; + h->bodyposY[i] = drawY; + + if (h->dir == -1) { + double difference = h->bodyposX[i] - getHydraX(h); + h->bodyposX[i] = getHydraX(h) - difference; + } + + if (h->blink % 2 == 0) { + if (h->state != 4 || (6 - h->counter >= i)) { + if (i != 6) { + int cropX = 0; + + if (h->dir == -1) { + cropX += 64; + } + + PHL_DrawSurfacePart(h->bodyposX[i] - 32, h->bodyposY[i] - 32, cropX, 64, 64, 64, images[imgBoss]); + }else{ + int cropX = 0; + + if (h->dir == -1) { + cropX += 320; + } + + cropX += (int)h->imageIndex * 80; + + PHL_DrawSurfacePart(h->bodyposX[i] - 40, h->bodyposY[i] - 32, cropX, 0, 80, 64, images[imgBoss]); + } + } + } + } + +} + +int checkWeaponCollision(Mask m) +{ + int i; + for (i = 0; i < MAX_WEAPONS; i++) { + if (weapons[i] != NULL) { + if (weapons[i]->cooldown == 0) { + if (checkCollision(weapons[i]->weaponMask, m) == 1) { + return i; + } + } + } + } + + return -1; +} + +double lengthdir_x(double ang, double len) +{ + return cos(ang * PI / 180) * len; +} + +double lengthdir_y(double ang, double len) +{ + return sin(ang * PI / 180) * len; +} + +double getHydraX(Hydrahead* h) +{ + if (enemies[h->bodyid] != NULL) { + Hydra* hbody = enemies[h->bodyid]->data; + return hbody->x; + } + + return -1; +} + +double getHydraY(Hydrahead* h) +{ + if (enemies[h->bodyid] != NULL) { + Hydra* hbody = enemies[h->bodyid]->data; + return hbody->y; + } + + return -1; +} + +void setHeadState(int headid, int state) +{ + if (enemies[headid] != NULL) { + Hydrahead* h = enemies[headid]->data; + if (h->state != 4) { + h->state = state; + h->timer = 0; + h->counter = 0; + } + } +} + +//#goop +void createHydragoop(int x, int y, int hsp, int vsp) +{ + int i; + for (i = 0; i < MAX_ENEMIES; i++) { + if (enemies[i] == NULL) { + Enemy* e = malloc(sizeof *e); + Hydragoop* h = malloc(sizeof *h); + h->id = i; + + h->x = x; + h->y = y; + + h->hsp = hsp; + h->vsp = vsp; + + h->inwall = 0; + h->bounce = 0; + + h->imageIndex = 0; + + e->data = h; + e->enemyStep = hydragoopStep; + e->enemyDraw = hydragoopDraw; + e->type = -1; + + enemies[i] = e; + i = MAX_ENEMIES; + + PHL_PlaySound(sounds[sndPi06], CHN_ENEMIES); + } + } +} + +void hydragoopStep(Hydragoop* h) +{ + char dead = 0; + + //Animate + { + h->imageIndex += 0.16; + if (h->imageIndex >= 3) { + h->imageIndex -= 3; + } + } + + //Setup Mask + Mask mask; + { + mask.circle = mask.unused = 0; + mask.w = 36; + mask.h = 36; + mask.x = h->x - mask.w / 2; + mask.y = h->y - mask.h / 2; + } + + //Movement + { + double grav = 0.2; + + h->x += h->hsp; + mask.x = h->x - mask.w / 2; + + if (checkTileCollision(1, mask) == 1) { + h->inwall = 1; + } + + h->y += h->vsp; + h->vsp += grav; + mask.y = h->y - mask.h / 2; + + if (h->inwall == 0 && h->bounce == 0) { + if (checkTileCollision(1, mask) == 1) { + h->bounce = 1; + h->vsp = -2; + } + } + } + + //Outside of room + { + if ( (h->y > 500 && h->vsp >= 0) || + (h->x < -20 && h->hsp <= 0) || + (h->x > 660 && h->hsp >= 0) ) + { + dead = 1; + } + } + + //Collide with hero + { + //Collide with shield + if (checkCollision(mask, shieldMask) == 1) { + createEffect(1, h->x - 20, h->y - 20); + PHL_PlaySound(sounds[sndHit07], CHN_EFFECTS); + dead = 1; + } + else if (checkCollision(mask, getHeroMask()) == 1) { + if (heroHit(25, h->x) == 1) { + heroPoison(); + } + } + } + + //Destroy object + { + if (dead == 1) { + enemyDestroy(h->id); + } + } + +} + +void hydragoopDraw(Hydragoop* h) +{ + int cropX = 320; + + cropX += (int)h->imageIndex * 40; + + PHL_DrawSurfacePart(h->x - 20, h->y - 20, cropX, 480, 40, 40, images[imgMisc20]); +} + +//#rock +void createHydrarock() +{ + int i; + for (i = 0; i < MAX_ENEMIES; i++) { + if (enemies[i] == NULL) { + Enemy* e = malloc(sizeof *e); + Hydrarock* h = malloc(sizeof *h); + h->id = i; + + h->x = 70 + (rand() % 26) * 20; + h->y = -24; + + h->vsp = 0; + + h->bounce = 0; + + h->imageIndex = 0; + + e->data = h; + e->enemyStep = hydrarockStep; + e->enemyDraw = hydrarockDraw; + e->type = -1; + + enemies[i] = e; + i = MAX_ENEMIES; + } + } +} + +void hydrarockStep(Hydrarock* h) +{ + //Animate + { + h->imageIndex += 0.25; + if (h->imageIndex >= 8) { + h->imageIndex -= 8; + } + } + + //Movement + { + double grav = 0.15; + + h->y += h->vsp; + h->vsp += grav; + } + + //Setup Mask + Mask mask; + { + mask.circle = mask.unused = 0; + mask.w = 44; + mask.h = 44; + mask.x = h->x - mask.w / 2; + mask.y = h->y - mask.h / 2; + } + + if (h->bounce == 0) { + if (checkTileCollision(1, mask) == 1) { + h->bounce = 1; + h->vsp = -2; + PHL_PlaySound(sounds[sndHit06], CHN_ENEMIES); + } + } + + //Hero collision + { + if (checkCollision(mask, getHeroMask()) == 1) { + heroHit(30, h->x); + } + } + + //Weapon Collision + { + int wid = checkWeaponCollision(mask); + if (wid != -1) { + weaponHit(weapons[wid]); + PHL_PlaySound(sounds[sndHit03], CHN_WEAPONS); + } + } + + if (h->y >= 520) { + enemyDestroy(h->id); + } +} + +void hydrarockDraw(Hydrarock* h) +{ + int cropX = 128; + + cropX += (int)h->imageIndex * 64; + + PHL_DrawSurfacePart(h->x - 32, h->y - 32, cropX, 128, 64, 64, images[imgMisc32]); +} + +//#electricity +void createHydrashock(int x, int y) +{ + int i; + for (i = 0; i < MAX_ENEMIES; i++) { + if (enemies[i] == NULL) { + Enemy* e = malloc(sizeof *e); + Hydrashock* h = malloc(sizeof *h); + h->id = i; + + h->timer = 0; + + h->x = x; + h->y = y; + + h->angle = 0; + + h->imageIndex = 0; + + e->data = h; + e->enemyStep = hydrashockStep; + e->enemyDraw = hydrashockDraw; + e->type = -1; + + enemies[i] = e; + i = MAX_ENEMIES; + + PHL_PlaySound(sounds[sndShot03], CHN_ENEMIES); + } + } +} + +void hydrashockStep(Hydrashock* h) +{ + //Animate + { + h->imageIndex += 0.5; + if (h->imageIndex >= 4) { + h->imageIndex -= 4; + } + } + + h->timer += 1; + + if (h->timer >= 20) { + if (h->timer == 20) { + //Set angle + h->angle = (atan2(h->x - (herox), heroy + 20 - h->y) * 180 / PI) + 90; + } + + h->timer = 22; + + //Movement + { + int spd = 5; + h->x += lengthdir_x(h->angle, spd); + h->y += lengthdir_y(h->angle, spd); + } + } + + //Setup mask + Mask mask; + { + mask.unused = mask.circle = 0; + mask.w = 28; + mask.h = 28; + mask.x = h->x - mask.w / 2; + mask.y = h->y - mask.h / 2; + } + + //Hero Collision + { + if (checkCollision(mask, getHeroMask()) == 1) { + if (heroHit(25, h->x) == 1) { + heroStun(); + } + } + } + + //Destroy if outside of room + { + if (mask.x > 660 || mask.x + mask.w < -20 || mask.y > 500 || mask.y + mask.h < -20) { + enemyDestroy(h->id); + } + } +} + +void hydrashockDraw(Hydrashock* h) +{ + if (h->timer % 2 == 0) { + int cropX = (int)h->imageIndex * 64; + + PHL_DrawSurfacePart(h->x - 32, h->y - 32, cropX, 192, 64, 64, images[imgMisc32]); + } +} \ No newline at end of file diff --git a/src/enemies/hydra.h b/src/enemies/hydra.h new file mode 100644 index 0000000..40cc28f --- /dev/null +++ b/src/enemies/hydra.h @@ -0,0 +1,65 @@ +#ifndef HYDRA_H +#define HYDRA_H + +typedef struct { + int id; + int hp, blink; + double x, y; + double hsp, vsp; + double imageIndex; + int state, timer; + int patternCounter; + char onground; + char noheads; + int headid[4]; +} Hydra; + +void createHydra(int x); + +typedef struct { + int id; + int hp, blink; + int dir; + int position; //0 = lower 1 = higher + double imageIndex; + double neckRot; + int state, timer, counter; + int bodyid; + double bodyposX[7]; + double bodyposY[7]; +} Hydrahead; + +int createHydrahead(int dir, int position, int bodyid); + +typedef struct { + int id; + double x, y; + double hsp, vsp; + char inwall; + char bounce; + double imageIndex; +} Hydragoop; + +void createHydragoop(int x, int y, int hsp, int vsp); + +typedef struct { + int id; + double x, y; + double vsp; + char bounce; + double imageIndex; +} Hydrarock; + +void createHydrarock(); + +typedef struct { + int id; + int timer; + double x, y; + double angle; + double imageIndex; +} Hydrashock; + +void createHydrashock(int x, int y); + +#endif \ No newline at end of file diff --git a/src/enemies/jellyfish.c b/src/enemies/jellyfish.c new file mode 100644 index 0000000..def3198 --- /dev/null +++ b/src/enemies/jellyfish.c @@ -0,0 +1,195 @@ +#include "jellyfish.h" +#include "../enemy.h" +#include "../game.h" +#include "../hero.h" +#include +#include + +void jellyfishStep(Jellyfish* j); +void jellyfishDraw(Jellyfish* j); + +void createJellyfish(int x, int y) +{ + int i; + for (i = 0; i < MAX_ENEMIES; i++) { + if (enemies[i] == NULL) { + Enemy* e = malloc(sizeof *e); + Jellyfish* j = malloc(sizeof *j); + j->id = i; + + j->x = x; + j->y = j->ystart = y; + j->ystart += 20; + + j->spd = 0; + j->angle = 0; + + j->state = 0; + j->imageIndex = 0; + + e->data = j; + e->enemyStep = jellyfishStep; + e->enemyDraw = jellyfishDraw; + e->type = 20; + + enemies[i] = e; + i = MAX_ENEMIES; + } + } +} + +void jellyfishStep(Jellyfish* j) +{ + Mask mask; + mask.unused = mask.circle = 0; + mask.w = mask.h = 30; + mask.x = j->x + 20 - (mask.w / 2); + mask.y = j->y + 20 - (mask.h / 2); + + //Idle float + if (j->state == 0) + { + //Animate + j->imageIndex += 0.06; + if (j->imageIndex >= 4) { + j->imageIndex -= 4; + } + + //Movement + j->angle += 2.5; + if (j->angle >= 360) { j->angle -= 360; } + j->y = j->ystart + (20 * sin(j->angle * 3.14159 / 180)); + + //Update mask + mask.y = j->y + 20 - (mask.h / 2); + + //if player is close enough + Mask area; + area.unused = area.circle = 0; + area.w = area.h = 160; + area.x = j->x - 60; + area.y = j->y - 60; + + if (checkCollision(area, getHeroMask()) == 1) { + j->state = 1; + j->spd = 0; + } + } + //Attack + if (j->state == 1) + { + //Setup + if (j->spd == 0) { + PHL_PlaySound(sounds[sndPi02], CHN_ENEMIES); + j->spd = 3; + + //Move Right + if (herox > j->x + 20) { + //Move Up + if (heroy < j->y) { + j->angle = 135; + } + //Move Down + else { + j->angle = 45; + } + } + //Move Left + else{ + //Move Up + if (heroy < j->y) { + j->angle = 225; + } + //Move Down + else { + j->angle = 315; + } + } + } + + //Movement + j->x += (j->spd) * sin(j->angle * 3.14159 / 180); + j->y += (j->spd) * cos(j->angle * 3.14159 / 180); + + //Slow down + j->spd -= 0.075; + if (j->spd <= 0) { + j->spd = 0; + j->state = 2; + } + } + //Stablize + if (j->state == 2) + { + //Setup + if (j->spd == 0) { + j->spd = 1; + j->ystart = j->y - 20; + j->angle = 80; + } + + //Movement + j->angle += 2.5; + if (j->angle >= 360) { j->angle -= 360; } + j->y = j->ystart + (20 * sin(j->angle * 3.14159 / 180)); + + + if (j->angle >= 180) { + j->state = 0; + j->ystart = j->y - 20; + j->angle = 100; + } + } + + //Update Mask + mask.x = j->x + 20 - (mask.w / 2); + mask.y = j->y + 20 - (mask.h / 2); + + //Collide with hero + if (checkCollision(mask, getHeroMask())) { + heroHit(15, j->x + 20); + } + + //Sword collision + int i; + for (i = 0; i < MAX_WEAPONS; i++) { + if (weapons[i] != NULL) { + if (checkCollision(mask, weapons[i]->weaponMask)) { + spawnCollectable(j->x + 20, j->y); + weaponHit(weapons[i]); + + createEffect(2, j->x - 12, j->y - 12); + enemyDestroy(j->id); + + i = MAX_WEAPONS; + } + } + } +} + +void jellyfishDraw(Jellyfish* j) +{ + int frame = 0; + + //if (j->state == 0) { + int animation[4] = { 0, 1, 0, 2}; + frame = animation[(int)j->imageIndex]; + //} + + if (j->state == 1) { + if (j->angle == 135) { + frame = 3; + } + else if (j->angle == 225) { + frame = 4; + } + else if (j->angle == 315) { + frame = 5; + } + else { + frame = 6; + } + } + + PHL_DrawSurfacePart(j->x, j->y, frame * 40, 520, 40, 40, images[imgEnemies]); +} \ No newline at end of file diff --git a/src/enemies/jellyfish.h b/src/enemies/jellyfish.h new file mode 100644 index 0000000..c247fe3 --- /dev/null +++ b/src/enemies/jellyfish.h @@ -0,0 +1,16 @@ +#ifndef JELLYFISH_H +#define JELLYFISH_H + +typedef struct { + int id; + double x, y; + int ystart; + double spd; + double angle; + int state; + double imageIndex; +} Jellyfish; + +void createJellyfish(int x, int y); + +#endif \ No newline at end of file diff --git a/src/enemies/knight.c b/src/enemies/knight.c new file mode 100644 index 0000000..06df4b7 --- /dev/null +++ b/src/enemies/knight.c @@ -0,0 +1,232 @@ +#include "knight.h" +#include "../enemy.h" +#include "../hero.h" +#include "../PHL.h" +#include "../game.h" +#include + +void knightDestroy(Knight* k); + +void createKnight(int x, int y, int type) +{ + int i; + for (i = 0; i < MAX_ENEMIES; i++) { + if (enemies[i] == NULL) { + Enemy* e = malloc(sizeof *e); + Knight* k = malloc(sizeof *k); + + k->id = i; + k->type = type; + + k->x = x; + k->y = y; + + //They face the player when they are spawned + k->dir = -1; + if (herox > x + 20) { + k->dir = 1; + } + + k->vsp = 0; + k->grav = 0.2; + + k->state = 0; + k->timer = 60 + (((rand() % 5) + 1) * 60); + k->imageIndex = 0; + + k->hp = 2; + //Shield Knight + if (k->type == 1) { + k->hp = 3; + } + + k->invincible = 0; + k->shieldhit = 0; + + k->mask.circle = 0; + k->mask.unused = 0; + k->mask.x = x + 4; + k->mask.y = y + 8; + k->mask.w = 32; + k->mask.h = 32; + + e->data = k; + e->enemyStep = knightStep; + e->enemyDraw = knightDraw; + e->type = 3; + + enemies[i] = e; + i = MAX_ENEMIES; + } + } +} + +void knightStep(Knight* k) +{ + if (k->shieldhit > 0) { + k->shieldhit -= 1; + } + + if (k->invincible > 0) { + k->invincible -= 1; + } + + if (k->state == 0) { //Walk + k->imageIndex += 0.1; + if (k->imageIndex >= 2) { + k->imageIndex -= 2; + } + + double spd = 1; + if (k->type == 1) { + spd = 0.5; + } + spd *= k->dir; + + k->x += spd; + + k->mask.x = k->x + 4; + k->mask.y = k->y + 8; + + Mask emask; + emask.circle = emask.unused = 0; + emask.w = 16; + emask.h = 32; + emask.x = k->x + 12; + emask.y = k->y + 8; + + //Turn when colliding with a wall + if (checkTileCollision(1, emask)) { + k->dir *= -1; + }else{ + //Turn when on an edge + k->mask.x += k->mask.w * k->dir; + k->mask.y += 1; + PHL_Rect collide = getTileCollision(1, k->mask); + if (collide.x == -1) { + collide = getTileCollision(3, k->mask); + } + if (collide.x == -1) { + k->dir *= -1; + } + } + + if (k->x + 20 >= 640 || k->x + 20 <= 0) { + k->dir *= -1; + } + + k->mask.x = k->x + 4; + k->mask.y = k->y + 8; + + k->timer -= 1; + if (k->timer <= 0) { + k->state = 1; + k->timer = 120; + k->imageIndex = 0; + } + } + else if (k->state == 1) { //Wait + k->timer -= 1; + if (k->timer <= 0) { + k->state = 0; + k->dir = 1; + if (herox < k->x + 20) { + k->dir = -1; + } + k->timer = 60 + (((rand() % 5) + 1) * 60); + } + } + + //Green Sword Knight + if (k->type == 0) { + //Hit player + Mask swordMask; + swordMask.unused = 0; + swordMask.circle = 0; + swordMask.x = k->x + (24 * k->dir); + swordMask.y = k->y + 20; + swordMask.w = 40; + swordMask.h = 10; + + if (checkCollision(getHeroMask(), swordMask)) { + heroHit(30, k->x + 20); + } + } + + if (checkCollision(getHeroMask(), k->mask)) { + heroHit(15, k->x + 20); + } + + //Weapon collision + int i; + for (i = 0; i < MAX_WEAPONS; i++) { + if (weapons[i] != NULL) { + if (weapons[i]->cooldown == 0) { + if (checkCollision(k->mask, weapons[i]->weaponMask)) { + char gotHit = 1; + + int weapondir = weapons[i]->dir; + weaponHit(weapons[i]); + + //Shield Collision + if (k->type == 1) { + if (weapondir == k->dir * -1) { + gotHit = 0; + k->shieldhit = 15; + PHL_PlaySound(sounds[sndHit03], CHN_WEAPONS); + } + } + + if (gotHit == 1) { + k->hp -= 1; + k->invincible = 15; + + i = MAX_WEAPONS; + } + + if (k->hp <= 0) { + knightDestroy(k); + } + } + } + } + } +} + +void knightDraw(Knight* k) +{ + if (k->invincible % 2 == 0) { + int cx = 0, cy = 200; + + //Green Knight's Sword + if (k->type == 0) { + int swordimg = 0; + if (k->dir == -1) { + swordimg = 1; + } + int posx = 24, posy = 8; + if ((int)k->imageIndex == 1) { + posx -= 2; + posy -= 2; + } + PHL_DrawSurfacePart(k->x + (posx * k->dir), k->y + posy, 160 + (swordimg * 40), 200, 40, 40, images[imgEnemies]); + } + + //Shield Knight + if (k->type == 1) { + cx = 240; + } + + if (k->dir == -1) { + cx += 80; + } + PHL_DrawSurfacePart(k->x, k->y, cx + ((int)k->imageIndex * 40), cy, 40, 40, images[imgEnemies]); + } +} + +void knightDestroy(Knight* k) +{ + createEffect(2, k->x - 12, k->y - 6); + spawnCollectable(k->x + 20, k->y); + enemyDestroy(k->id); +} \ No newline at end of file diff --git a/src/enemies/knight.h b/src/enemies/knight.h new file mode 100644 index 0000000..a709fc8 --- /dev/null +++ b/src/enemies/knight.h @@ -0,0 +1,23 @@ +#ifndef KNIGHT_H +#define KNIGHT_H + +#include "../collision.h" + +typedef struct { + int id, type; + double x, y, + vsp, grav; + int dir, state, timer; + double imageIndex; + int hp, invincible; + int shieldhit; + + Mask mask; +} Knight; + +void createKnight(int x, int y, int type); + +void knightStep(Knight* k); +void knightDraw(Knight* k); + +#endif \ No newline at end of file diff --git a/src/enemies/lolidra.c b/src/enemies/lolidra.c new file mode 100644 index 0000000..15879a4 --- /dev/null +++ b/src/enemies/lolidra.c @@ -0,0 +1,367 @@ +#include "lolidra.h" +#include "../enemy.h" +#include "../game.h" +#include "../hero.h" +#include +#include + +int boss5flag = 38; + +void lolidraDestroy(Lolidra* l); +int getNumOfMinions(); + +void createLolidra(int x, int y) +{ + if (flags[boss5flag] == 0) { //have not beaten boss 5 + PHL_FreeSurface(images[imgBoss]); + images[imgBoss] = PHL_LoadQDA("boss05.bmp"); + + int i; + for (i = 0; i < MAX_ENEMIES; i++) { + if (enemies[i] == NULL) { + //Boss start + setBossRoom(); + + Enemy* e = malloc(sizeof *e); + Lolidra* l = malloc(sizeof *l); + l->id = i; + + l->x = x; + l->y = y; + + l->positionY = l->y; + + l->imageIndex = 0; + l->hoverRot = 0; + + l->hp = 100; + //l->hp = 1; + l->state = 0; + l->invincible = 0; + l->visible = 1; + + l->timer = 0; + l->counter = 0; + + l->mask.unused = 0; + l->mask.circle = 1; + l->mask.w = 46; + l->mask.h = 0; + l->mask.x = l->x; + l->mask.y = l->y; + + e->data = l; + e->enemyStep = lolidraStep; + e->enemyDraw = lolidraDraw; + e->type = 44; + + enemies[i] = e; + i = MAX_ENEMIES; + } + } + } +} + +void lolidraStep(Lolidra* l) +{ + char dead = 0; + + l->imageIndex += 0.1; + if (l->imageIndex >= 3) { + l->imageIndex -= 3; + } + + if (l->invincible > 0) { + l->invincible -= 1; + } + + //Spawn minions + if (l->state == 0) + { + if (l->counter < 5) { + l->counter += 1; + }else{ + if (getNumOfMinions() < 10) { + l->counter = 0; + PHL_PlaySound(sounds[sndPi02], CHN_ENEMIES); + createMinion(l->x, l->y - 10); + } + } + + l->timer += 1; + if (l->timer >= 600){ + l->counter = 0; + l->timer = 0; + l->state = 1; + l->invincible = 20; + } + } + //Disappear + else if (l->state == 1 || l->state == 3) + { + if (l->invincible <= 0) { + l->visible = 0; + } + + if (l->timer == 0) { + PHL_PlaySound(sounds[sndPi10], CHN_ENEMIES); + } + + l->timer += 1; + if (l->timer >= 330) { + PHL_PlaySound(sounds[sndPi03], CHN_ENEMIES); + l->timer = 0; + l->visible = 1; + l->invincible = 20; + l->x = herox; + l->positionY = heroy - 40; + + if (l->state == 1) { + l->state = 2; + } + if (l->state == 3) { + l->state = 0; + } + } + } + //Pop-up + else if (l->state == 2) + { + l->timer += 1; + if (l->timer >= 180) { + l->timer = 0; + l->state = 3; + l->invincible = 20; + } + } + //Death + else if (l->state == 4) + { + l->y += 0.2; + + l->timer -= 1; + l->invincible -= 1; + + if (l->timer % 12 == 0) { + createEffect(2, l->x - 64 + (rand() % 128), l->y - 64 + (rand() % 128)); + } + + if (l->timer <= 0) { + lolidraDestroy(l); + dead = 1; + } + } + + if (dead == 0) + { + if (l->state != 4) { + //Hover + l->hoverRot += 5; + if (l->hoverRot >= 360) { + l->hoverRot -= 360; + } + l->y = l->positionY + (5 * sin(l->hoverRot * 3.14159 / 180)); + + //Update Mask + l->mask.x = l->x; + l->mask.y = l->y; + + //Collisions + if (l->visible == 1) { + //Collide with Hero + if (checkCollision(getHeroMask(), l->mask) == 1) { + heroHit(30, l->x); + } + + //Weapon collision + int i; + for (i = 0; i < MAX_WEAPONS; i++) { + if (weapons[i] != NULL) { + if (weapons[i]->cooldown == 0) { + if (checkCollision(l->mask, weapons[i]->weaponMask)) { + weaponHit(weapons[i]); + l->invincible = 15; + l->hp -= 1; + + //Die + if (l->hp <= 0) { + l->state = 4; + l->timer = 180; + l->invincible = 200; + } + + i = MAX_WEAPONS; + } + } + } + } + } + } + } + +} + +void lolidraDraw(Lolidra* l) +{ + if (l->visible == 1 && l->invincible % 2 == 0) { + PHL_DrawSurfacePart(l->x - 64, l->y - 74, ((int)l->imageIndex) * 128, 0, 128, 128, images[imgBoss]); + } +} + +void lolidraDestroy(Lolidra* l) +{ + enemyDestroy(l->id); + bossDefeatedFlag = 1; + roomSecret = 1; + + flags[boss5flag] = 1; + PHL_StopMusic(); +} + +//Minions +void createMinion(int x, int y) +{ + int i; + for (i = 0; i < MAX_ENEMIES; i++) { + if (enemies[i] == NULL) { + Enemy* e = malloc(sizeof *e); + Minion* m = malloc(sizeof *m); + m->id = i; + + m->state = 0; + m->timer = 0; + + m->x = x; + m->y = y; + + m->positionY = m->y; + + m->dir = rand() % 360; + m->spd = 8; + + m->imageIndex = 0; + + m->mask.circle = 1; + m->mask.unused = 0; + m->mask.w = 10; + m->mask.x = 0; + m->mask.y = 0; + + e->data = m; + e->enemyStep = minionStep; + e->enemyDraw = minionDraw; + e->type = 23; + + enemies[i] = e; + i = MAX_ENEMIES; + } + } +} + +void minionStep(Minion* m) +{ + char dead = 0; + + m->imageIndex += 0.2; + if (m->imageIndex >= 2) { + m->imageIndex -= 2; + } + + //Slow down + if (m->state == 0) + { + if (m->spd > 0) { + m->spd -= 0.3; + } + + if (m->spd <= 0) { + m->positionY = m->y; + m->spd = 0; + m->dir = 0; + m->state = 1; + } + } + //Hover + else if (m->state == 1) + { + //Hover + m->dir += 5; + if (m->dir >= 360) { + m->dir -= 360; + } + m->y = m->positionY + (10 * sin(m->dir * 3.14159 / 180)); + + m->timer += 1; + if (m->timer >= 120) { + m->timer = 0; + m->state = 2; + m->spd = (rand() % 2) + 1; + m->dir = (atan2(heroy + 20 - m->y, m->x - herox) * 180 / 3.14159) + 270; + } + } + //Suicide + else if (m->state == 2) + { + m->timer += 1; + if (m->timer >= 120) { + createEffect(5, m->x, m->y); + enemyDestroy(m->id); + dead = 1; + } + } + + if (dead == 0) + { + //Movement + if (m->spd != 0) { + m->x += m->spd * sin(m->dir * 3.14159 / 180); + m->y += m->spd * cos(m->dir * 3.14159 / 180); + } + + //Update Mask + m->mask.x = m->x; + m->mask.y = m->y; + + //Collide with Hero + if (checkCollision(getHeroMask(), m->mask) == 1) { + if (heroHit(10, m->x) == 1) { + heroPoison(); + } + } + + //Weapon collision + int i; + for (i = 0; i < MAX_WEAPONS; i++) { + if (weapons[i] != NULL) { + if (checkCollision(m->mask, weapons[i]->weaponMask)) { + weaponHit(weapons[i]); + createEffect(2, m->x - 32, m->y - 32); + enemyDestroy(m->id); + + i = MAX_WEAPONS; + } + } + } + } +} + +void minionDraw(Minion* m) +{ + PHL_DrawSurfacePart(m->x - 32, m->y - 32, ((int)m->imageIndex) * 64, 128, 64, 64, images[imgBoss]); +} + +int getNumOfMinions() +{ + int result = 0; + + int i; + for (i = 0; i < MAX_ENEMIES; i++) { + if (enemies[i] != NULL) { + if (enemies[i]->type == 23) { + result += 1; + } + } + } + + return result; +} \ No newline at end of file diff --git a/src/enemies/lolidra.h b/src/enemies/lolidra.h new file mode 100644 index 0000000..f9a9b51 --- /dev/null +++ b/src/enemies/lolidra.h @@ -0,0 +1,40 @@ +#ifndef LOLIDRA_H +#define LOLIDRA_H + +#include "../collision.h" + +typedef struct { + int id; + double x, y; + double positionY; + double imageIndex, hoverRot; + int hp, state, invincible, + visible, timer, counter; + + Mask mask; +} Lolidra; + +void createLolidra(int x, int y); + +void lolidraStep(Lolidra* l); +void lolidraDraw(Lolidra* l); + +//Minion +typedef struct { + int id; + int state; + int timer; + double x, y; + double positionY; + double imageIndex; + double dir, spd; + + Mask mask; +} Minion; + +void createMinion(int x, int y); + +void minionStep(Minion* m); +void minionDraw(Minion* m); + +#endif \ No newline at end of file diff --git a/src/enemies/pendulum.c b/src/enemies/pendulum.c new file mode 100644 index 0000000..4eda905 --- /dev/null +++ b/src/enemies/pendulum.c @@ -0,0 +1,78 @@ +#include "pendulum.h" +#include "../enemy.h" +#include "../game.h" +#include "../hero.h" +#include +#include + +void createPendulum(int x, int y, int side) +{ + int i; + for (i = 0; i < MAX_ENEMIES; i++) { + if (enemies[i] == NULL) { + Enemy* e = malloc(sizeof *e); + Pendulum* p = malloc(sizeof *p); + p->id = i; + + p->x = x; + p->y = y; + + p->angle = 0; + p->rotCounter = 180; + if (side == 1) { + p->rotCounter += 180; + } + + p->mask.circle = 1; + p->mask.unused = 0; + p->mask.w = 24; + p->mask.x = 0; + p->mask.y = 0; + + e->data = p; + e->enemyStep = pendulumStep; + e->enemyDraw = pendulumDraw; + e->type = 22; + + enemies[i] = e; + i = MAX_ENEMIES; + } + } +} + +void pendulumStep(Pendulum* p) +{ + p->rotCounter += 2; + if (p->rotCounter >= 360) { + p->rotCounter -= 360; + } + + p->angle += (3.15 * cos(p->rotCounter * 3.14159 / 180)); + + //Update Mask + p->mask.x = p->x + (96 * cos((p->angle + 90) * 3.14159 / 180)); + p->mask.y = p->y + (96 * sin((p->angle + 90) * 3.14159 / 180)); + + //Hit Player + if (checkCollision(p->mask, getHeroMask())) { + heroHit(15, p->mask.x); + } +} + +void pendulumDraw(Pendulum* p) +{ + int drawX = p->x, + drawY = p->y; + + int len[] = {0, 16, 32, 48, 66, 96}; + int cropX[] = {64, 64, 64, 64, 0, 576}; + int cropY[] = {128, 128, 128, 128, 128, 64}; + + int i; + for (i = 0; i < 6; i++) { + drawX = p->x + (len[i] * cos((p->angle + 90) * 3.14159 / 180)); + drawY = p->y + (len[i] * sin((p->angle + 90) * 3.14159 / 180)); + + PHL_DrawSurfacePart(drawX- 32, drawY - 32, cropX[i], cropY[i], 64, 64, images[imgMisc32]); + } +} \ No newline at end of file diff --git a/src/enemies/pendulum.h b/src/enemies/pendulum.h new file mode 100644 index 0000000..faaba05 --- /dev/null +++ b/src/enemies/pendulum.h @@ -0,0 +1,19 @@ +#ifndef PENDULUM_H +#define PENDULUM_H + +#include "../collision.h" + +typedef struct { + int id; + double x, y; + double rotCounter, angle; + + Mask mask; +} Pendulum; + +void createPendulum(int x, int y, int side); + +void pendulumStep(Pendulum* p); +void pendulumDraw(Pendulum* p); + +#endif \ No newline at end of file diff --git a/src/enemies/podoboo.c b/src/enemies/podoboo.c new file mode 100644 index 0000000..1dfc30b --- /dev/null +++ b/src/enemies/podoboo.c @@ -0,0 +1,176 @@ +#include "podoboo.h" +#include "../PHL.h" +#include "../hero.h" +#include "../game.h" +#include "../effect.h" +#include +#include + +void podobooStep(Podoboo* p); +void podobooDraw(Podoboo* p); + +void createPodoboo(int x, int y, int offset, int height) +{ + int i; + for (i = 0; i < MAX_ENEMIES; i++) { + if (enemies[i] == NULL) { + Enemy* e = malloc(sizeof *e); + Podoboo* p = malloc(sizeof *p); + + p->id = i; + + p->x = x; + p->y = p->ystart = y; + + p->hp = 2; + p->blink = 0; + + p->yoffset = p->rot = 0; + + p->vsp = 0; + p->grav = 0.13; + + p->jumpheight = -5; + /* + if (height == 1) { + p->jumpheight = -5.4; + } + */ + if (height == 1) { + p->jumpheight = -7; + } + + p->imageIndex = 0; + + p->timer = 30 * offset; + p->state = 0; + + e->data = p; + e->enemyStep = podobooStep; + e->enemyDraw = podobooDraw; + e->type = 15; + + enemies[i] = e; + i = MAX_ENEMIES; + } + } +} + +void podobooStep(Podoboo* p) +{ + //Blinking + { + if (p->blink > 0) { + p->blink -= 1; + } + } + + p->timer -= 1; + + //Patterns + { + //Float in lava + if (p->state == 0) + { + //Animate + p->imageIndex += 0.1; + if (p->imageIndex >= 2) { + p->imageIndex -= 2; + } + + //Bob movement + p->rot += 5; + if (p->rot >= 360) { + p->rot -= 360; + } + p->y = p->ystart + (5 * sin(p->rot * 3.14159 / 180)); + + //Jump + if (p->timer <= 0) { + p->state = 1; + createLavaSplash(p->x + 20, p->y); + + p->y = p->ystart; + p->vsp = p->jumpheight; + } + } + //In air + else if (p->state == 1) + { + //Animate + p->imageIndex += 0.25; + if (p->imageIndex >= 3) { + p->imageIndex -= 3; + } + + //Movement + p->y += p->vsp; + p->vsp += p->grav; + + //Land in lava again + if (p->vsp > 0 && p->y >= p->ystart) { + createLavaSplash(p->x + 20, p->y); + p->y = p->ystart; + p->state = 0; + p->vsp = 0; + p->timer = 60; + } + } + + } + + //Create Mask + Mask mask; + { + mask.unused = mask.circle = 0; + mask.w = mask.h = 30; + mask.x = p->x + 5; + mask.y = p->y + 5; + } + + //Collide with hero + { + if (checkCollision(mask, getHeroMask())) { + heroHit(15, mask.x + (mask.w / 2)); + } + } + + //Weapon collision + { + int i; + for (i = 0; i < MAX_WEAPONS; i++) { + if (weapons[i] != NULL) { + if (weapons[i]->cooldown == 0) { + if (checkCollision(mask, weapons[i]->weaponMask)) { + weaponHit(weapons[i]); + p->hp -= 1; + p->blink = 15; + + //Death + if (p->hp <= 0) { + createEffect(2, p->x - 12, p->y - 12); + spawnCollectable(p->x + 20, p->y); + enemyDestroy(p->id); + } + + i = MAX_WEAPONS; + } + } + } + } + } + +} + +void podobooDraw(Podoboo* p) +{ + if (p->blink % 2 == 0) { + int thisImage = p->imageIndex; + + if (p->state == 1) { + thisImage += 2; + } + + PHL_DrawSurfacePart(p->x, p->y, 280 + (40 * thisImage), 520, 40, 40, images[imgEnemies]); + } +} \ No newline at end of file diff --git a/src/enemies/podoboo.h b/src/enemies/podoboo.h new file mode 100644 index 0000000..eec8ba6 --- /dev/null +++ b/src/enemies/podoboo.h @@ -0,0 +1,20 @@ +#ifndef PODOBOO_H +#define PODOBOO_H + +typedef struct { + int id; + double x, y; + int ystart; + int hp; + int blink; + int rot; + double yoffset; + double vsp, grav; + double jumpheight; + double imageIndex; + int timer, state; +} Podoboo; + +void createPodoboo(int x, int y, int offset, int height); + +#endif \ No newline at end of file diff --git a/src/enemies/poisonknight.c b/src/enemies/poisonknight.c new file mode 100644 index 0000000..5997ce4 --- /dev/null +++ b/src/enemies/poisonknight.c @@ -0,0 +1,318 @@ +#include "poisonknight.h" +#include "../game.h" +#include "../hero.h" +#include + +void poisonknightStep(Poisonknight* p); +void poisonknightDraw(Poisonknight* p); + +void goopStep(Goop* g); +void goopDraw(Goop* g); + +void createPoisonknight(int x, int y) +{ + int i; + for (i = 0; i < MAX_ENEMIES; i++) { + if (enemies[i] == NULL) { + Enemy* e = malloc(sizeof *e); + Poisonknight* p = malloc(sizeof *p); + p->id = i; + p->hp = 2; + + p->x = x; + p->y = y; + + p->imageIndex = 0; + + p->dir = 1; + if (herox < p->x) { + p->dir = -1; + } + + p->blink = 0; + p->timer = 0; + p->state = 0; + + e->data = p; + e->enemyStep = poisonknightStep; + e->enemyDraw = poisonknightDraw; + e->type = 29; + + enemies[i] = e; + i = MAX_ENEMIES; + } + } +} + +void poisonknightStep(Poisonknight* p) +{ + char dead = 0; + + //Animate + { + p->imageIndex += 0.1; + if (p->imageIndex >= 2) { + p->imageIndex -= 2; + } + + if (p->blink > 0) { + p->blink -= 1; + } + } + + //Setup Mask + Mask mask; + { + mask.circle = mask.unused = 0; + mask.w = 38; + mask.h = 36; + mask.x = p->x + ((40 - mask.w) / 2); + mask.y = p->y + (40 - mask.h); + } + + //Walk + if (p->state == 0) { + double hsp = 1; + + p->x += hsp * p->dir; + mask.x = p->x + ((40 - mask.w) / 2); + + //Hit wall + if (checkTileCollision(1, mask) == 1) { + p->dir *= -1; + } + + //On wall edge + else { + mask.x += mask.w * p->dir; + mask.y += 10; + + PHL_Rect collide = getTileCollision(1, mask); + if (collide.x == -1) { + collide = getTileCollision(3, mask); + } + if (collide.x == -1) { + p->dir *= -1; + } + } + + //Hero is close enough + { + if (p->timer <= 0) { + Mask area; + area.circle = area.unused = 0; + area.x = p->x - 110; + area.y = p->y; + area.w = 260; + area.h = 40; + + if (checkCollision(area, getHeroMask()) == 1) { + p->dir = 1; + if (herox < p->x + 20) { + p->dir = -1; + } + p->imageIndex = 1; + p->timer = 0; + p->state = 1; + } + + }else{ + p->timer -= 1; + } + } + } + + //*beat* + else if (p->state == 1) + { + //Animate + p->imageIndex = 1; + + p->timer += 1; + if (p->timer >= 15) { + p->state = 2; + p->timer = 0; + p->imageIndex = 2; + } + } + + //Shoot goop + else if (p->state == 2) + { + //Shoot goop + if (p->timer == 0) { + PHL_PlaySound(sounds[sndPi05], CHN_ENEMIES); + createGoop(p->x + (20 * p->dir), p->y - 2, p->dir); + } + + //Animate + p->imageIndex = 2; + + p->timer += 1; + if (p->timer >= 25) { + p->state = 0; + p->timer = 240; + } + } + + //Update Mask + mask.x = p->x + ((40 - mask.w) / 2); + mask.y = p->y + (40 - mask.h); + + + //Collide with hero + { + if (checkCollision(mask, getHeroMask()) == 1) { + heroHit(15, mask.x + (mask.w / 2)); + } + } + + //Weapon collision + { + int i; + for (i = 0; i < MAX_WEAPONS; i++) { + if (weapons[i] != NULL) { + if (weapons[i]->cooldown == 0) { + if (checkCollision(mask, weapons[i]->weaponMask)) { + weaponHit(weapons[i]); + + p->hp -= 1; + p->blink = 15; + + if (p->hp <= 0) { + dead = 1; + createEffect(2, p->x - 12, p->y - 6); + spawnCollectable(p->x + 20, p->y); + } + } + } + } + } + } + + //Destroy object + { + if (dead == 1) { + enemyDestroy(p->id); + } + } +} + +void poisonknightDraw(Poisonknight* p) +{ + if (p->blink % 2 == 0) { + int cropX = (int)p->imageIndex * 40; + + if (p->dir == -1) { + cropX += 120; + } + + PHL_DrawSurfacePart(p->x, p->y, cropX, 280, 40, 40, images[imgEnemies]); + } +} + +//Poison Goop +void createGoop(int x, int y, int dir) +{ + int i; + for (i = 0; i < MAX_ENEMIES; i++) { + if (enemies[i] == NULL) { + Enemy* e = malloc(sizeof *e); + Goop* g = malloc(sizeof *g); + g->id = i; + + g->x = x; + g->y = y; + + g->dir = dir; + + g->imageIndex = 0; + + e->data = g; + e->enemyStep = goopStep; + e->enemyDraw = goopDraw; + e->type = -1; + + enemies[i] = e; + i = MAX_ENEMIES; + } + } +} + +void goopStep(Goop* g) +{ + char dead = 0; + + //Animate + { + g->imageIndex += 0.33; + if (g->imageIndex >= 3) { + g->imageIndex -= 3; + } + } + + //Movement + { + int hsp = 4; + g->x += hsp * g->dir; + } + + //Setup Mask + Mask mask; + { + mask.circle = mask.unused = 0; + mask.w = 22; + mask.h = 22; + mask.x = g->x + ((40 - mask.w) / 2); + mask.y = g->y + ((40 - mask.h) / 2); + } + + //Collide with hero + { + //Collide with shield + if (checkCollision(mask, shieldMask) == 1) { + dead = 1; + PHL_PlaySound(sounds[sndHit07], CHN_EFFECTS); + createEffect(1, g->x, g->y); + } + //Collide with hero + else if (checkCollision(mask, getHeroMask()) == 1) { + if (heroHit(10, mask.x + (mask.w / 2)) == 1) { + heroPoison(); + } + } + } + + //Collide with wall + { + if (checkTileCollision(1, mask) == 1) { + dead = 1; + createEffect(1, g->x, g->y); + } + } + + //Destroy if out of room + { + if (g->x + 40 < 0 || g->x > 640) { + dead = 1; + } + } + + //Destroy object + { + if (dead == 1) { + enemyDestroy(g->id); + } + } +} + +void goopDraw(Goop* g) +{ + int cropX = 400 + ((int)g->imageIndex * 40); + + if (g->dir == -1) { + cropX += 120; + } + + PHL_DrawSurfacePart(g->x, g->y, cropX, 520, 40, 40, images[imgMisc20]); +} \ No newline at end of file diff --git a/src/enemies/poisonknight.h b/src/enemies/poisonknight.h new file mode 100644 index 0000000..13350d0 --- /dev/null +++ b/src/enemies/poisonknight.h @@ -0,0 +1,26 @@ +#ifndef POISONKNIGHT_H +#define POISONKNIGHT_H + +typedef struct { + int id; + int hp; + double x, y; + double imageIndex; + int dir; + int blink; + int timer; + int state; +} Poisonknight; + +void createPoisonknight(int x, int y); + +typedef struct { + int id; + double x, y; + int dir; + double imageIndex; +} Goop; + +void createGoop(int x, int y, int dir); + +#endif \ No newline at end of file diff --git a/src/enemies/pumpkin.c b/src/enemies/pumpkin.c new file mode 100644 index 0000000..8feec0f --- /dev/null +++ b/src/enemies/pumpkin.c @@ -0,0 +1,375 @@ +#include "pumpkin.h" +#include "../game.h" +#include "../hero.h" +#include + +void pumpkinenemyStep(Pumpkinenemy* p); +void pumpkinenemyDraw(Pumpkinenemy* p); + +void pumpkinheadStep(Pumpkinhead* p); +void pumpkinheadDraw(Pumpkinhead* p); + +void createPumpkinenemy(int x, int y) +{ + int i; + for (i = 0; i < MAX_ENEMIES; i++) { + if (enemies[i] == NULL) { + Enemy* e = malloc(sizeof *e); + Pumpkinenemy* p = malloc(sizeof *p); + + p->id = i; + + p->hp = 3; + p->blink = 0; + + p->x = x; + p->y = y; + + p->dir = 1; + if (herox < p->x + 20) { + p->dir = -1; + } + + p->imageIndex = 0; + + p->state = 0; + p->timer = 0; + + e->data = p; + e->enemyStep = pumpkinenemyStep; + e->enemyDraw = pumpkinenemyDraw; + e->type = 32; + + enemies[i] = e; + i = MAX_ENEMIES; + } + } +} + +void pumpkinenemyStep(Pumpkinenemy* p) +{ + //Setup Mask + Mask mask; + { + mask.unused = mask.circle = 0; + mask.w = 20; + mask.h = 38; + mask.x = p->x + ((40 - mask.w) / 2); + mask.y = p->y + (40 - mask.h); + } + + //Animate + { + p->imageIndex += 0.1; + if (p->imageIndex >= 2) { + p->imageIndex -= 2; + } + + if (p->blink > 0) { + p->blink -= 1; + } + } + + //Walking + if (p->state == 0) + { + double hsp = 0.5; + p->x += hsp * p->dir; + mask.x = p->x + ((40 - mask.w) / 2); + + //Hit wall + { + if (checkTileCollision(1, mask) == 1 || mask.x > 640 || mask.x + mask.w < 0) { + p->dir *= -1; + } + } + + //On edge + { + mask.x += mask.w * p->dir; + mask.y += 20; + + PHL_Rect collide = getTileCollision(1, mask); + if (collide.x == -1) { + collide = getTileCollision(3, mask); + } + + if (collide.x == -1) { + p->dir *= -1; + } + } + + //Player is close + { + if (p->timer <= 0) { + Mask area; + { + area.circle = area.unused = 0; + area.w = 240; + area.h = 80; + area.x = p->x - 100; + area.y = p->y - 40; + } + if (checkCollision(area, getHeroMask()) == 1) { + p->state = 1; + p->timer = 0; + p->dir = 1; + if (herox < p->x + 20) { + p->dir = -1; + } + } + + }else{ + p->timer -= 1; + } + } + + } + + //Deheaded + else if (p->state == 1) { + //Animate + { + p->imageIndex = 0; + if (p->timer >= 15) { + p->imageIndex = 2; + } + } + + p->timer += 1; + if (p->timer == 15) { + createPumpkinhead(p->x, p->y - 6, p->dir); + PHL_PlaySound(sounds[sndPi05], CHN_ENEMIES); + } + + if (p->timer >= 40) { + p->state = 0; + p->imageIndex = 0; + p->timer = 300; + } + } + + //Update Mask + mask.x = p->x + ((40 - mask.w) / 2); + mask.y = p->y + (40 - mask.h); + + //Hero Collision + { + if (checkCollision(mask, getHeroMask()) == 1) { + heroHit(15, mask.x + (mask.w / 2)); + } + } + + //Weapon Collision + { + int i; + for (i = 0; i < MAX_WEAPONS; i++) { + if (weapons[i] != NULL) { + if (weapons[i]->cooldown == 0) { + if (checkCollision(mask, weapons[i]->weaponMask)) { + weaponHit(weapons[i]); + + p->hp -= 1; + p->blink = 15; + + //Death + if (p->hp <= 0) { + createEffect(2, p->x - 12, p->y - 6); + spawnCollectable(p->x + 20, p->y); + enemyDestroy(p->id); + } + + i = MAX_WEAPONS; + } + } + } + } + } + +} + +void pumpkinenemyDraw(Pumpkinenemy* p) +{ + if (p->blink % 2 == 0) { + int cropX = (int)p->imageIndex * 40; + + if (p->dir == -1) { + cropX += 120; + } + + PHL_DrawSurfacePart(p->x, p->y, cropX, 560, 40, 40, images[imgEnemies]); + } +} + + +//Pumpkin bomb head +void createPumpkinhead(int x, int y, int dir) +{ + int i; + for (i = 0; i < MAX_ENEMIES; i++) { + if (enemies[i] == NULL) { + Enemy* e = malloc(sizeof *e); + Pumpkinhead* p = malloc(sizeof *p); + p->id = i; + + p->dir = dir; + + p->x = x; + p->y = y; + + p->vsp = -2; + + p->imageIndex = 0; + + p->state = 0; + p->timer = 0; + + e->data = p; + e->enemyStep = pumpkinheadStep; + e->enemyDraw = pumpkinheadDraw; + e->type = -1; + + enemies[i] = e; + i = MAX_ENEMIES; + } + } +} + +void pumpkinheadStep(Pumpkinhead* p) +{ + char dead = 0; + + //Setup Mask + Mask mask; + { + mask.circle = mask.unused = 0; + mask.w = 20; + mask.h = 22; + mask.x = p->x + ((40 - mask.w) / 2); + mask.y = p->y + ((40 - mask.h) / 2); + } + + //Pumpkin head + if (p->state == 0) + { + char explode = 0; + + //Animate + { + p->imageIndex += 0.1; + if (p->imageIndex >= 2) { + p->imageIndex -= 2; + } + } + + //Movement + { + int hsp = 3; + p->x += hsp * p->dir; + mask.x = p->x + ((40 - mask.w) / 2); + + PHL_Rect collide = getTileCollision(1, mask); + if (collide.x != -1) { + p->x = collide.x + 20 - ((20 + (mask.w / 2)) * p->dir) - 20; + mask.x = p->x + ((40 - mask.w) / 2); + p->dir *= -1; + } + + double grav = 0.15; + p->y += p->vsp; + p->vsp += grav; + mask.y = p->y + ((40 - mask.h) / 2); + + collide = getTileCollision(1, mask); + if (collide.x == -1) { + collide = getTileCollision(3, mask); + } + + if (collide.x != -1) { + p->y = collide.y - 40; + explode = 1; + } + } + + //Update Mask + mask.x = p->x + ((40 - mask.w) / 2); + mask.y = p->y + ((40 - mask.h) / 2); + + //Explode + { + if (explode == 1) { + PHL_PlaySound(sounds[sndBom03], CHN_ENEMIES); + p->state = 1; + p->imageIndex = 0; + p->timer = 0; + } + } + + //Outside of room + { + if (mask.y > 480 || mask.x > 640 || mask.x + mask.w < 0) { + dead = 1; + } + } + + } + + //Explosion + else if (p->state == 1) + { + //Update Mask + { + mask.w = 68; + mask.h = 66; + mask.x = p->x - 44 + 64 - (mask.w / 2); + mask.y = p->y - 44 + (84 - mask.h); + } + + //Animate + { + p->imageIndex += 0.33; + if (p->imageIndex >= 12) { + dead = 1; + } + } + + //Hero Collision + { + if (checkCollision(mask, getHeroMask()) == 1) { + heroHit(40, mask.x + (mask.w / 2)); + } + } + } + + //Destroy object + { + if (dead == 1) { + enemyDestroy(p->id); + } + } +} + +void pumpkinheadDraw(Pumpkinhead* p) +{ + if (p->state == 0) { + int cropX = (int)p->imageIndex * 40; + + if (p->dir == -1) { + cropX += 80; + } + + PHL_DrawSurfacePart(p->x, p->y, cropX, 240, 40, 40, images[imgEnemies]); + } + + if (p->state == 1) { + int cropX = (int)p->imageIndex * 128; + int cropY = 0; + + while (cropX >= 640) { + cropX -= 640; + cropY += 96; + } + + PHL_DrawSurfacePart(p->x - 44, p->y - 44, cropX, cropY, 128, 96, images[imgExplosion]); + } +} \ No newline at end of file diff --git a/src/enemies/pumpkin.h b/src/enemies/pumpkin.h new file mode 100644 index 0000000..6a2b87c --- /dev/null +++ b/src/enemies/pumpkin.h @@ -0,0 +1,27 @@ +#ifndef PUMPKIN_H +#define PUMPKIN_H + +typedef struct { + int id; + int hp; + int blink; + double x, y; + int dir; + double imageIndex; + int state, timer; +} Pumpkinenemy; + +void createPumpkinenemy(int x, int y); + +typedef struct { + int id; + int dir; + double x, y; + double vsp; + double imageIndex; + int state, timer; +} Pumpkinhead; + +void createPumpkinhead(int x, int y, int dir); + +#endif \ No newline at end of file diff --git a/src/enemies/seal.c b/src/enemies/seal.c new file mode 100644 index 0000000..f0a3fa9 --- /dev/null +++ b/src/enemies/seal.c @@ -0,0 +1,204 @@ +#include "seal.h" +#include "../game.h" +#include "../enemy.h" +#include "../collision.h" +#include "../hero.h" +#include + +void sealStep(Seal* s); +void sealDraw(Seal* s); + +void createSeal(int x, int y) +{ + int i; + for (i = 0; i < MAX_ENEMIES; i++) { + if (enemies[i] == NULL) { + Enemy* e = malloc(sizeof *e); + Seal* s = malloc(sizeof *s); + s->id = i; + s->hp = 2; + + s->x = x; + s->y = y; + + s->imageIndex = 0; + + s->dir = 1; + if (x + 20 > herox) { + s->dir = -1; + } + + s->state = 0; + s->timer = 0; + + s->invincible = 0; + + e->data = s; + e->enemyStep = sealStep; + e->enemyDraw = sealDraw; + e->type = 19; + + enemies[i] = e; + i = MAX_ENEMIES; + } + } +} + +void sealStep(Seal* s) +{ + if (s->invincible > 0) { + s->invincible -= 1; + } + + Mask mask; + mask.unused = mask.circle = 0; + mask.w = mask.h = 28; + mask.x = s->x + ((40 - mask.w) / 2); + mask.y = s->y + (40 - mask.h); + + //Walk + if (s->state == 0) + { + //Animate + s->imageIndex += 0.1; + if (s->imageIndex >= 2) { + s->imageIndex -= 2; + } + + //Check if hit a wall + if (checkTileCollision(1, mask) == 1) { + s->dir *= -1; + }else{ + //Check if on edge + mask.x += mask.w * s->dir; + mask.y += mask.h; + + PHL_Rect collide = getTileCollision(1, mask); + if (collide.x == -1) { + collide = getTileCollision(3, mask); + } + if (collide.x == -1) { + s->dir *= -1; + } + + mask.x = s->x + ((40 - mask.w) / 2); + mask.y = s->y + (40 - mask.h); + } + + //Movement + s->x += 0.5 * s->dir; + + if (s->timer <= 0) { + //Check if player is close enough + Mask area; + area.unused = area.circle = 0; + area.x = s->x - 40; + area.y = s->y; + area.w = 120; + area.h = 120; + + if (checkCollision(area, getHeroMask()) == 1) { + s->state = 1; + s->timer = -1; + } + }else{ + s->timer -= 1; + } + } + + //Rear back + else if (s->state == 1) + { + //Setup + if (s->timer == -1) { + s->imageIndex = 4; + s->timer = 20; + } + + s->timer -= 1; + + if (s->timer <= 0) { + s->state = 2; + s->timer = -1; + s->imageIndex = 0; + } + } + + //Tounge attack + else if (s->state == 2) + { + //Setup + if (s->timer == -1) { + s->timer = 0; + PHL_PlaySound(sounds[sndGet01], CHN_ENEMIES); + } + + //Animate + int animation[41] = {0, 1, 1, 2, 2, 3, 3, 4, 4, 4, + 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, + 4, 4, 4, 4, 4, 4, 4, 4, 3, 3, + 2, 2, 1, 1, 0, 0, 5, 5, 5, 5, 5 }; + + s->imageIndex = animation[(int)s->timer]; + + //Update mask height to fit tounge + int len[6] = { 18, 38, 58, 64, 66, 0}; + mask.h += len[(int)s->imageIndex]; + + s->timer += 1; + + if (s->timer >= 41) { + s->state = 0; + s->timer = 120; + s->imageIndex = 0; + } + } + + //Hit Player + if (checkCollision(mask, getHeroMask())) { + heroHit(10, s->x + 20); + } + + int i; + for (i = 0; i < MAX_WEAPONS; i++) { + if (weapons[i] != NULL) { + if (weapons[i]->cooldown == 0) { + if (checkCollision(mask, weapons[i]->weaponMask)) { + s->hp -= 1; + s->invincible = 15; + weaponHit(weapons[i]); + if (s->hp <= 0) { + enemyDestroy(s->id); + createEffect(2, s->x - 12, s->y - 6); + spawnCollectable(s->x + 20, s->y); + } + i = MAX_WEAPONS; + } + } + } + } +} + +void sealDraw(Seal* s) +{ + if (s->invincible % 2 == 0) { + int cx = 400 + ((int)s->imageIndex * 40); + + if (s->state == 0) { + if (s->dir == -1) { + cx += 80; + } + } + + if (s->state == 2) { + cx = 600; + } + + PHL_DrawSurfacePart(s->x, s->y, cx, 200, 40, 40, images[imgEnemies]); + + //Draw tounge + if (s->state == 2) { + PHL_DrawSurfacePart(s->x, s->y + 28, 200 + ((int)s->imageIndex * 40), 0, 40, 80, images[imgMisc2040]); + } + } +} \ No newline at end of file diff --git a/src/enemies/seal.h b/src/enemies/seal.h new file mode 100644 index 0000000..ea1dbe8 --- /dev/null +++ b/src/enemies/seal.h @@ -0,0 +1,14 @@ +#ifndef SEAL_H +#define SEAL_H + +typedef struct { + int id, hp; + double x, y; + double imageIndex; + int dir, state, timer; + int invincible; +} Seal; + +void createSeal(int x, int y); + +#endif \ No newline at end of file diff --git a/src/enemies/skeleton.c b/src/enemies/skeleton.c new file mode 100644 index 0000000..5d88605 --- /dev/null +++ b/src/enemies/skeleton.c @@ -0,0 +1,298 @@ +#include "skeleton.h" +#include "../enemy.h" +#include "../game.h" +#include "../PHL.h" +#include "../hero.h" +#include + +void skeletonStep(Skeleton* s); +void skeletonDraw(Skeleton* s); + +void boneStep(Bone* b); +void boneDraw(Bone* b); + +void createSkeleton(int x, int y, int dir) +{ + int i; + for (i = 0; i < MAX_ENEMIES; i++) { + if (enemies[i] == NULL) { + Enemy* e = malloc(sizeof *e); + Skeleton* s = malloc(sizeof *s); + s->id = i; + + s->x = x; + s->y = y; + + s->imageIndex = 0; + + s->dir = 1; + if (dir == 1) { + s->dir = -1; + } + + s->hsp = 0.5 * s->dir; + + s->hp = 2; + + s->state = 0; + s->timer = 0; + s->invincible = 0; + + s->mask.unused = s->mask.circle = 0; + s->mask.w = 24; + s->mask.h = 36; + s->mask.x = s->x + 8; + s->mask.y = s->y + 4; + + e->data = s; + e->enemyStep = skeletonStep; + e->enemyDraw = skeletonDraw; + e->type = 17; + + enemies[i] = e; + i = MAX_ENEMIES; + } + } +} + +void skeletonStep(Skeleton* s) +{ + if (s->invincible > 0) { + s->invincible -= 1; + } + + //Collide with wall + if (checkTileCollision(1, s->mask) == 1) { + s->hsp *= -1; + }else{ + //Check if on ledge + int tempdir = 1; + if (s->hsp < 0) { + tempdir = -1; + } + s->mask.x += tempdir * s->mask.w; + s->mask.y += 10; + if (checkTileCollision(1, s->mask) == 0 && checkTileCollision(3, s->mask) == 0) { + s->hsp *= -1; + } + s->mask.y -= 10; + s->mask.x = s->x + 8; + } + + s->x += s->hsp; + + if (s->timer >= 0) { + s->timer -= 1; + } + + //Walk around + if (s->state == 0) + { + s->imageIndex += 0.1; + + if (s->hsp < 0) { + s->dir = -1; + s->hsp = -0.5; + }else if (s->hsp > 0) { + s->dir = 1; + s->hsp = 0.5; + }else{ + s->hsp = 0.5 * s->dir; + } + + if (s->timer <= 0) { + //If hero is too close + Mask area; + area.unused = area.circle = 0; + area.x = s->x - 80; + area.y = s->y - 20; + area.w = 200; + area.h = 80; + + if (checkCollision(area, getHeroMask()) == 1) { + s->state = 1; + s->timer = 30; + s->hsp = 0; + + s->dir = 1; + if (herox < s->mask.x + (s->mask.w / 2)) { + s->dir = -1; + } + } + } + } + //Alert + else if (s->state == 1) + { + s->hsp = 0; + s->imageIndex += 0.1; + + if (s->timer <= 0) { + s->state = 2; + s->hsp = 2.5 * -s->dir; + PHL_PlaySound(sounds[sndPi05], CHN_ENEMIES); + } + } + //Slide backwards + else if (s->state == 2) + { + s->imageIndex = 0; + double fric = 0.075; + if (s->hsp > 0) { + s->hsp -= fric; + if (s->hsp <= 0) { s->hsp = 0; } + } + else if (s->hsp < 0) { + s->hsp += fric; + if (s->hsp >= 0) { s->hsp = 0; } + } + + if (s->hsp == 0) { + s->state = 3; + s->timer = 30; + createBone(s->x, s->y, s->dir); + PHL_PlaySound(sounds[sndShot05], CHN_ENEMIES); + } + } + //Throw bone + else if (s->state == 3) + { + s->imageIndex += 0.1; + if (s->timer <= 0) { + s->timer = 0; + s->state = 0; + } + } + + if (s->imageIndex >= 2) { + s->imageIndex -= 2; + } + + //Update mask + s->mask.x = s->x + 8; + + //Hit Player + if (checkCollision(s->mask, getHeroMask())) { + heroHit(10, s->x + 20); + } + + int i; + for (i = 0; i < MAX_WEAPONS; i++) { + if (weapons[i] != NULL) { + if (weapons[i]->cooldown == 0) { + if (checkCollision(s->mask, weapons[i]->weaponMask)) { + s->hp -= 1; + s->invincible = 15; + weaponHit(weapons[i]); + if (s->hp <= 0) { + createRockSmash(s->x + 20, s->y + 20); + spawnCollectable(s->x + 20, s->mask.y + s->mask.h - 40); + enemyDestroy(s->id); + } + i = MAX_WEAPONS; + } + } + } + } +} + +void skeletonDraw(Skeleton* s) +{ + if (s->invincible % 2 == 0) { + int dx = 160 + ((int)s->imageIndex * 40); + + if (s->dir == -1) { + dx += 80; + } + + PHL_DrawSurfacePart(s->x, s->y, dx, 240, 40, 40, images[imgEnemies]); + } +} + + + +void createBone(int x, int y, int dir) +{ + int i; + for (i = 0; i < MAX_ENEMIES; i++) { + if (enemies[i] == NULL) { + Enemy* e = malloc(sizeof *e); + Bone* b = malloc(sizeof *b); + b->id = i; + + b->x = x; + b->y = y; + + b->hsp = dir * 0.75; + b->vsp = -4; + b->grav = 0.1; + + b->imageIndex = 0; + + b->mask.unused = 0; + b->mask.circle = 1; + b->mask.w = 12; + b->mask.h = 12; + b->mask.x = b->x + 20; + b->mask.y = b->y + 20; + + e->data = b; + e->enemyStep = boneStep; + e->enemyDraw = boneDraw; + e->type = -1; + + enemies[i] = e; + i = MAX_ENEMIES; + } + } +} + +void boneStep(Bone* b) +{ + if (b->hsp < 0) { + b->imageIndex += 0.25; + if (b->imageIndex >= 4) { + b->imageIndex -= 4; + } + } + else{ + b->imageIndex -= 0.25; + if (b->imageIndex < 0) { + b->imageIndex += 4; + } + } + + b->x += b->hsp; + + b->y += b->vsp; + b->vsp += b->grav; + + //Update Mask + b->mask.x = b->x + 20; + b->mask.y = b->y + 20; + + if (b->y > 480) { + enemyDestroy(b->id); + } + + //Hit Player + if (checkCollision(b->mask, shieldMask)) { + enemyDestroy(b->id); + PHL_PlaySound(sounds[sndHit07], CHN_EFFECTS); + createEffect(1, b->x, b->y); + }else{ + if (checkCollision(b->mask, getHeroMask())) { + heroHit(10, b->x + 20); + } + } +} + +void boneDraw(Bone* b) +{ + int img = 320 + ((int)b->imageIndex * 40); + if (b->hsp > 0) { + img += 160; + } + + PHL_DrawSurfacePart(b->x, b->y, img, 240, 40, 40, images[imgEnemies]); +} \ No newline at end of file diff --git a/src/enemies/skeleton.h b/src/enemies/skeleton.h new file mode 100644 index 0000000..a7b0816 --- /dev/null +++ b/src/enemies/skeleton.h @@ -0,0 +1,31 @@ +#ifndef SKELETON_H +#define SKELETON_H + +#include "../collision.h" + +typedef struct { + int id; + double x, y; + double hsp; + double imageIndex; + int dir; + int hp; + int state, timer, invincible; + + Mask mask; +} Skeleton; + +void createSkeleton(int x, int y, int dir); + +typedef struct { + int id; + double x, y; + double hsp, vsp, grav; + double imageIndex; + + Mask mask; +} Bone; + +void createBone(int x, int y, int dir); + +#endif \ No newline at end of file diff --git a/src/enemies/skull.c b/src/enemies/skull.c new file mode 100644 index 0000000..7ee5836 --- /dev/null +++ b/src/enemies/skull.c @@ -0,0 +1,174 @@ +#include "skull.h" +#include "../enemy.h" +#include "../PHL.h" +#include "../game.h" +#include "../hero.h" +#include +#include + +void skullStep(Skull* s); +void skullDraw(Skull* s); + +void createSkull(int x, int y) +{ + int i; + for (i = 0; i < MAX_ENEMIES; i++) { + if (enemies[i] == NULL) { + Enemy* e = malloc(sizeof *e); + Skull* s = malloc(sizeof *s); + s->id = i; + + //X/Y in center of sprite + s->x = x + 20; + s->y = y + 20; + s->yoffset = 0; + + s->rot = 0; + s->state = 0; + s->timer = 0; + s->imageIndex = 0; + s->dir = 0; + + e->data = s; + e->enemyStep = skullStep; + e->enemyDraw = skullDraw; + e->type = 12; + + enemies[i] = e; + i = MAX_ENEMIES; + } + } +} + +void skullStep(Skull* s) +{ + double imageSpeed = 0; + + //Wait + if (s->state == 0) + { + imageSpeed = 0.2; + + if (s->timer > 0) { + s->timer -= 1; + }else{ + Mask tempmask; + + tempmask.unused = tempmask.circle = 0; + tempmask.x = s->x - 100; + tempmask.y = s->y - 100; + tempmask.w = tempmask.h = 200; + + if (checkCollisionXY(tempmask, herox, heroy + 20)) { + + //Calculate distance + //int dis = sqrt(pow(s->x - herox, 2) + pow(s->y - (heroy + 20), 2)); + //if (dis <= 100) { + s->state = 1; + //s->dir = (rand() % 8) * 45; + s->dir = (rand() % 360) + 1; + PHL_PlaySound(sounds[sndPi08], CHN_ENEMIES); + s->timer = 130; + } + } + } + + //Chase + else if (s->state == 1) + { + imageSpeed = 0.3; + + int spd = 2; + s->x += (spd * cos(s->dir * 3.14159 / 180)); + s->y += (spd * sin(s->dir * 3.14159 / 180)); + + double herodir = ((atan2((heroy + 20) - s->y, herox - s->x) * 180) / 3.14159); + if (herodir >= 360) { + herodir -= 360; + } + if (herodir < 0) { + herodir += 360; + } + + double tempdir = s->dir - herodir; + if (tempdir < 0) { + tempdir += 360; + } + + if (tempdir < 180) { + s->dir -= 2; + }else{ + s->dir += 2; + } + if (s->dir >= 360) { + s->dir -= 360; + } + if (s->dir < 0) { + s->dir += 360; + } + + s->timer -= 1; + if (s->timer <= 0) { + s->state = 0; + s->timer = 10; + } + } + + //Animate + { + s->imageIndex += imageSpeed; + if (s->imageIndex >= 4) { + s->imageIndex -= 4; + } + } + + //Hover offset + { + s->rot += 5; + if (s->rot >= 360) { + s->rot -= 360; + } + s->yoffset = (5 * sin(s->rot * 3.14159 / 180)); + } + + //Setup Mask + Mask mask; + { + mask.unused = 0; + mask.circle = 1; + mask.x = s->x; + mask.y = s->y; + mask.w = mask.h = 10; + } + + //Hero collision + { + if (checkCollision(mask, getHeroMask())) { + heroHit(15, mask.x); + } + } + + //Weapon collision + { + int i; + for (i = 0; i < MAX_WEAPONS; i++) { + if (weapons[i] != NULL) { + if (checkCollision(mask, weapons[i]->weaponMask)) { + weaponHit(weapons[i]); + + createEffect(2, s->x - 32, s->y - 32); + spawnCollectable(s->x, s->y - 20); + enemyDestroy(s->id); + + i = MAX_WEAPONS; + } + } + } + } + +} + +void skullDraw(Skull* s) +{ + PHL_DrawSurfacePart(s->x - 20, s->y + s->yoffset - 20, 480 + ((int)s->imageIndex * 40), 40, 40, 40, images[imgEnemies]); +} \ No newline at end of file diff --git a/src/enemies/skull.h b/src/enemies/skull.h new file mode 100644 index 0000000..b62ea2d --- /dev/null +++ b/src/enemies/skull.h @@ -0,0 +1,15 @@ +#ifndef SKULL_H +#define SKULL_H + +typedef struct { + int id, state, timer; + double x, y; + double yoffset; + int rot; + double dir; + double imageIndex; +} Skull; + +void createSkull(int x, int y); + +#endif \ No newline at end of file diff --git a/src/enemies/slime.c b/src/enemies/slime.c new file mode 100644 index 0000000..297b190 --- /dev/null +++ b/src/enemies/slime.c @@ -0,0 +1,228 @@ +#include "slime.h" +#include +#include "../PHL.h" +#include "../game.h" +#include "../collision.h" +#include "../hero.h" +#include "../enemy.h" +#include "../weapon.h" + +void slimeStep(Slime* s); +void slimeDraw(Slime* s); + +void createSlime(int x, int y, int type, int offset) +{ + int i; + for (i = 0; i < MAX_ENEMIES; i++) { + if (enemies[i] == NULL) { + Enemy* result = malloc(sizeof *result); + Slime* s = malloc(sizeof *s); + + s->id = i; + + s->x = x + 20; + s->y = y; + s->type = type; + s->offset = offset; + + s->hp = 1; + s->state = 0; + s->counter = 0; + s->timer = 0; + s->grav = 0.125; + s->vsp = 0; + s->hsp = 0; + s->imageIndex = 0; + + result->data = s; + result->enemyStep = slimeStep; + result->enemyDraw = slimeDraw; + result->type = 0; + + enemies[i] = result; + i = MAX_ENEMIES; + } + } +} + +void slimeStep(Slime* s) +{ + char dead = 0; + + //Stay within room + { + if (s->x > 640) { + s->x = 640; + } + + if (s->x < 0) { + s->x = 0; + } + } + + //Setup Rectangle Mask + Mask mask; + { + mask.unused = 0; + mask.circle = 0; + mask.w = 24; + mask.h = 24; + mask.x = s->x - (mask.w / 2); + mask.y = s->y + 28 - (mask.h / 2); + } + + //Idle + if (s->state == 0) + { + s->imageIndex += 0.25; + + if (s->imageIndex >= 6) { + s->imageIndex = 0; + + if (s->offset <= 0) { + s->state = 1; + + //Red/Yellow Slime + if (s->type == 1 || s->type == 2) + { + s->hsp = 1; + if (s->type == 2) { + s->hsp = 1.5; + } + if ((int)(rand() % 2) == 0) { + s->hsp *= -1; + } + } + + if (s->counter < 2) { + s->vsp = -2; + s->counter += 1; + }else{ + s->vsp = -4; + s->counter = 0; + } + }else{ + s->offset -= 1; + } + } + } + + //Jump + else if (s->state == 1) + { + //Red/Yellow Slime + if (s->type == 1 || s->type == 2) + { + s->x += s->hsp; + mask.x = s->x - (mask.w / 2); + + PHL_Rect collide = getTileCollision(1, mask); + if (collide.x != -1) { + if (s->hsp > 0) { + s->x = collide.x - (mask.w / 2); + }else if (s->hsp < 0) { + s->x = collide.x + 40 + (mask.w / 2); + } + } + + mask.x = s->x - (mask.w / 2); + } + + s->y += s->vsp; + s->vsp += s->grav; + + mask.y = s->y + 28 - (mask.h / 2); + + PHL_Rect collide = getTileCollision(1, mask); + if (collide.x == -1) { + collide = getTileCollision(3, mask); + } + if (collide.x != -1) { + if (s->vsp >= 0) { + s->state = 0; + s->hsp = 0; + s->y = collide.y - 40; + }else{ + s->y = collide.y + 40 - (40 - mask.h) + 1; + } + } + } + + //Setup Collision Mask + { + mask.unused = 0; + mask.circle = 1; + mask.w = 12; + mask.x = s->x; + mask.y = s->y + 28; + } + + //Fell in a pit + { + if (s->y > 480) { + dead = 1; + } + } + + //Collide with hero + { + if (checkCollision(mask, heroMask)) { + int dmg[3] = {10, 20, 20}; + + if (heroHit(dmg[s->type], s->x) == 1 && s->type == 2) { + heroStun(); + } + } + } + + //Sword collision + { + int i; + for (i = 0; i < MAX_WEAPONS; i++) { + if (weapons[i] != NULL) { + if (checkCollision(mask, weapons[i]->weaponMask)) { + weaponHit(weapons[i]); + + spawnCollectable(s->x, s->y + 6); + createEffect(2, s->x - 32, s->y - 12); + dead = 1; + + i = MAX_WEAPONS; + } + } + } + } + + //Destroy object + { + if (dead == 1) { + enemyDestroy(s->id); + } + } +} + +void slimeDraw(Slime* s) +{ + int cropX = 0, + cropY = 0; + + //Idle + if (s->state == 0) { + int image[6] = { 0, 1, 2, 3, 4, 6 }; + cropX = image[(int)s->imageIndex] * 40; + } + + //Jump + else if (s->state == 1) { + cropX = 200; + if (s->vsp >= 0) { + cropX += 40; + } + } + + //Color offsets + int addX[3] = {0, 280, 0}; + int addY[3] = {0, 0, 480}; + + PHL_DrawSurfacePart(s->x - 20, s->y + 12, cropX + addX[s->type], cropY + addY[s->type], 40, 40, images[imgEnemies]); +} \ No newline at end of file diff --git a/src/enemies/slime.h b/src/enemies/slime.h new file mode 100644 index 0000000..bef215b --- /dev/null +++ b/src/enemies/slime.h @@ -0,0 +1,17 @@ +#ifndef SLIME_H +#define SLIME_H + +typedef struct { + int id; + double x, y; + int type; //0 = blue | 1 = red | 2 = yellow + int offset; + double vsp, hsp, grav; + double imageIndex; + int counter, timer, state; + int hp; +} Slime; + +void createSlime(int x, int y, int type, int offset); + +#endif \ No newline at end of file diff --git a/src/enemies/slug.c b/src/enemies/slug.c new file mode 100644 index 0000000..c8fcc2c --- /dev/null +++ b/src/enemies/slug.c @@ -0,0 +1,164 @@ +#include "slug.h" +#include "../enemy.h" +#include "../game.h" +#include "../PHL.h" +#include "../hero.h" +#include + +void slugStep(Slug* s); +void slugDraw(Slug* s); + +void createSlug(int x, int y, int dir) +{ + int i; + for (i = 0; i < MAX_ENEMIES; i++) { + if (enemies[i] == NULL) { + Enemy* e = malloc(sizeof *e); + Slug* s = malloc(sizeof *s); + s->id = i; + + s->x = x; + s->y = y; + + s->imageIndex = 0; + s->vsp = 0; + + s->dir = 1; + if (dir == 1) { + s->dir = -1; + } + + e->data = s; + e->enemyStep = slugStep; + e->enemyDraw = slugDraw; + e->type = 2; + + enemies[i] = e; + i = MAX_ENEMIES; + } + } +} + +void slugStep(Slug* s) +{ + //Create Mask + Mask mask; + { + mask.circle = 0; + mask.unused = 0; + mask.w = 32; + mask.h = 24; + mask.x = s->x + ((40 - mask.w) / 2); + mask.y = s->y + (40 - mask.h); + } + + //Animate + { + s->imageIndex += 0.1; + if (s->imageIndex >= 4) { + s->imageIndex -= 4; + } + } + + //Check if on ground + int onground = 1; + { + mask.y += 1; + if (checkTileCollision(1, mask) == 0 && checkTileCollision(3, mask) == 0) { + onground = 0; + } + mask.y -= 1; + } + + if (onground == 0) { + double grav = 0.2; + + //Fall + { + s->y += s->vsp; + s->vsp += grav; + } + + //Land on ground + { + mask.y = mask.y + (40 - mask.h); + PHL_Rect collide = getTileCollision(1, mask); + if (collide.x == -1) { + collide = getTileCollision(3, mask); + } + if (collide.x != -1) { + s->y = collide.y - 40; + s->vsp = 0; + } + } + }else{ + //Check if on ledge + { + mask.x += mask.w * s->dir; + mask.y += 1; + + PHL_Rect collide = getTileCollision(1, mask); + if (collide.x == -1) { + collide = getTileCollision(3, mask); + } + if (collide.x == -1) { + s->dir *= -1; + } + } + } + + //Horizontal movement + double hsp = 0.5; + { + s->x += s->dir * hsp; + } + + //Check if hit a wall + { + mask.x = s->x + ((40 - mask.w) / 2); + mask.y = s->y + (40 - mask.h); + + PHL_Rect collide = getTileCollision(1, mask); + if (collide.x != -1) { + s->dir *= -1; + } + } + + //Hit Player + { + if (checkCollision(mask, getHeroMask()) == 1) { + heroHit(15, mask.x + (mask.w / 2)); + } + } + + //Weapon collision + { + int i; + for (i = 0; i < MAX_WEAPONS; i++) { + if (weapons[i] != NULL) { + if (checkCollision(mask, weapons[i]->weaponMask) == 1) { + weaponHit(weapons[i]); + + createEffect(2, s->x - 12, s->y - 6); + spawnCollectable(s->x + 20, s->y); + enemyDestroy(s->id); + + i = MAX_WEAPONS; + } + } + } + } + +} + +void slugDraw(Slug* s) +{ + int anim[4] = { 1, 0, 2, 0 }; + + int cropx = anim[(int)s->imageIndex] * 40; + if (s->dir == -1) { + cropx += 120; + } + + PHL_DrawSurfacePart(s->x, s->y + 10, cropx, 40, 40, 40, images[imgEnemies]); +} \ No newline at end of file diff --git a/src/enemies/slug.h b/src/enemies/slug.h new file mode 100644 index 0000000..77c2e14 --- /dev/null +++ b/src/enemies/slug.h @@ -0,0 +1,13 @@ +#ifndef SLUG_H +#define SLUG_H + +typedef struct { + int id; + int dir; + double x, y, vsp; + double imageIndex; +} Slug; + +void createSlug(int x, int y, int dir); + +#endif \ No newline at end of file diff --git a/src/enemies/thwomp.c b/src/enemies/thwomp.c new file mode 100644 index 0000000..9c58f6b --- /dev/null +++ b/src/enemies/thwomp.c @@ -0,0 +1,265 @@ +#include "thwomp.h" +#include "../enemy.h" +#include "../hero.h" +#include "../game.h" +#include "../PHL.h" +#include "../effect.h" +#include + +void thwompStep(Thwomp* t); +void thwompDraw(Thwomp* t); + +void createThwomp(int x, int y, int type, int offset, int delay, int dir) +{ + int i; + for (i = 0; i < MAX_ENEMIES; i++) { + if (enemies[i] == NULL) { + Enemy* e = malloc(sizeof *e); + Thwomp* t = malloc(sizeof *t); + + t->id = i; + + t->x = x; + t->y = y; + + t->vsp = 0; + t->grav = 0.3; + + t->imageIndex = 0; + + t->type = type; + t->state = 0; + t->timer = offset * 30; + t->delay = delay * 30; + //default delay is 60 + if (delay == 0) { + t->delay = 60; + } + + t->dir = dir; + + t->hp = 3; + t->blink = 0; + + e->data = t; + e->enemyStep = thwompStep; + e->enemyDraw = thwompDraw; + e->type = 16; + + enemies[i] = e; + i = MAX_ENEMIES; + } + } +} + +void thwompStep(Thwomp* t) +{ + //Animate + { + t->imageIndex += 0.1; + if (t->imageIndex >= 3) { + t->imageIndex -= 3; + } + } + + //Counters + { + if (t->blink > 0) { + t->blink -= 1; + } + } + + //Setup Mask + Mask mask; + { + mask.unused = mask.circle = 0; + mask.w = mask.h = 36; + mask.x = t->x + ((40 - mask.w) / 2); + mask.y = t->y + ((40 - mask.h) / 2); + } + + //Wait + if (t->state == 0) { + t->vsp = 0; + + //Waiter + if (t->type == 0) { + Mask area; + area.unused = area.circle = 0; + area.x = t->x - 40; + area.y = t->y; + area.w = 120; + area.h = 160; + + if (checkCollisionXY(area, herox, heroy)) { + t->state = 1; + } + } + + //Automatic + else{ + t->timer -= 1; + if (t->timer <= 0) { + t->state = 1; + } + } + } + + //Fall + else if (t->state == 1) { + //Down + if (t->dir == 0) { + t->y += t->vsp; + } + //Left + if (t->dir == 1) { + t->x -= t->vsp; + } + //Right + if (t->dir == 2) { + t->x += t->vsp; + } + + t->vsp += t->grav; + + if (t->vsp >= 7) { + t->vsp = 7; + } + + //Update Mask + mask.x = t->x + ((40 - mask.w) / 2); + mask.y = t->y + ((40 - mask.h) / 2); + + PHL_Rect collide = getTileCollision(1, mask); + if (collide.x != -1) { + int effX = t->x, + effY = t->y; + //Down + if (t->dir == 0) { + t->y = collide.y - 40; + effY = t->y + 20; + } + //Left + if (t->dir == 1) { + t->x = collide.x + 40; + effX = t->x - 20; + } + //Right + if (t->dir == 2) { + t->x = collide.x - 40; + effX = t->x + 20; + } + + t->state = 2; + t->timer = 60; + createEffect(1, effX, effY); + PHL_PlaySound(sounds[sndHit07], CHN_ENEMIES); + } + } + else if (t->state == 2) { + if (t->type == 1) { //Automatic + t->timer -= 1; + if (t->timer <= 0) { + t->state = 3; + } + } + } + else if (t->state == 3) { //rise up + //Down + if (t->dir == 0) { + t->y -= 2; + } + //Left + if (t->dir == 1) { + t->x += 2; + } + //Right + if (t->dir == 2) { + t->x -= 2; + } + + //Update Mask + mask.x = t->x + ((40 - mask.w) / 2); + mask.y = t->y + ((40 - mask.h) / 2); + + if (t->dir == 0) { + mask.y -= 4; + } + if (t->dir == 1) { + mask.x += 4; + } + if (t->dir == 2) { + mask.x -= 4; + } + + PHL_Rect collide = getTileCollision(1, mask); + if (collide.x != -1) { + //Down + if (t->dir == 0) { + t->y = collide.y + 40 + 2; + } + //Left + if (t->dir == 1) { + t->x = collide.x - 40 + 2; + } + //Right + if (t->dir == 2) { + t->x = collide.x + 40 + 2; + } + + t->state = 0; + t->timer = t->delay; + } + } + + //Update Mask + { + mask.x = t->x + ((40 - mask.w) / 2); + mask.y = t->y + ((40 - mask.h) / 2); + } + + //Hit Player + { + if (checkCollision(mask, getHeroMask())) { + heroHit(15, mask.x + (mask.w / 2)); + } + } + + //Weapon collision + { + int i; + for (i = 0; i < MAX_WEAPONS; i++) { + if (weapons[i] != NULL) { + if (weapons[i]->cooldown == 0) { + if (checkCollision(mask, weapons[i]->weaponMask)) { + weaponHit(weapons[i]); + + if (hasItem[16] == 1) { //Has blue paper + t->hp -= 1; + t->blink = 15; + + if (t->hp <= 0) { + createRockSmash(t->x + 20, t->y + 20); + spawnCollectable(t->x + 20, t->y); + enemyDestroy(t->id); + } + }else{ + //Tink + PHL_PlaySound(sounds[sndHit03], CHN_WEAPONS); + } + + i = MAX_WEAPONS; + } + } + } + } + } + +} + +void thwompDraw(Thwomp* t) +{ + if (t->blink % 2 == 0) { + PHL_DrawSurfacePart(t->x, t->y, 240 + ((int)t->imageIndex * 40), 400, 40, 40, images[imgMisc20]); + } +} \ No newline at end of file diff --git a/src/enemies/thwomp.h b/src/enemies/thwomp.h new file mode 100644 index 0000000..87edebc --- /dev/null +++ b/src/enemies/thwomp.h @@ -0,0 +1,18 @@ +#ifndef THWOMP_H +#define THWOMP_H + +//#include "../collision.h" + +typedef struct { + int id; + double x, y; + double vsp, grav; + double imageIndex; + int type, state, timer, dir; + int hp, blink; + int delay; +} Thwomp; + +void createThwomp(int x, int y, int type, int offset, int delay, int dir); + +#endif \ No newline at end of file diff --git a/src/enemies/waterjumper.c b/src/enemies/waterjumper.c new file mode 100644 index 0000000..d01d206 --- /dev/null +++ b/src/enemies/waterjumper.c @@ -0,0 +1,356 @@ +#include "waterjumper.h" +#include +#include +#include "../game.h" +#include "../enemy.h" +#include "../hero.h" +#include "../collision.h" + +void waterJumperStep(WaterJumper* w); +void waterJumperDraw(WaterJumper* w); + +void createWaterJumper(int x, int y, int type, int offset, int height) +{ + int i; + for (i = 0; i < MAX_ENEMIES; i++) { + if (enemies[i] == NULL) { + Enemy* e = malloc(sizeof *e); + WaterJumper* w = malloc(sizeof *w); + w->id = i; + w->type = type; + + w->x = x; + w->y = w->ystart = y; + + w->hp = 1; + + w->hsp = w->vsp = 0; + w->grav = 0.115; + w->yoffset = 0; + + w->imageIndex = 0; + w->blink = 0; + + w->timer = 60; + w->timer += 60 * offset; + + w->rot = 0; + w->state = 0; + + if (type == 1) { + w->hp = 2; + w->timer = 60 * offset; + } + + //Specific offset timer + if (offset > 10) { + w->timer = offset; + } + + w->height = height; + /* + w->mask.unused = w->mask.circle = 0; + w->mask.x = x + 8; + w->mask.y = y + 8; + w->mask.w = 24; + w->mask.h = 24; + */ + e->data = w; + e->enemyStep = waterJumperStep; + e->enemyDraw = waterJumperDraw; + e->type = 14; + + enemies[i] = e; + i = MAX_ENEMIES; + } + } +} + +void waterJumperStep(WaterJumper* w) +{ + //Counters + { + if (w->blink > 0) { + w->blink -= 1; + } + + if (w->timer > 0) { + w->timer -= 1; + } + } + + //Setup Mask + Mask mask; + { + mask.unused = mask.circle = 0; + mask.w = 24; + mask.h = 24; + mask.x = w->x + ((40 - mask.w) / 2); + mask.y = w->y + ((40 - mask.h) / 2); + } + + //Float + if (w->state == 0) + { + //Animate + { + w->imageIndex += 0.1; + if (w->imageIndex >= 2) { + w->imageIndex -= 2; + } + } + + //Movement + { + w->rot += 5; + if (w->rot >= 360) { + w->rot -= 360; + } + + w->y = w->ystart + (5 * sin(w->rot * 3.14159 / 180)); + } + + //Hop out of water + { + if (w->timer <= 0) { + w->state = 1; + w->timer = -1; + + createSplash(w->x + 20, w->y); + w->y = w->ystart; + } + } + } + //In air + else if (w->state == 1) + { + //Animate + { + w->imageIndex += 0.25; + if (w->imageIndex >= 3) { + w->imageIndex -= 3; + } + } + + //Green type + if (w->type == 0) + { + //State Start + { + if (w->timer == -1) { + w->timer = 0; + + w->vsp = -5.5; + w->hsp = -2; + if (w->x + 20 < herox) { + w->hsp *= -1; + } + } + } + + //Horizontal Movement + w->x += w->hsp; + + //Land in water + { + if (w->vsp > 0 && w->y >= w->ystart) { + createSplash(w->x + 20, w->y); + w->y = w->ystart; + w->state = 0; + w->hsp = w->vsp = 0; + w->timer = 120; + } + } + } + + //Blue type + else + { + //State Start + { + if (w->timer == -1) { + w->timer = 0; + + if (w->height == 2) { + w->vsp = -5.5; + } + else if (w->height == 3) { + w->vsp = -6; + } + else if (w->height == 4) { + w->vsp = -7; + } + else if (w->height == 5) { + w->vsp = -7.5; + } + } + } + + //Land on expected ground + { + if (w->vsp > 0) { + if (w->y >= w->ystart - 22 - (w->height * 40)) { + w->y = w->ystart - 22 - (w->height * 40); + w->imageIndex = 5; + w->state = 2; + w->timer = 240; + w->hsp = 2; + if (herox < w->x + 20) { + w->hsp *= -1; + } + + } + } + } + + } + + //Vertical Movement + w->y += w->vsp; + w->vsp += w->grav; + + } + + //Walk + else if (w->state == 2) { + //Animate + { + w->imageIndex += 0.16; + if (w->imageIndex >= 7) { + w->imageIndex -= 2; + } + } + + //Movement + { + w->x += w->hsp; + mask.x = w->x + ((40 - mask.w) / 2); + } + + //Hit wall + if (checkTileCollision(1, mask) == 1) { + w->hsp *= -1; + } + + //Turn on edge + else{ + mask.x += (mask.w / 2) * w->hsp; + mask.y += mask.h; + PHL_Rect collide = getTileCollision(1, mask); + if (collide.x == -1) { + collide = getTileCollision(3, mask); + } + if (collide.x == -1) { + w->hsp *= -1; + } + } + + //End walk + { + if (w->timer <= 0) { + w->state = 3; + w->timer = -1; + } + } + } + + //Jump Down + else if (w->state == 3) + { + //Setup + { + if (w->timer == -1) { + w->timer = 0; + PHL_PlaySound(sounds[sndPi02], CHN_ENEMIES); + w->vsp = -4; + w->imageIndex = 2; + } + } + + //Animate + { + w->imageIndex += 0.25; + if (w->imageIndex >= 5) { + w->imageIndex -= 3; + } + } + + //Movement + { + w->y += w->vsp; + w->vsp += w->grav; + + if (w->vsp > 6) { + w->vsp = 6; + } + } + + //Land in Water + { + if (w->y >= w->ystart) { + w->state = 0; + createSplash(w->x + 20, w->y); + w->timer = 60; + } + } + } + + //Update Mask + { + mask.x = w->x + ((40 - mask.w) / 2); + mask.y = w->y + ((40 - mask.h) / 2); + } + + //Collide with hero + { + if (checkCollision(mask, getHeroMask())) { + heroHit(15, mask.x + (mask.w / 2)); + } + } + + //Weapon collision + { + int i; + for (i = 0; i < MAX_WEAPONS; i++) { + if (weapons[i] != NULL) { + if (weapons[i]->cooldown == 0) { + if (checkCollision(mask, weapons[i]->weaponMask)) { + weaponHit(weapons[i]); + + w->blink = 15; + w->hp -= 1; + if (w->hp <= 0) { + createEffect(2, w->x - 12, w->y - 12); + spawnCollectable(w->x + 20, w->y); + enemyDestroy(w->id); + } + + i = MAX_WEAPONS; + } + } + } + } + } + +} + +void waterJumperDraw(WaterJumper* w) +{ + if (w->blink % 2 == 0) { + int cx = (int)w->imageIndex * 40; + + if (w->state == 1) { + cx += 80; + } + + if (w->type == 1) { + cx += 200; + + if (w->state == 2 && w->hsp < 0) { + cx += 80; + } + } + + PHL_DrawSurfacePart(w->x, w->y, cx, 440, 40, 40, images[imgEnemies]); + } +} \ No newline at end of file diff --git a/src/enemies/waterjumper.h b/src/enemies/waterjumper.h new file mode 100644 index 0000000..77823d8 --- /dev/null +++ b/src/enemies/waterjumper.h @@ -0,0 +1,23 @@ +#ifndef WATERJUMPER_H +#define WATERJUMPER_H + +#include "../enemy.h" + +typedef struct { + int id, type; + + double x, y; + int hp; + int blink; + int ystart, rot; + double yoffset; + double hsp, vsp, grav; + + double imageIndex; + int state, timer; + int height; +} WaterJumper; + +void createWaterJumper(int x, int y, int type, int offset, int height); + +#endif \ No newline at end of file diff --git a/src/enemies/wizard.c b/src/enemies/wizard.c new file mode 100644 index 0000000..b496a45 --- /dev/null +++ b/src/enemies/wizard.c @@ -0,0 +1,133 @@ +#include "wizard.h" +#include "../enemy.h" +#include "../game.h" +#include "../hero.h" +#include + +void createWizard(int x, int y) +{ + int i; + for (i = 0; i < MAX_ENEMIES; i++) { + if (enemies[i] == NULL) { + Enemy* e = malloc(sizeof *e); + Wizard* w = malloc(sizeof *w); + w->id = i; + + w->x = x; + w->y = y; + + w->imageIndex = 0; + + w->state = 0; + w->timer = 50; + w->visible = 1; + + w->mask.circle = w->mask.unused = 0; + w->mask.w = 24; + w->mask.h = 38; + w->mask.x = w->x + 8; + w->mask.y = w->y + 2; + + e->data = w; + e->enemyStep = wizardStep; + e->enemyDraw = wizardDraw; + e->type = 21; + + enemies[i] = e; + i = MAX_ENEMIES; + } + } +} + +void wizardStep(Wizard* w) +{ + w->imageIndex += 0.3; + if (w->imageIndex >= 3) { + w->imageIndex -= 3; + } + + //Stand still + if (w->state == 0) { + w->timer -= 1; + + if (w->timer <= 0) { + PHL_PlaySound(sounds[sndPi10], CHN_ENEMIES); + w->state = 1; + w->timer = 15; + } + } + //Flash + else if (w->state == 1 || w->state == 3) { + if (w->visible == 0) { + w->visible = 1; + }else{ + w->visible = 0; + } + + w->timer -= 1; + if (w->timer <= 0) { + if (w->state == 1) { + w->state = 2; + w->timer = 60; + } + else if (w->state == 3) { + w->visible = 1; + w->state = 0; + w->timer = 50; + } + } + } + //Invisible + else if (w->state == 2) { + w->visible = 0; + + w->timer -= 1; + if (w->timer <= 0) { + PHL_PlaySound(sounds[sndPi03], CHN_ENEMIES); + w->state = 3; + w->timer = 15; + + //Horizontal Jump + int gridX = w->x / 40, + gridY = w->y / 40, + lastGridX = gridX; + + do { + gridX = (rand() % 16) + 1; + } while (collisionTiles[gridX][gridY] != 0 || + collisionTiles[gridX][gridY+1] != 1 || + gridX == lastGridX); + + w->x = gridX * 40; + w->mask.x = w->x + 8; + } + } + + if (w->state == 0 || w->state == 3) { + //Hit Player + if (checkCollision(w->mask, getHeroMask())) { + heroHit(15, w->x + 20); + } + + //Weapon Collision + int i; + for (i = 0; i < MAX_WEAPONS; i++) { + if (weapons[i] != NULL) { + if (checkCollision(w->mask, weapons[i]->weaponMask)) { + weaponHit(weapons[i]); + createEffect(2, w->x - 12, w->y - 6); + spawnCollectable(w->x + 20, w->y); + enemyDestroy(w->id); + i = MAX_WEAPONS; + } + } + } + } +} + +void wizardDraw(Wizard* w) +{ + if (w->visible == 1) { + PHL_DrawSurfacePart(w->x, w->y, 520 + (((int)w->imageIndex) * 40), 480, 40, 40, images[imgEnemies]); + } +} \ No newline at end of file diff --git a/src/enemies/wizard.h b/src/enemies/wizard.h new file mode 100644 index 0000000..00fed6d --- /dev/null +++ b/src/enemies/wizard.h @@ -0,0 +1,20 @@ +#ifndef WIZARD_H +#define WIZARD_H + +#include "../collision.h" + +typedef struct { + int id; + double x, y; + double imageIndex; + int state, timer, visible; + + Mask mask; +} Wizard; + +void createWizard(int x, int y); + +void wizardStep(Wizard* w); +void wizardDraw(Wizard* w); + +#endif \ No newline at end of file diff --git a/src/enemy.c b/src/enemy.c new file mode 100644 index 0000000..3fc49a0 --- /dev/null +++ b/src/enemy.c @@ -0,0 +1,16 @@ +#include "enemy.h" +#include "game.h" +#include + +void enemyDestroy(int id) +{ + if (enemies[id] != NULL) { + if (enemies[id]->data != NULL) { + free(enemies[id]->data); + } + enemies[id]->data = NULL; + + free(enemies[id]); + } + enemies[id] = NULL; +} diff --git a/src/enemy.h b/src/enemy.h new file mode 100644 index 0000000..14ced41 --- /dev/null +++ b/src/enemy.h @@ -0,0 +1,13 @@ +#ifndef ENEMY_H +#define ENEMY_H + +typedef struct { + void* data; //Specific enemy struct + void (*enemyStep)(); + void (*enemyDraw)(); + int type; +} Enemy; + +void enemyDestroy(int id); + +#endif \ No newline at end of file diff --git a/src/game.c b/src/game.c new file mode 100644 index 0000000..4234f50 --- /dev/null +++ b/src/game.c @@ -0,0 +1,1678 @@ +#include "game.h" +#include "hero.h" +#include "PHL.h" +#include "qda.h" +#include "ini.h" +#include "titlescreen.h" +#include "options.h" +#include "inventory.h" +#include "object.h" +#include "effect.h" +#include "text.h" +#include "stagedata.h" +#include +#include +#include + +int gameStep(); +void gameDraw(char doDrawHud); + +void freeArrays(); +void drawHud(); +int getTileType(int valx, int valy); + +void loadUncommonImages(); + +char forceGameExit = 0; + +int drawhp; +int NumOfSounds = 43; +int NumOfImages = 14; + +char autoSave = 1; +int levelStartFlag = 0; + +char tilesetStrings[9][12] = {"stage01.bmp", + "stage02.bmp", + "stage02.bmp", + "stage03.bmp", + "stage04.bmp", + "stage03.bmp", + "stage02.bmp", + "stage05.bmp", + "stage08.bmp" }; + +char musicStrings[9][14] = { "midi/main01", + "midi/main02", + "midi/main02", + "midi/main05", + "midi/main03", + "midi/main05", + "midi/main02", + "midi/main04", + "midi/main06" }; + + + +double cutInTimer = 240; +int transitionTimer = 0; +int level = 0; +int screenX = 5, + screenY = 2; + +#ifdef _SDL +char savename[4096]; +#else +#define savename "data/save.tmp" +#endif + +void game() +{ +#ifdef _SDL + const char* home = getenv("HOME"); + if(home) + { + strcpy(savename, home); + strcat(savename, "/.hydracastlelabyrinth/"); + strcat(savename, "save.tmp"); + } else { + strcpy(savename, "data/save.tmp"); + } +#endif + //Setup services + PHL_Init(); + initQDA(); + textInit(); + iniInit(); + + //Load Resources + loadText(); + loadResources(); + + while (PHL_MainLoop()) + { + //Titlescreen + int titleScreenResult = titleScreen(); + + //Exit game + if (titleScreenResult == 2) { + PHL_GameQuit(); + } + + //Game Start + else{ + //Reset game state + gameSetup(); + + //Load Game + if (titleScreenResult == 1) + { + if (fileExists(savename) == 1) { + loadSave(savename); + }else if (fileExists("map/018.map") == 1) { + loadSave("map/018.map"); + } + } + + //Update resources, depending on level + loadUncommonImages(); + + /*printf("\nTiles are "); + if (images[imgTiles].pxdata == NULL) { + printf("not loaded."); + }else{ + printf("loaded."); + }*/ + PHL_FreeSurface(images[imgTiles]); + images[imgTiles] = PHL_LoadQDA(tilesetStrings[level]); + + PHL_FreeMusic(bgmMusic); + bgmMusic = PHL_LoadMusic(musicStrings[level], 1); + + loadScreen(); + + //In game main loop + char gameLoop = 1; + + while (PHL_MainLoop() == 1 && gameLoop == 1) { + PHL_ScanInput(); + + int gameResult = gameStep(); + + if (gameResult != -1) { + gameLoop = 0; + } + + if (gameLoop == 1) { + PHL_StartDrawing(); + gameDraw(1); + PHL_EndDrawing(); + } + } + + //Game end (return to titlescreen) + roomDarkness = 0; + freeArrays(); + + //Erase temp save if it exists + if (fileExists(savename)) + { + #ifdef _SDL + remove(savename); + #else + char fullPath[128]; + strcpy(fullPath, ""); + #ifdef _3DS + strcat(fullPath, "sdmc:/3ds/appdata/HydraCastleLabyrinth/"); + #endif + strcat(fullPath, savename); + remove(fullPath); + #endif + } + } + } + + //Free Resources + textFree(); + freeResources(); + + //Deinit services + PHL_Deinit(); +} + +void loadResources() +{ + //Loading Images + images[imgTiles] = PHL_LoadQDA(tilesetStrings[level]); + images[imgEnemies] = PHL_LoadQDA("ene01.bmp"); + images[imgHud] = PHL_LoadQDA("status.bmp"); + images[imgMisc20] = PHL_LoadQDA("chr20.BMP"); + images[imgMisc32] = PHL_LoadQDA("chr32.BMP"); + images[imgHero] = PHL_LoadQDA("mychr.bmp"); + images[imgItems] = PHL_LoadQDA("items.bmp"); + images[imgExplosion] = PHL_LoadQDA("chr64.BMP"); + images[imgBoss] = PHL_LoadQDA("boss01.bmp"); + //images[imgMisc2040] = PHL_LoadQDA("chr20x40.BMP"); + images[imgFontKana] = PHL_LoadQDA("font8x8-kana.bmp"); + images[imgBoldFont] = PHL_LoadQDA("font8x8-01.bmp"); + //images[imgDark] = PHL_LoadQDA("dark.bmp"); + //images[imgMisc6020] = PHL_LoadQDA("chr60x20.bmp"); + //images[imgHud].colorKey = PHL_NewRGB(0, 0, 0); + //PHL_SetColorKey(images[imgHud], 0, 0, 0); + images[imgTitle01] = PHL_LoadQDA("title01.BMP"); + + //Load Sounds + sounds[sndBee01] = PHL_LoadSound("wav/bee01.wav"); + sounds[sndBell01] = PHL_LoadSound("wav/bell01.wav"); + sounds[sndBom01] = PHL_LoadSound("wav/bom01.wav"); + sounds[sndBom02] = PHL_LoadSound("wav/bom02.wav"); + sounds[sndBom03] = PHL_LoadSound("wav/bom03.wav"); + sounds[sndDoor00] = PHL_LoadSound("wav/door00.wav"); + sounds[sndFire01] = PHL_LoadSound("wav/fire01.wav"); + sounds[sndGas01] = PHL_LoadSound("wav/gas01.wav"); + sounds[sndGet01] = PHL_LoadSound("wav/get01.wav"); + sounds[sndGet02] = PHL_LoadSound("wav/get02.wav"); + sounds[sndHit01] = PHL_LoadSound("wav/hit01.wav"); + sounds[sndHit02] = PHL_LoadSound("wav/hit02.wav"); + sounds[sndHit03] = PHL_LoadSound("wav/hit03.wav"); + sounds[sndHit04] = PHL_LoadSound("wav/hit04.wav"); + sounds[sndHit05] = PHL_LoadSound("wav/hit05.wav"); + sounds[sndHit06] = PHL_LoadSound("wav/hit06.wav"); + sounds[sndHit07] = PHL_LoadSound("wav/hit07.wav"); + sounds[sndJump01] = PHL_LoadSound("wav/jump01.wav"); + sounds[sndJump02] = PHL_LoadSound("wav/jump02.wav"); + sounds[sndNg] = PHL_LoadSound("wav/ng.wav"); + sounds[sndOk] = PHL_LoadSound("wav/ok.wav"); + sounds[sndPi01] = PHL_LoadSound("wav/pi01.wav"); + sounds[sndPi02] = PHL_LoadSound("wav/pi02.wav"); + sounds[sndPi03] = PHL_LoadSound("wav/pi03.wav"); + sounds[sndPi04] = PHL_LoadSound("wav/pi04.wav"); + sounds[sndPi05] = PHL_LoadSound("wav/pi05.wav"); + sounds[sndPi06] = PHL_LoadSound("wav/pi06.wav"); + sounds[sndPi07] = PHL_LoadSound("wav/pi07.wav"); + sounds[sndPi08] = PHL_LoadSound("wav/pi08.wav"); + sounds[sndPi09] = PHL_LoadSound("wav/pi09.wav"); + sounds[sndPi10] = PHL_LoadSound("wav/pi10.wav"); + sounds[sndPower01] = PHL_LoadSound("wav/power01.wav"); + sounds[sndPower02] = PHL_LoadSound("wav/power02.wav"); + sounds[sndShot01] = PHL_LoadSound("wav/shot01.wav"); + sounds[sndShot02] = PHL_LoadSound("wav/shot02.wav"); + sounds[sndShot03] = PHL_LoadSound("wav/shot03.wav"); + sounds[sndShot04] = PHL_LoadSound("wav/shot04.wav"); + sounds[sndShot05] = PHL_LoadSound("wav/shot05.wav"); + sounds[sndShot06] = PHL_LoadSound("wav/shot06.wav"); + sounds[sndShot07] = PHL_LoadSound("wav/shot07.wav"); + sounds[sndStep01] = PHL_LoadSound("wav/step01.wav"); + sounds[sndWater01] = PHL_LoadSound("wav/water01.wav"); + sounds[sndWolf01] = PHL_LoadSound("wav/wolf01.wav"); + + //Load Music + bgmSecret = PHL_LoadMusic("midi/nazo", 0); + bgmGameover = PHL_LoadMusic("midi/gameover", 0); +} + +void freeResources() +{ + //Free sounds + int i; + for (i = 0; i < NumOfSounds; i++) { + PHL_FreeSound(sounds[i]); + } + + //Free Music + PHL_FreeMusic(bgmMusic); + PHL_FreeMusic(bgmGameover); + PHL_FreeMusic(bgmSecret); + + //Free graphics + for (i = 0; i < NumOfImages; i++) { + PHL_FreeSurface(images[i]); + } +} + +void gameSetup() +{ + //Reset Flags + { + quakeTimer = 0; + secretTimer = 0; + roomDarkness = 0; + + bellFlag = 0; + bossFlag = 0; + bossDefeatedFlag = 0; + + int i; + for (i = 0; i < 60; i++) { + flags[i] = 0; + } + } + + //Save Data + { + playTime = 0; + + //Inventory + int i; + for (i = 0; i < 5; i++) { + hasWeapon[i] = 0; + } + + for (i = 0; i < 28; i++) { + hasItem[i] = 0; + } + + for (i = 0; i< 8; i++) { + hasKey[i] = 0; + } + } + + //Room Data + { + roomSecret = 0; + level = 0; + screenX = 5; + screenY = 2; + } + + //Hero Setup + { + heroSetup(); + drawhp = herohp; + } + + //Reset object arrays + freeArrays(); + + //Setup screen transition + cutInTimer = 240; +} + +int gameStep() +{ + //Manage Timers + { + playTime += 1; + + secretCountdown(); + + if (quakeTimer > 0) { + quakeTimer -= 1; + } + + if (cutInTimer > 0) { + cutInTimer -= 5; + + //Play music when the transition ends + if (cutInTimer <= 0 && bossDefeatedFlag == 0 && bossFlag == 0) { + PHL_PlayMusic(bgmMusic); + } + } + } + + //Hero step + { + //End game if hero died + if (heroStep() == 1) { + return 0; + } + } + + //Menu button presses + { + if (getHeroState() <= 5 && cutInTimer <= 0) { + if (btnSelect.pressed == 1) + { + int optionsResult = options(); + + //Reset Game + if (optionsResult == 1) { + return 0; + } + //Exit Game + if (optionsResult == 3) { + PHL_GameQuit(); + return 1; + } + }else if (btnStart.pressed == 1) { + inventory(); + } + } + } + + //Objects steps + { + int i; + for (i = 0; i < MAX_PLATFORMS; i++) { + if (platforms[i] != NULL) { + platformStep(platforms[i]); + } + } + + for (i = 0; i < MAX_OBJECTS; i++) { + if (objects[i] != NULL) { + objects[i]->objectStep(objects[i]->data); + } + } + + for (i = 0; i < MAX_WEAPONS; i++) { + if (weapons[i] != NULL) { + weaponStep(weapons[i]); + } + } + + for (i = 0; i < MAX_EFFECTS; i++) { + if (effects[i] != NULL) { + effectStep(effects[i]); + } + } + + for (i = 0; i < MAX_ENEMIES; i++) { + if (enemies[i] != NULL) { + enemies[i]->enemyStep(enemies[i]->data); + } + } + } + + if (forceGameExit == 1) { + forceGameExit = 0; + return 0; + } + + return -1; +} + +void gameDraw(char doDrawHud) +{ + PHL_DrawBackground(background, foreground); + + int i; + //Draw water/lava top effects + for (i = 0; i < MAX_EFFECTS; i++) { + if (effects[i] != NULL) { + if (effects[i]->depth == -1) { + effectDraw(effects[i]); + } + } + } + + for (i = 0; i < MAX_PLATFORMS; i++) { + if (platforms[i] != NULL) { + platformDraw(platforms[i]); + } + } + + for (i = 0; i < MAX_OBJECTS; i++) { + if (objects[i] != NULL) { + objects[i]->objectDraw(objects[i]->data); + } + } + + for (i = 0; i < MAX_WEAPONS; i++) { + if (weapons[i] != NULL) { + weaponDraw(weapons[i]); + } + } + + //Draw effects under + for (i = 0; i < MAX_EFFECTS; i++) { + if (effects[i] != NULL) { + if (effects[i]->depth == 0) { + effectDraw(effects[i]); + } + } + } + + //Draw enemies backwards, so bullets and such are underneath their spawners + //for (i = MAX_ENEMIES - 1; i >= 0; i--) { + for (i = 0; i < MAX_ENEMIES; i++) { + if (enemies[i] != NULL) { + enemies[i]->enemyDraw(enemies[i]->data); + } + } + + //Not Death, draw death later + if (getHeroState() != 8) { + heroDraw(); + } + + //Draw effects over + for (i = 0; i < MAX_EFFECTS; i++) { + if (effects[i] != NULL) { + if (effects[i]->depth == 1) { + effectDraw(effects[i]); + } + } + } + + //Draw Darkness + if (roomDarkness == 1) { + int cornerX = herox - 160, + cornerY = heroy + 20 - 160; + + PHL_DrawSurfacePart(cornerX, cornerY, 320 * hasItem[18], 0, 320, 320, images[imgDark]); + + //Top darkness rectangle + if (cornerY > 0) { + PHL_DrawRect(0, 0, 640, cornerY, PHL_NewRGB(10, 0, 0)); + } + //Bottom darkness rectangle + if (cornerY + 320 < 480) { + PHL_DrawRect(0, cornerY + 320, 640, 480, PHL_NewRGB(10, 0, 0)); + } + + //Left rectangle + if (cornerX > 0) { + PHL_DrawRect(0, cornerY, cornerX, 320, PHL_NewRGB(10, 0, 0)); + } + //Right rectangle + if (cornerX + 320 < 640) { + PHL_DrawRect(cornerX + 320, cornerY, 640 - cornerX + 320, 320, PHL_NewRGB(10, 0, 0)); + } + } + + //Draw death over darkness + if (getHeroState() == 8) { + heroDraw(); + } + + if (doDrawHud == 1) { + drawHud(); + } + + //cut-in transition + { + if (cutInTimer > 0) { + PHL_DrawRect(0, 0, 640, cutInTimer, PHL_NewRGB(0, 0, 0)); + PHL_DrawRect(0, 240 + (240 - cutInTimer), 640, 480, PHL_NewRGB(0, 0, 0)); + } + } + +} + +void getItem(int itemNum) +{ + setHeroState(6); + setHeroImageIndex(0); + + char getItemTimer = 0; + char loop = 1; + + while (PHL_MainLoop() && loop == 1) + { + secretCountdown(); + //Get Item Step + if (getItemTimer == 0) { + setHeroImageIndex(getHeroImageIndex() + 0.3); + if (getHeroImageIndex() > 3) { + getItemTimer = 1; + } + }else if (getItemTimer == 1) { + //Wait for input + PHL_ScanInput(); + + if (btnAccept.pressed == 1 || btnFaceDown.pressed == 1 || btnFaceRight.pressed == 1 || + btnFaceUp.pressed == 1 || btnFaceLeft.pressed == 1 || btnStart.pressed == 1) { + getItemTimer = 2; + } + }else if (getItemTimer == 2) { + setHeroImageIndex(getHeroImageIndex() + 0.3); + if (getHeroImageIndex() >= 7) { + loop = 0; + + setHeroState(0); + setHeroImageIndex(0); + } + } + + //Get Item Draw + { + PHL_StartDrawing(); + + gameDraw(1); + + if (getHeroImageIndex() >= 3) { + char tempDarkness = roomDarkness; + roomDarkness = 0; + + PHL_DrawRect(140, 208, 360, 64, PHL_NewRGB(255, 255, 255)); + PHL_DrawRect(142, 210, 356, 60, PHL_NewRGB(0, 20, 0)); + + PHL_DrawRect(148, 216, 48, 48, PHL_NewRGB(255, 255, 255)); + PHL_DrawRect(152, 220, 40, 40, PHL_NewRGB(119, 166, 219)); + //Image + PHL_DrawSurfacePart(152, 220, itemGotX, itemGotY, 40, 40, images[imgItems]); + //Text + { + int drawX = 196, drawY = 216; + int twoLayers = 0; + if (itemName[itemNum]->length + found->length + 2 > 17) { + twoLayers = 1; + drawY -= 8; + } + drawX = drawCharacter(17, 2, drawX, drawY); + drawX = drawText(itemName[itemNum], drawX, drawY); + drawX = drawCharacter(18, 2, drawX, drawY); + if (twoLayers == 1) { + drawX = 204; + drawY += 24; + } + drawText(found, drawX, drawY); + } + + roomDarkness = tempDarkness; + } + + PHL_EndDrawing(); + } + } +} + +void saveScreen() +{ + PHL_PlaySound(sounds[sndPower02], CHN_SOUND); + herohp = maxhp; + setHeroHsp(0); + + int saveTimer = 60; + char loop = 1; + + while (PHL_MainLoop() && loop == 1) + { + PHL_StartDrawing(); + + gameDraw(1); + + PHL_DrawRect(140, 208, 360, 64, PHL_NewRGB(255, 255, 255)); + PHL_DrawRect(142, 210, 356, 60, PHL_NewRGB(0, 0, 255)); + drawTextCentered(saving, 320, 216); + + saveTimer -= 1; + if (saveTimer <= 0) { + loop = 0; + } + + PHL_EndDrawing(); + } + + if (writeSave("map/018.map") == 1) + { + if (fileExists(savename)) + { + char fullPath[128]; + strcpy(fullPath, ""); + #ifdef _3DS + strcat(fullPath, "sdmc:/3ds/appdata/HydraCastleLabyrinth/"); + #endif + strcat(fullPath, savename); + remove(fullPath); + } + } +} + +//Result screen and credits +void gameEnding() +{ + int timer = 0; + char exitLoop = 0; + + //Result screen + { + PHL_StopMusic(); + PHL_FreeMusic(bgmMusic); + bgmMusic = PHL_LoadMusic("midi/allclear", 0); + PHL_PlayMusic(bgmMusic); + + //Calculate completion percentage + char treasureString[10]; + { + int itemCount = 0; + int ALLITEMS = 41; + + int i; + for (i = 0; i < 5; i++) { + itemCount += hasWeapon[i]; + } + + for (i = 0; i < 28; i++) { + itemCount += hasItem[i]; + } + + for (i = 0; i < 8; i++) { + itemCount += hasKey[i]; + } + + sprintf(treasureString, "%d%%", itemCount * 100 / ALLITEMS); + } + + //Calculate time + char timeString[9]; + { + int hours = playTime / 216000; + int minutes = (playTime % 216000) / 3600; + int seconds = ((playTime % 216000) % 3600) / 60; + + sprintf(timeString, "%02d:%02d:%02d", hours, minutes, seconds); + } + + int transTimer = 0; + + while (PHL_MainLoop() && exitLoop == 0) + { + timer += 1; + if (timer >= 500) { + transTimer += 8; + } + + if (transTimer >= 360) { + exitLoop = 1; + } + + //Animate Effects + int i; + for (i = 0; i < MAX_EFFECTS; i++) { + if (effects[i] != NULL) { + effectStep(effects[i]); + } + } + + PHL_StartDrawing(); + + gameDraw(0); + + PHL_DrawTextBoldCentered("--- ALL CLEAR! ---", 320, 64, YELLOW); + + PHL_DrawTextBoldCentered("TIME", 320, 128, YELLOW); + PHL_DrawTextBoldCentered(timeString, 320, 144, WHITE); + + PHL_DrawTextBoldCentered("TREASURE", 320, 192, YELLOW); + PHL_DrawTextBoldCentered(treasureString, 320, 208, WHITE); + + //transition + if (transTimer > 0) { + PHL_DrawRect(0, 0, 640, transTimer, PHL_NewRGB(0, 0, 0)); + PHL_DrawRect(0, 240 + (240 - transTimer), 640, 480, PHL_NewRGB(0, 0, 0)); + } + + PHL_EndDrawing(); + } + } + + //Credits + { + timer = 0; + exitLoop = 0; + + PHL_StopMusic(); + PHL_FreeMusic(bgmMusic); + bgmMusic = PHL_LoadMusic("midi/ending", 0); + PHL_PlayMusic(bgmMusic); + + int timer = 0; + double viewY = 0; + int maxViewY = 2200; + double imageIndex = 0; + + while (PHL_MainLoop() && exitLoop == 0) + { + timer += 1; + if (timer >= 2220) { + exitLoop = 1; + } + + viewY += 1; + if (viewY >= maxViewY - 480) { + viewY = maxViewY - 480; + } + + imageIndex += 0.1; + if (imageIndex >= 2) { + imageIndex -= 2; + } + + PHL_StartDrawing(); + + PHL_DrawRect(0, 0, 640, 480, PHL_NewRGB(0, 0, 0)); + + if (exitLoop == 0) { + PHL_DrawTextBoldCentered("- STAFF -", 320, 480 - viewY, YELLOW); + + PHL_DrawTextBoldCentered("SPRITES", 320, 560 - viewY, YELLOW); + PHL_DrawTextBoldCentered("BUSTER", 320, 576 - viewY, WHITE); + + PHL_DrawTextBoldCentered("PROGRAM", 320, 640 - viewY, YELLOW); + PHL_DrawTextBoldCentered("BUSTER", 320, 656 - viewY, WHITE); + + PHL_DrawTextBoldCentered("MUSIC", 320, 720 - viewY, YELLOW); + PHL_DrawTextBoldCentered("MATAJUUROU", 320, 736 - viewY, WHITE); + + PHL_DrawTextBoldCentered("TEST PLAYER", 320, 800 - viewY, YELLOW); + PHL_DrawTextBoldCentered("ZAC", 320, 816 - viewY, WHITE); + + PHL_DrawTextBoldCentered("- SPECIAL THANKS -", 320, 912 - viewY, YELLOW); + + PHL_DrawTextBoldCentered("QUADRUPLE D", 320, 992 - viewY, YELLOW); + PHL_DrawTextBoldCentered("SANDMAN", 320, 1008 - viewY, WHITE); + + PHL_DrawTextBoldCentered("KBGM", 320, 1072 - viewY, YELLOW); + PHL_DrawTextBoldCentered("KR.SHIN", 320, 1088 - viewY, WHITE); + + PHL_DrawTextBoldCentered("KBGMPLAYER", 320, 1152 - viewY, YELLOW); + PHL_DrawTextBoldCentered("NARUTO", 320, 1168 - viewY, WHITE); + + PHL_DrawTextBoldCentered("SOUND EFFECT", 320, 1232 - viewY, YELLOW); + PHL_DrawTextBoldCentered("OSABISHIYUUKI", 320, 1248 - viewY, WHITE); + + PHL_DrawTextBoldCentered("EDGE", 320, 1312 - viewY, YELLOW); + PHL_DrawTextBoldCentered("TAKABO", 320, 1328 - viewY, WHITE); + + PHL_DrawTextBoldCentered("THE END", 320, maxViewY - 284 - viewY, YELLOW); + PHL_DrawSurfacePart(300, maxViewY - 256 - viewY, (int)imageIndex * 40, 280, 40, 80, images[imgHero]); + } + + PHL_EndDrawing(); + } + + } + + forceGameExit = 1; + +} + +//Black screen between screens +void screenTransition() +{ + char timer = 15; + + while (PHL_MainLoop() && timer > 0) + { + PHL_StartDrawing(); + + PHL_DrawRect(0, 0, 640, 480, PHL_NewRGB(0, 0, 0)); + timer -= 1; + + PHL_EndDrawing(); + } + + if (autoSave == 1) { + writeSave(savename); + } +} + +void enterDoor() +{ + //Is not leaving boss room prematurely + bossFlag = 0; + + level = lastDoor->warplevel; + + screenX = lastDoor->warpcoords % 12; + screenY = lastDoor->warpcoords / 12; + + herox = lastDoor->warpx; + heroy = lastDoor->warpy; + + PHL_StopMusic(); + PHL_FreeMusic(bgmMusic); + + if (level == 0) { + //Free uncommon images + PHL_FreeSurface(images[imgMisc2040]); + PHL_FreeSurface(images[imgMisc6020]); + PHL_FreeSurface(images[imgDark]); + }else{ + bgmMusic = PHL_LoadMusic("midi/start", 0); + PHL_PlayMusic(bgmMusic); + + int timer = 125; + while (PHL_MainLoop() && timer > 0) + { + timer -= 1; + + PHL_StartDrawing(); + + PHL_DrawRect(0, 0, 640, 480, PHL_NewRGB(0, 0, 0)); + drawTextCentered(dungeon[level - 1], 320, 216); + + PHL_EndDrawing(); + } + + PHL_StopMusic(); + PHL_FreeMusic(bgmMusic); + + loadUncommonImages(); + } + + //Reload tileset + PHL_FreeSurface(images[imgTiles]); + images[imgTiles] = PHL_LoadQDA(tilesetStrings[level]); + + bgmMusic = PHL_LoadMusic(musicStrings[level], 1); + + changeScreen(0, 0); + + PHL_PlayMusic(bgmMusic); +} + +void loadScreen() +{ + //Stop music if you leave a boss room early + if (bossFlag == 1) { + PHL_StopMusic(); + PHL_FreeMusic(bgmMusic); + + bgmMusic = PHL_LoadMusic(musicStrings[level], 1); + PHL_PlayMusic(bgmMusic); + } + + bossDefeatedFlag = bossFlag = 0; + roomDarkness = 0; + + screenTransition(); + + int fileNum = stage[level][(screenY * 12) + screenX]; + + //Cycle through this process twice. Once for the backgroud, and one for the foreground + int cycle = 0; + for (cycle = 0; cycle < 2; cycle++) + { + //Build file string + char toChar[4]; + sprintf(toChar, "%03d", fileNum); + + char dest[80]; + strcpy(dest, ""); + #ifdef _3DS + strcat(dest, "romfs:/map/"); + #elif defined(_SDL) + strcat(dest, "data/map/"); + #else + strcat(dest, "romfs/map/"); + #endif + strcat(dest, toChar); + + //load background on first pass + if (cycle == 0) { + strcat(dest, "a"); + } + strcat(dest, ".map"); + + //Read file + FILE* file; + if ((file = fopen(dest, "rb"))) + { + char* memblock; + int size; + + fseek(file, 0, SEEK_END); + size = ftell(file); + memblock = (char*)malloc(size); + fseek(file, 0, SEEK_SET); + fread(memblock, 1, size, file); + fclose(file); + + //Load data + int count = 162; //Level data starts 118 + int xx, yy; + int valx = 0, valy = 0; + int raw; + for (yy = 0; yy < 12; yy++) { + for (xx = 0; xx < 16; xx++) { + raw = (unsigned)memblock[count]; + valx = raw & 0x0F; + valy = raw & 0xF0; + valy >>= 4; + + if (cycle == 0) { + background.tileX[xx][yy] = valx; + background.tileY[xx][yy] = valy; + }else if (cycle == 1) { + foreground.tileX[xx][yy] = valx; + foreground.tileY[xx][yy] = valy; + + collisionTiles[xx][yy] = getTileType(valx, valy); + //Breakable blocks + if (valy == 11 && (valx == 0 || valx == 1 || valx == 2)) { + int secret = 0; + if (valx == 2) { + secret = 1; + } + createDestroyable(xx * 40, yy * 40, secret); + } + + //Lava + if (valx == 2 && valy == 1) { + createEffect(10, xx * 40, yy * 40); + foreground.tileX[xx][yy] = 0; + foreground.tileY[xx][yy] = 0; + } + + //Water + if (valx == 6 && valy == 1) { + createEffect(11, xx * 40, yy * 40); + foreground.tileX[xx][yy] = 0; + foreground.tileY[xx][yy] = 0; + } + } + + count += 2; + } + count += 12; + } + + free(memblock); + + }else{ + PHL_ErrorScreen("Map file was not found"); + } + } + PHL_UpdateBackground(background, foreground); + + //Load file + //Build file string + char toChar[4]; + sprintf(toChar, "%03d", fileNum); + + char dest[30]; + #ifdef _3DS + strcpy(dest, "romfs:/obj/"); + #elif defined(_SDL) + strcpy(dest, "data/obj/"); + #else + strcpy(dest, "romfs/obj/"); + #endif + + + //Add a 0 if needed + /* + if (fileNum < 100) { + strcat(dest, "0"); + } + */ + strcat(dest, toChar); + strcat(dest, ".dat"); + + FILE* file; + if ((file = fopen(dest, "rb"))) { + unsigned char* memblock; + int size; + + fseek(file, 0, SEEK_END); + size = ftell(file); + memblock = (unsigned char*)malloc(size); + fseek(file, 0, SEEK_SET); + fread(memblock, 1, size, file); + + int count = 0; + while (count < size) { + int type = memblock[count]; + + if (type <= 10) + { + if (type == 0 || type == 9) { //Blue/Red Slime + createSlime(memblock[count + 1] * 20, memblock[count + 2] * 20, memblock[count + 3], memblock[count + 4]); + } + else if (type == 1) { //Bat (grey/red) + createBat(memblock[count + 1] * 20, memblock[count + 2] * 20, memblock[count + 3]); + } + else if (type == 2) { //Slug + createSlug(memblock[count + 1] * 20, memblock[count + 2] * 20, memblock[count + 3]); + } + else if (type == 3) { //Knight + createKnight(memblock[count + 1] * 20, memblock[count + 2] * 20, memblock[count + 3]); + } + else if (type == 4) { //Rhyno head + createHead(0, memblock[count + 1] * 20, memblock[count + 2] * 20, memblock[count + 3], memblock[count+4], memblock[count+5]); + } + else if (type == 5) { //Dragon head + createHead(2, memblock[count + 1] * 20, memblock[count + 2] * 20, memblock[count + 3], memblock[count+4], memblock[count+5]); + } + else if (type == 6) { //Goblin/medusa head + createHead(1, memblock[count + 1] * 20, memblock[count + 2] * 20, memblock[count + 3], memblock[count+4], memblock[count+5]); + } + else if (type == 7) { //Demon head + createHead(3, memblock[count + 1] * 20, memblock[count + 2] * 20, memblock[count + 3], memblock[count+4], memblock[count+5]); + } + else if (type == 10) { //Fireball head + createHead(4, memblock[count+1] * 20, memblock[count+2] * 20, 1, memblock[count+3], memblock[count+4]); + } + } + + else if (type <= 20) + { + if (type == 11) { //Poison Gas + createGas(memblock[count+1] * 20, memblock[count+2] * 20, memblock[count+3]); + } + else if (type == 12) { //Flying skull + createSkull(memblock[count+1] * 20, memblock[count+2] * 20); + } + else if (type == 13) { //Fish + createFish(memblock[count+1] * 20, memblock[count+2] * 20, memblock[count+3]); + } + else if (type == 14) { //Water Jumper + createWaterJumper(memblock[count+1] * 20, memblock[count+2] * 20, memblock[count+3], memblock[count+4], memblock[count+5]); + } + else if (type == 15) { //Podoboo + createPodoboo(memblock[count+1] * 20, memblock[count+2] * 20, memblock[count+3], memblock[count+4]); + } + else if (type == 16) { //Thwomp + createThwomp(memblock[count+1] * 20, memblock[count+2] * 20, memblock[count+3], memblock[count+4], memblock[count+5], memblock[count+6]); + } + else if (type == 17) { //Skeleton + createSkeleton(memblock[count+1] * 20, memblock[count+2] * 20, memblock[count+3]); + } + else if (type == 18) { //Ghoul + createGhoul(memblock[count+1]*20, memblock[count+2]*20, memblock[count+3]); + } + else if (type == 19) { //Seal + createSeal(memblock[count+1]*20, memblock[count+2]*20); + } + else if (type == 20) { //Jellyfish + createJellyfish(memblock[count+1]*20, memblock[count+2]*20); + } + } + + else if (type <= 30) + { + if (type == 21) { //Wizard + createWizard(memblock[count+1]*20, memblock[count+2]*20); + } + else if (type == 22) { //Pendulum + createPendulum(memblock[count+1]*20, memblock[count+2]*20, memblock[count+3]); + } + else if (type == 24) { //Bee + createBee(memblock[count+1]*20, memblock[count+2]*20, memblock[count+3]); + } + else if (type == 25) { //Air Jar + //createJar(memblock[count+1]*20, memblock[count+2]*20, memblock[count+3], memblock[count+4]); + createHead(5, memblock[count + 1] * 20, memblock[count + 2] * 20, 0, memblock[count+3], memblock[count+4]); + } + else if (type == 26) { //Boar + createBoar(memblock[count+1]*20, memblock[count+2]*20); + } + else if (type == 27) { //Fire Wheel + createFirewheel(memblock[count+1]*20, memblock[count+2]*20, memblock[count+3]); + } + else if (type == 28) { //Rock Golem + createGolem(memblock[count+1]*20, memblock[count+2]*20, memblock[count+3]); + } + else if (type == 29) { //Poison Knight + createPoisonknight(memblock[count+1]*20, memblock[count+2]*20); + } + else if (type == 30) { //Electricity doggy + createDog(memblock[count+1]*20, memblock[count+2]*20); + } + } + + else if (type < 40) + { + if (type == 31) { + createBoomknight(memblock[count+1]*20, memblock[count+2]*20); + } + else if (type == 32) { + createPumpkinenemy(memblock[count+1]*20, memblock[count+2]*20); + } + } + + else if (type < 50) + { + //Bosses + if (type == 40) { + createDodo(memblock[count+1] * 20, memblock[count+2] * 20, memblock[count+3]); + } + else if (type == 41) { + createBatboss(memblock[count+1] * 20, memblock[count+2] * 20); + } + else if (type == 42) { + createCrab(memblock[count+1] * 20, memblock[count+2] * 20); + } + else if (type == 43) { + createGyra(memblock[count+1] * 20, memblock[count+2] * 20); + } + else if (type == 44) { + createLolidra(memblock[count+1] * 20, memblock[count+2] * 20); + } + else if (type == 45) { + createDevil(memblock[count+1] * 20, memblock[count+2] * 20); + } + else if (type == 46) { + createGarm(memblock[count+1] * 20, memblock[count+2] * 20); + } + else if (type == 47) { + createHydra(memblock[count+1] * 20); + } + } + + else if (type <= 60) + { + //Objects + if (type == 50) { //Moving platforms + createPlatform(0, memblock[count + 1] * 20, memblock[count + 2] * 20, memblock[count + 3] * 20, memblock[count + 4] * 20, memblock[count + 5], memblock[count+6]); + } + else if (type == 51) { //Loose block + createPlatform(1, memblock[count + 1] * 20, memblock[count + 2] * 20, 0, 0, 0, memblock[count+3]); + } + else if (type == 52) { //Locked Block + createLockBlock(memblock[count+1] * 20, memblock[count+2] * 20, memblock[count+3]); + } + else if (type == 53) { //Gate + createGate(memblock[count+1]*20, memblock[count+2]*20, memblock[count+3]); + } + else if (type == 54) { //Statue + createStatue(memblock[count+1]*20, memblock[count+2]*20, memblock[count+3]); + } + else if (type == 55) { //Megaman block + createPlatform(2, memblock[count + 1] * 20, memblock[count + 2] * 20, memblock[count + 3], memblock[count + 4], 0, 0); + } + else if (type == 56) { //Electric gate + createShockgate(memblock[count + 1] * 20, memblock[count + 2] * 20, memblock[count + 3]); + } + else if (type == 57) { //Hydra platform + createPlatform(3, memblock[count + 1] * 20, memblock[count + 2] * 20, 0, 0, 0, 0); + } + } + + else if (type < 70) + { + + } + + else/* if (type <= 80)*/ + { + if (type == 70) { //Breakable Block + createDestroyable(memblock[count+1] * 20, memblock[count+2] * 20, 1); + } + else if (type == 71) { //Secret Trigger + createSecretTrigger(memblock[count+1], memblock[count+2], memblock[count+3]); + } + else if (type == 73) { //Chests + createChest(memblock[count + 1] * 20, memblock[count + 2] * 20, memblock[count + 3], memblock[count + 4]); + } + else if (type == 74) { //Save Points + createSavePoint(memblock[count+1] * 20, memblock[count+2] * 20, memblock[count+3]); + } + else if (type == 75) { //door + createDoor(memblock[count+1] * 20, memblock[count+2] * 20, memblock[count+3], memblock[count+4], memblock[count+5] * 20, memblock[count+6] * 20, memblock[count+7]); + } + else if (type == 76) { //Light Switch + createSwitch(memblock[count+1] * 20, memblock[count+2] * 20, memblock[count+3]); + } + else if (type == 77) { //Floor Button + createFloorPad(memblock[count+1]*20, memblock[count+2]*20, memblock[count+3]); + } + else if (type == 78) { + roomDarkness = 1; + } + else if (type == 79) { //Ladder Spawner + createLadder(memblock[count+1] * 20, memblock[count+2] * 20, memblock[count+3]); + } + else if (type == 80) { //Generator + createGenerator(memblock[count+1] * 20, memblock[count+2] * 20, memblock[count+3]); + } + else if (type == 81) { //Crown + createCrown(memblock[count+1] * 20, memblock[count+2] * 20); + } + } + count += 16; + } + + free(memblock); + fclose(file); + } + +} + +void drawHud() +{ + //Repress certain screen altering variables + int tempDark = roomDarkness; + roomDarkness = 0; + + int tempQuake = quakeTimer; + quakeTimer = 0; + + //Change HUD position + int drawy = 8; + { + if (heroy <= 100) { + drawy = 400; + } + } + + //Move scrolling health bar + { + if (drawhp > herohp) { + drawhp -= 1; + } + if (drawhp < herohp) { + drawhp += 1; + } + } + + //Main image + { + PHL_DrawSurfacePart(8, drawy, 0, 0, 368, 64, images[imgHud]); + } + + //Health bar + { + PHL_RGB hpbarc = PHL_NewRGB(128, 255, 0); + if (getHeroPoisoned() > 0) { + hpbarc = PHL_NewRGB(255, 128, 255); + } + + PHL_DrawRect(76, drawy + 8, maxhp * 2, 6, PHL_NewRGB(255, 0, 0)); + PHL_DrawRect(76, drawy + 8, drawhp * 2, 6, hpbarc); + } + + //Ammo counter + { + char c[10]; + sprintf(c, "%02d", heroAmmo); + PHL_DrawTextBold(c, 74, drawy + 36, WHITE); + } + + //Draw weapon icon + { + int wx = 32 * (heroWeapon + 1); + if (hasWeapon[heroWeapon] == 0) { + wx = 0; + } + PHL_DrawSurfacePart(24, drawy + 16, wx, 64, 32, 32, images[imgHud]); + } + + //Restore screen altering variables + { + quakeTimer = tempQuake; + roomDarkness = tempDark; + } +} + +void freeArrays() +{ + int i; + for (i = 0; i < MAX_EFFECTS; i++) { + effectDestroy(i); + } + + for (i = 0; i < MAX_OBJECTS; i++) { + objectDestroy(i); + } + + for (i = 0; i < MAX_ENEMIES; i++) { + enemyDestroy(i); + } + + for (i = 0; i < MAX_WEAPONS; i++) { + weaponDestroy(i); + } + + for (i = 0; i < MAX_PLATFORMS; i++) { + platformDestroy(i); + } +} + +void changeScreen(int dx, int dy) +{ + roomSecret = 0; + + freeArrays(); + + screenX += dx; + screenY += dy; + loadScreen(); + + writeSave(savename); +} + +int getTileType(int valx, int valy) { + int result = 0; + + if (valy == 11 && valx == 8) { + result = 3; //Ladder Top + }else + if (valy == 1 && valx == 1) { + result = 5; //Lava + }else + if (valy > 7) { + result = 1; //Solid + }else + //specifics + if (valy == 0 && (valx == 3 || valx == 5)) { + result = 2; //Ladders + }else + if (valy == 1 && valx == 5) { + result = 4; //Water + }else + if (valx == 0 && (valy == 1 || valy == 2)) { + result = 6; //Spikes + }else + if (valy == 11 && (valx == 0 || valx == 1 || valx == 2)) { + result = 1; //Breakable solid block + } + + if (level == 4 && valy == 3 && (valx == 0 || valx == 1 || valx == 2)) { + result = 6; //Spikes + } + + return result; +} + +//Save file load/save +int writeSave(char* fname) +{ + int result = 0; + //mkdir("data"); + FILE* f; + + char fullPath[128]; + strcpy(fullPath, ""); + #ifdef _3DS + strcat(fullPath, "sdmc:/3ds/appdata/HydraCastleLabyrinth/"); + #endif + strcat(fullPath, fname); + + if ( (f = fopen(fullPath, "wb")) ) { + int size = 4548; + unsigned char* memblock = (unsigned char*)malloc(size); + memset(memblock, 0, size); + + memblock[0x0] = herohp; + memblock[0x4] = maxhp; + memblock[0x8] = heroAmmo; + memblock[0x0C] = maxAmmo; + + if (heroWeapon == -1) { + memblock[0x10] = 0; + }else{ + memblock[0x10] = heroWeapon; + } + memblock[0x14] = 1; //Unknown, but always resets to 1 + + int i; + for (i = 0; i < 60; i++) { + memblock[(0x3FC) + i] = flags[i]; + } + + for (i = 0; i < 5; i++) { + memblock[(0x7E4) + i] = hasWeapon[i]; + } + + int itemorder[28] = { 0x7F6, 0x7FA, 0x7F9, 0x7F8, 0x7F1, 0x7F3, 0x7F2, + 0x7FB, 0x7ED, 0x7EF, 0x7EE, 0x7F0, 0x7EC, 0x7F4, + 0x7F7, 0x7F5, 0x7EA, 0x7EB, 0x7FF, 0x803, 0x804, + 0x7FE, 0x802, 0x805, 0x800, 0x7FD, 0x7FC, 0x801 }; + + for (i = 0; i < 28; i++) { + memblock[itemorder[i]] = hasItem[i]; + } + + for (i = 0; i < 8; i++) { + memblock[(0x806) + i] = hasKey[i]; + } + + int writeHerox = herox; + int writeHeroy = heroy; + memcpy(&memblock[0x11B0], &writeHerox, 4); + memcpy(&memblock[0x11B4], &writeHeroy, 4); + + if (getHeroDirection() == 1) { + memblock[0x11C0] = 0; + }else{ + memblock[0x11C0] = 1; + } + + memblock[0x11B8] = level; + + //Screen + memblock[0x11BC] = (screenX) + (screenY * 12); + + //Time + memcpy(&memblock[0x11AC], &playTime, 4); + + fwrite(memblock, 1, size, f); + + free(memblock); + + result = 1; + fclose(f); + } + + + return result; +} + +void loadSave(char* fname) +{ + FILE* f; + + char fullPath[128]; + strcpy(fullPath, ""); + #ifdef _3DS + strcat(fullPath, "sdmc:/3ds/appdata/HydraCastleLabyrinth/"); + #endif + strcat(fullPath, fname); + + if ((f = fopen(fullPath, "rb"))) { + //Reminder: read order matters + unsigned long loadTemp = 0; + //Hero HP + fread(&loadTemp, 4, 1, f); + herohp = loadTemp; + drawhp = herohp; + + //Max HP + fread(&loadTemp, 4, 1, f); + maxhp = loadTemp; + + //Ammo + fread(&loadTemp, 4, 1, f); + heroAmmo = loadTemp; + + //Max Ammo + fread(&loadTemp, 4, 1, f); + maxAmmo = loadTemp; + + int loadedWeapon = 0; + fread(&loadedWeapon, 1, 1, f); + + //Read Flags + fseek(f, 0x3FC, SEEK_SET); + int i; + for (i = 0; i < 60; i++) { + fread(&flags[i], 1, 1, f); + } + + //Read weapons + fseek(f, 0x7E4, SEEK_SET); + for (i = 0; i < 5; i++) { + fread(&hasWeapon[i], 1, 1, f); + } + + heroWeapon = -1; + if (hasWeapon[loadedWeapon] == 1) { + heroWeapon = loadedWeapon; + } + + //Read items + int itemorder[28] = { 16, 17, 12, 8, 10, 9, 11, + 4, 6, 5, 13, 15, 0, 14, + 3, 2, 1, 7, 26, 25, 21, + 18, 24, 27, 22, 19, 20, 23 }; + fseek(f, 0x7EA, SEEK_SET); + for (i = 0; i < 28; i++) { + fread(&hasItem[itemorder[i]], 1, 1, f); + } + + //Read keys + for (i = 0; i < 8; i++) { + fread(&hasKey[i], 1, 1, f); + } + + fseek(f, 0x11AC, SEEK_SET); + fread(&playTime, 4, 1, f); + + //fseek(f, 4540, SEEK_SET); + //Hero X and Y + fread(&loadTemp, 4, 1, f); + herox = loadTemp; + fread(&loadTemp, 4, 1, f); + heroy = loadTemp; + + //Level + fread(&loadTemp, 4, 1, f); + level = loadTemp; + + //Screen coords + fread(&loadTemp, 4, 1, f); + screenX = (loadTemp) % 12; + screenY = ((int)(loadTemp) / 12); + + //Direction + fread(&loadTemp, 4, 1, f); + if (loadTemp == 0) { + setHeroDirection(1); + }else{ + setHeroDirection(-1); + } + fclose(f); + } + +} + +int fileExists(char* fpath) +{ + int result = 0; + + char fullPath[128]; + strcpy(fullPath, ""); + #ifdef _3DS + strcat(fullPath, "sdmc:/3ds/appdata/HydraCastleLabyrinth/"); + #endif + strcat(fullPath, fpath); + + FILE* f; + if ( (f = fopen(fullPath, "rb")) ) { + result = 1; + fclose(f); + } + + return result; +} + +void playSecret() +{ + PHL_StopMusic(); + secretTimer = 210; +} + +void secretCountdown() +{ + if (secretTimer > 0) { + secretTimer -= 1; + if (secretTimer <= 0) { + PHL_StopMusic(); + if (bossFlag == 0 && bossDefeatedFlag == 0) { + PHL_PlayMusic(bgmMusic); + } + }else if (secretTimer == 180) { + PHL_PlayMusic(bgmSecret); + } + } +} + +int getDrawHP() +{ + return drawhp; +} + +void setDrawHP(int val) +{ + drawhp = val; +} + +int getLevel() +{ + return level; +} + +void setBossRoom() +{ + bossFlag = 1; + PHL_StopMusic(); + secretTimer = 0; + + PHL_FreeMusic(bgmMusic); + if (level != 8) { + bgmMusic = PHL_LoadMusic("midi/boss", 1); + }else{ + bgmMusic = PHL_LoadMusic("midi/lastboss", 1); + } + PHL_PlayMusic(bgmMusic); +} + +void setAutoSave(char val) +{ + autoSave = val; +} + +char getAutoSave() +{ + return autoSave; +} + +void loadUncommonImages() +{ + //Seal Toungs + if (level == 4) { + images[imgMisc2040] = PHL_LoadQDA("chr20x40.BMP"); + } + //Darkness + if (level == 5) { + images[imgDark] = PHL_LoadQDA("dark.bmp"); + } + //Dragon Flame + if (level == 7 || level == 8) { + images[imgMisc6020] = PHL_LoadQDA("chr60x20.bmp"); + } +} \ No newline at end of file diff --git a/src/game.h b/src/game.h new file mode 100644 index 0000000..c700f66 --- /dev/null +++ b/src/game.h @@ -0,0 +1,217 @@ +#ifndef GAME_H +#define GAME_H + +#include "PHL.h" +#include "enemy.h" +#include "enemies/slime.h" +#include "enemies/bat.h" +#include "enemies/slug.h" +#include "enemies/knight.h" +#include "enemies/heads.h" +#include "enemies/gas.h" +#include "enemies/skull.h" +#include "enemies/fish.h" +#include "enemies/waterjumper.h" +#include "enemies/podoboo.h" +#include "enemies/thwomp.h" +#include "enemies/dodo.h" +#include "enemies/batboss.h" +#include "enemies/crab.h" +#include "enemies/skeleton.h" +#include "enemies/ghoul.h" +#include "enemies/seal.h" +#include "enemies/jellyfish.h" +#include "enemies/wizard.h" +#include "enemies/pendulum.h" +#include "enemies/gyra.h" +#include "enemies/lolidra.h" +#include "enemies/bee.h" +#include "enemies/devil.h" +#include "enemies/firewheel.h" +#include "enemies/boar.h" +#include "enemies/golem.h" +#include "enemies/garm.h" +#include "enemies/poisonknight.h" +#include "enemies/dog.h" +#include "enemies/boomknight.h" +#include "enemies/pumpkin.h" +#include "enemies/hydra.h" +#include "object.h" +#include "effect.h" +#include "weapon.h" +#include "platform.h" + +#define TITLE 0 +#define GAME 1 +#define INVENTORY 2 +#define OPTIONS 3 +#define SAVING 4 +#define LEVELSTART 5 +#define GETITEM 6 + +//Sound channels +#define CHN_MUSIC 0 +#define CHN_SOUND 1 //Various sounds, like menus and fanfares +#define CHN_HERO 2 +#define CHN_WEAPONS 3 +#define CHN_ENEMIES 4 +#define CHN_EFFECTS 5 + +Door* lastDoor; + +int secretTimer; +int levelStartTimer; +int saveTimer; + +int quakeTimer; + +int bellFlag; +int bossFlag; +int bossDefeatedFlag; + +char roomDarkness; + +//Used for item get message +int itemGotX; +int itemGotY; + +int roomSecret; + +int collisionTiles[16][12]; + +//Playtime in frames. At 60 frames per second can hold ~828 1/2 days worth of playtime if my math isn't shit +unsigned long playTime; + +//Inventory +unsigned char hasWeapon[5]; +unsigned char hasItem[28]; +unsigned char hasKey[8]; + +//Save data flags +unsigned char flags[60]; + +PHL_Background background, + foreground; + +//Game assets +PHL_Surface images[15]; +PHL_Music bgmMusic; +PHL_Music bgmSecret; +PHL_Music bgmGameover; +PHL_Sound sounds[43]; + +#define MAX_WEAPONS 5 +Weapon* weapons[MAX_WEAPONS]; + +#define MAX_OBJECTS 40 +Object* objects[MAX_OBJECTS]; + +#define MAX_ENEMIES 20 +Enemy* enemies[MAX_ENEMIES]; + +#define MAX_EFFECTS 30 +Effect* effects[MAX_EFFECTS]; + +#define MAX_PLATFORMS 10 +Platform* platforms[MAX_PLATFORMS]; + +//Graphic names +#define imgTiles 0 +#define imgEnemies 1 +#define imgHud 2 +#define imgMisc20 3 +#define imgMisc32 4 +#define imgHero 5 +#define imgItems 6 +#define imgExplosion 7 +#define imgBoss 8 +#define imgMisc2040 9 +#define imgFontKana 10 +#define imgBoldFont 11 +#define imgDark 12 +#define imgMisc6020 13 +#define imgTitle01 14 + +//Sound names +#define sndBee01 0 +#define sndBell01 1 +#define sndBom01 2 +#define sndBom02 3 +#define sndBom03 4 +#define sndDoor00 5 +#define sndFire01 6 +#define sndGas01 7 +#define sndGet01 8 +#define sndGet02 9 +#define sndHit01 10 +#define sndHit02 11 +#define sndHit03 12 +#define sndHit04 13 +#define sndHit05 14 +#define sndHit06 15 +#define sndHit07 16 +#define sndJump01 17 +#define sndJump02 18 +#define sndNg 19 +#define sndOk 20 +#define sndPi01 21 +#define sndPi02 22 +#define sndPi03 23 +#define sndPi04 24 +#define sndPi05 25 +#define sndPi06 26 +#define sndPi07 27 +#define sndPi08 28 +#define sndPi09 29 +#define sndPi10 30 +#define sndPower01 31 +#define sndPower02 32 +#define sndShot01 33 +#define sndShot02 34 +#define sndShot03 35 +#define sndShot04 36 +#define sndShot05 37 +#define sndShot06 38 +#define sndShot07 39 +#define sndStep01 40 +#define sndWater01 41 +#define sndWolf01 42 + +void loadResources(); +void freeResources(); + +void game(); + +void gameSetup(); +void gameCleanup(); + +void enterDoor(); +void getItem(int itemNum); +void saveScreen(); + +void gameEnding(); + +//void enterDoor(Door* d); +void loadScreen(); + +void changeScreen(int dx, int dy); + +int writeSave(char* fname); +void loadSave(char* fname); + +int fileExists(char* fpath); + +void playSecret(); +void secretCountdown(); + +int getDrawHP(); +void setDrawHP(int val); + +int getLevel(); + +void setBossRoom(); + +void setAutoSave(char val); +char getAutoSave(); + +#endif \ No newline at end of file diff --git a/src/hero.c b/src/hero.c new file mode 100644 index 0000000..a7f5a7a --- /dev/null +++ b/src/hero.c @@ -0,0 +1,1406 @@ +#include "hero.h" +#include "game.h" +#include +#include "weapon.h" +#include "platform.h" +#include + +//State constants +const char NORMAL = 0; +const char SLASH = 1; +const char HIT = 2; +const char LADDER = 3; +const char STONE = 4; +const char CHARGE = 5; +//#define GETITEM 6 +const char DOOR = 7; +const char DEATH = 8; +const char QUAKE = 9; + +int state; + +void updateMask(); +void heroChangeScreen(int dx, int dy); + +int herodir = 1; + +int canCharge = 0; +int canJump = 0; +int onground = 0; +int heldUp = 0; + +const double GRAVITY = 0.3; +const double CLIMBSPEED = 1; + +double vsp = 0; +double hsp = 0; +double imageIndex = 0; +double jumpspd = 7.5; + + +int invincible = 0; +int timer = 0; +int chargeTimer = 0; +int shieldTimer = 0; //Holds up shield if this is 0 +int stun = 0; +int stunTimer = 0; + +int poisoned = 0; +int stoneTimer = 0; +int stoneState = 0; +int stoneDir = 1; + +int inWater = 0; +int drownTimer = 0; + +void heroSetup() +{ + state = NORMAL; + herodir = 1; + + herox = 320; + heroy = 320; + vsp = 0; + hsp = 0; + imageIndex = 0; + //climbspd = 1; + + invincible = 0; + timer = 0; + chargeTimer = 0; + shieldTimer = 0; + + poisoned = 0; + stoneTimer = 0; + stoneState = 0; + stoneDir = 1; + + herohp = 128; + maxhp = 128; + heroAmmo = 0; + maxAmmo = 99; + heroWeapon = -1; + + heroMask.unused = 0; + heroMask.circle = 0; + heroMask.w = 24; + heroMask.h = 26; + + onground = 0; + canJump = 0; + + heroy += 1; + if (checkTileCollision(1, getHeroMask()) == 1 || checkTileCollision(3, getHeroMask()) == 1) { + onground = 1; + if (hasItem[12] == 1) { + canJump = 1; + } + } + heroy -= 1; + + shieldMask.unused = 1; + shieldMask.circle = 0; + shieldMask.w = 24; + shieldMask.h = 24; + shieldMask.x = 0; + shieldMask.y = 0; + + inWater = -1; +} + +int heroStep() +{ + int result = -1; + + //set HP limits + { + if (herohp > maxhp) { + herohp = maxhp; + } + + if (herohp < 0) { + herohp = 0; + } + } + + heldUp = btnUp.held; + + //Counters + { + if (invincible > 0) { + invincible -= 1; + } + } + + //Scripted states + if (state == DOOR) { + //Remove some status conditions + stun = 0; + poisoned = 0; + inWater = 0; + + //Animate + imageIndex += 0.2; + + //Done walking + if (imageIndex >= 10) { + enterDoor(); + state = NORMAL; + } + } + + else if (state == DEATH) { + stun = 0; + stunTimer = 0; + poisoned = 0; + + //Animate + { + imageIndex += 0.3; + if (imageIndex >= 4) { + imageIndex -= 4; + } + + //blinking + if (timer >= 90) { + invincible = 1; + }else{ + invincible = 0; + } + } + + timer += 1; + + //Poof + if (timer == 90) { + createEffect(2, herox - 32, heroy - 12); + } + //Play Music + if (timer == 150) { + PHL_PlayMusic(bgmGameover); + } + + //End game over screen prematurly + if (timer > 150 && btnStart.pressed == 1) { + btnStart.pressed = 0; + timer = 630; + } + + //Reset game + if (timer == 630) { + /* + FILE* f; + if ((f = fopen("data/save.tmp", "rb"))) { + remove("data/save.tmp"); + } + fclose(f); + */ + PHL_StopMusic(); + result = 1; + } + } + + //Uncontrollable states, but can move + else { + char canGrav = 1; + double grav = GRAVITY; + + if (state == CHARGE) { + canGrav = 0; + shieldTimer = 10; + vsp = 0; + + //Charge start (rear back) + { + if (timer == 0) { + imageIndex = 0; + hsp = -2 * herodir; + } + } + + //Friction + { + double fric = 0.3; + + if (hsp < 0) { + hsp += fric; + if (hsp >= 0) { + hsp = 0; + } + }else if (hsp > 0) { + hsp -= fric; + if (hsp <= 0) { + hsp = 0; + } + } + } + + timer += 1; + + //Forward charge start + { + if (timer == 15) { + invincible = 35; + hsp = 7 * herodir; + PHL_PlaySound(sounds[sndShot01], CHN_WEAPONS); + } + } + + //Animation + { + if (timer > 15) { + imageIndex = 1; + } + if (timer > 19) { + imageIndex = 2; + } + if (timer > 21) { + imageIndex = 3; + } + if (timer == 59) { + imageIndex = 4; + } + } + + //Stop + if (timer == 30) { + hsp = 0; + } + + //End state + if (timer >= 60) { + state = NORMAL; + } + + } + + else if (state == HIT) { + grav = GRAVITY - 0.05; + + //timer + { + timer -= 1; + if (timer < 0) { + timer = 0; + } + } + + //Animate + { + imageIndex += 0.33; + if (imageIndex >= 2) { + imageIndex -= 2; + } + } + + if (onground == 1) { + hsp = 0; + } + + //End hit state + { + if (onground == 1 && vsp == 0 && timer == 0) { + state = NORMAL; + invincible = 60; + } + } + + } + + else if (state == STONE) { + grav = GRAVITY - 0.05; + + if (stoneState != 2) { + stoneTimer -= 1; + //Setup break free animation + if (stoneTimer <= 0) { + stoneTimer = 0; + stoneState = 2; + imageIndex = 0; + + createRockSmash(herox, heroy + 20); + herodir = stoneDir; + } + } + + //Animate + imageIndex += 0.16; + + //Frozen state flashes + if (stoneState != 2 && imageIndex >= 2) { + imageIndex = 0; + } + + //In air + if (stoneState == 0) { + if (onground == 0) { + //hsp = -(herodir * 2); + }else{ + stoneState = 1; + createEffect(9, herox, heroy + 20); + createEffect(9, herox, heroy + 20); + } + } + //On ground + else if (stoneState == 1) { + hsp = 0; + if (btnFaceDown.pressed == 1) { + stoneTimer -= 30; + createEffect(9, herox, heroy + 20); + } + } + //Break free animation + else if (stoneState == 2) { + imageIndex += 0.16; + + if ((int)imageIndex == 3) { + createEffect(8, herox - 32, heroy - 22); + imageIndex += 0.5; + } + + if (imageIndex >= 17) { + state = NORMAL; + stoneState = 0; + } + } + } + + else if (state == QUAKE) { + grav = GRAVITY - 0.05; + hsp = 0; + + if (onground == 1) { + PHL_PlaySound(sounds[sndPi02], CHN_HERO); + if (timer == 0) { + vsp = -2 - grav; + onground = 0; + } + else if (timer == 1) { + vsp = -1 - grav; + onground = 0; + } + else if (timer == 2) { + vsp = -0.5 - grav; + onground = 0; + } + else if (timer == 3) { + state = NORMAL; + vsp = 0; + } + timer += 1; + } + } + + //Controllable states + else { + char canWalk = 1; + + if (state == NORMAL) { + //Timers + { + if (shieldTimer > 0) { + shieldTimer -= 1; + } + } + + //Change direction with buttons + { + if (btnLeft.held == 1) { + herodir = -1; + } + if (btnRight.held == 1) { + herodir = 1; + } + } + + //Jumping + { + if (btnFaceDown.pressed == 1) { + if (onground == 1 || canJump == 1) { + if (onground == 0) { + canJump = 0; + } + vsp = -jumpspd; + onground = 0; + PHL_PlaySound(sounds[sndJump01], CHN_HERO); + } + } + + //cancel jump + if (vsp < 0 && btnFaceDown.released == 1) { + vsp = 0; + } + } + + //Animate + { + if (onground == 1 && hsp != 0) { + imageIndex += 0.1; + if (imageIndex >= 2) { + imageIndex -= 2; + } + } + } + + //Charging + { + if (canCharge == 1 && btnFaceLeft.held == 1) { + chargeTimer += 1; + //Create Effects + if (chargeTimer >= 10 && chargeTimer < 66 && ((chargeTimer - 10) % 8) == 0) { + createEffect(6, herox, heroy + 20); + } + + if (chargeTimer == 70) { + PHL_PlaySound(sounds[sndPower01], CHN_SOUND); + } + } + + if (canCharge == 1 && chargeTimer >= 70 && btnFaceLeft.released == 1) { + state = CHARGE; + timer = 0; + imageIndex = 0; + addWeapon(SWORD, herox, heroy); + } + } + + //Attack + { + if (stun == 0) { + //Slash + if (btnFaceLeft.pressed == 1) { + state = SLASH; + imageIndex = 0; + PHL_PlaySound(sounds[sndShot01], CHN_WEAPONS); + addWeapon(SWORD, herox, heroy); + } + + //Weapon + if (btnFaceRight.pressed == 1) { + if (heroWeapon != -1) { + addWeapon(heroWeapon, (int)herox - 20, (int)heroy); + } + } + } + } + + //Grabbing Ladder + { + //Grab ladder + if (btnUp.held == 1) { + PHL_Rect collide = getTileCollisionXY(2, herox, heroy + 20); + if (collide.x != -1) { + state = LADDER; + canWalk = 0; + hsp = 0; + vsp = 0; + herox = collide.x + 20; + } + } + + //Climb down onto ladder + else if (onground == 1 && btnDown.held == 1) { + PHL_Rect collide = getTileCollisionXY(3, herox, heroy + 40); + if (collide.x != -1) { + state = LADDER; + canWalk = 0; + hsp = 0; + vsp = 0; + herox = collide.x + 20; + heroy += 1; + } + } + } + + } + + else if (state == SLASH) { + shieldTimer = 10; + + //Can move in air, not on the ground + if (onground == 1) { + canWalk = 0; + hsp = 0; + } + + //Animate + { + double imgspd = 0.25; + + if (imageIndex < 1) { + imgspd = 0.25; + }else if (imageIndex < 2) { + imgspd = 0.34; + }else if (imageIndex < 3) { + imgspd = 0.34; + }else if (imageIndex < 4) { + imgspd = 0.125; + }else if (imageIndex < 5) { + imgspd = 0.5; + } + + imageIndex += imgspd; + } + + //Finish slash + { + if (imageIndex >= 5) { + state = NORMAL; + canCharge = hasItem[17]; //Has red scroll + chargeTimer = 0; + } + } + + } + + else if (state == LADDER) { + onground = 0; + canWalk = 0; + canGrav = 0; + hsp = 0; + vsp = 0; + + //Generate final climb speed + double climbspd = CLIMBSPEED; + { + //Has power bracelet + if (hasItem[4] == 1) { + climbspd *= 2; + } + + //Stun slows climb speed + if (stun > 0) { + climbspd /= 2; + } + } + + //Get up/down axis + int yaxis = btnDown.held - btnUp.held; + + //Animate + if (yaxis != 0) { + imageIndex += 0.125; + + //Limit imageIndex + if (imageIndex >= 8) { + imageIndex -= 8; + } + } + + //Movement + heroy += climbspd * yaxis; + + //Touch ground + { + if (yaxis == 1) { + PHL_Rect collide = getTileCollision(1, getHeroMask()); + if (collide.x != -1) { + state = NORMAL; + heroy = collide.y - 40; + imageIndex = 0; + } + } + } + + //Off of ladder + { + if (yaxis != 0) { + if (checkTileCollision(2, getHeroMask()) == 0 && checkTileCollision(3, getHeroMask()) == 0) { + state = NORMAL; + if (btnDown.held == 1) { + onground = 0; + } + } + } + } + } + + //Walking + { + if (canWalk == 1) { + int xaxis = btnRight.held - btnLeft.held; + hsp = 3 * xaxis; + } + } + + //Cancel jump + { + if (vsp < 0 && btnFaceDown.released == 1) { + vsp = 0; + } + } + + //Earthquake + { + if (hasItem[11] == 0) { //Does not have amulete + if (quakeTimer > 0 && onground == 1) { + state = QUAKE; + vsp = -3 - grav; + timer = 0; + PHL_PlaySound(sounds[sndPi02], CHN_HERO); + } + } + } + + } + + //Movement + { + //Used to prevent glitching out on ladder tops + int precheckladder = checkTileCollision(3, getHeroMask()); + + //Horizontal movement + { + if (hsp != 0) { + double finalhsp = hsp; + //Slow when climbing and stunned + { + if ( (inWater == 1 && hasItem[5] == 0) || stun == 1) { + finalhsp /= 4; + } + } + + //Speed up movement in water + { + if (inWater == 1 && hasItem[5] == 1) { //Has fins + finalhsp = (finalhsp / 3) * 2; + } + } + + //Move + herox += finalhsp; + + //Stay within screen during boss fight + { + if (bossFlag == 1) { + if (herox < 10) { + herox = 10; + } + if (herox > 630) { + herox = 630; + } + } + } + + //Collide with wall + { + PHL_Rect collide = getTileCollision(1, getHeroMask()); + if (collide.x == -1 && precheckladder == 0) { + collide = getTileCollision(3, getHeroMask()); + } + + //Did collide + if (collide.x != -1) { + if (hsp > 0) { + herox = collide.x - (heroMask.w / 2); + }else if (hsp < 0) { + herox = collide.x + 40 + (heroMask.w / 2); + } + + if (state == STONE) { + herodir *= -1; + } + } + } + + //Check if walked off ledge + if (vsp >= 0) { + heroy += 1; + + if ( checkTileCollision(1, getHeroMask()) //Solid ground + || (hasItem[13] == 1 && checkTileCollision(5, getHeroMask())) //Has red shoes + || (precheckladder == 0 && checkTileCollision(3, getHeroMask())) ) //Ladder tops + {}else{ + onground = 0; + } + + heroy -= 1; + } + } + } + + //Vertical Movement + { + //Gravity + if (canGrav == 1 && onground == 0) { + int maxVsp = 8; + + //Water slows movement + if (inWater == 1) { + grav *= 0.5; + maxVsp *= 0.5; + } + + vsp += grav; + if (vsp > maxVsp) { + vsp = maxVsp; + } + } + + //Vertical Movement + { + double tempVsp = vsp; + char landed = 0; + + //Water slows movement + if (inWater == 1 || stun == 1) { + tempVsp *= 0.5; + } + + //Movement + heroy += tempVsp; + + //Colliding with floor/ceiling + { + PHL_Rect collide = getTileCollision(1, getHeroMask()); + if (collide.x == -1&& precheckladder == 0) { + collide = getTileCollision(3, getHeroMask()); + } + if (collide.x == -1 && hasItem[13] == 1) { //has red shoes + collide = getTileCollision(5, getHeroMask()); + } + + if (collide.x != -1) { + //Collide with floor + if (vsp > 0) { + heroy = collide.y - 40; + vsp = 0; + + onground = 1; + if (hasItem[12] == 1) { //Has blue boots + canJump = 1; + } + + landed = 1; + } + + //Collide with ceiling + else if (vsp < 0) { + heroy = collide.y + 40 - (40 - heroMask.h); + } + } + + else{ + //Jumpthrough/moving platforms + if (vsp >= 0) { + int i; + for (i = 0; i < MAX_PLATFORMS; i++) { + if (platforms[i] != NULL) { + int onPlatTop = 0; + if (herox - (heroMask.w / 2) > platforms[i]->mask.x + platforms[i]->mask.w || herox + (heroMask.w / 2) < platforms[i]->mask.x) { + } + else{ + if (platforms[i]->y == heroy + 40 && vsp >= 0) { + onPlatTop = 1; + } + } + + if (onPlatTop == 1 || checkCollision(getHeroMask(), platforms[i]->mask) == 1) { + heroMask.y -= vsp; + if (onPlatTop == 1 || checkCollision(heroMask, platforms[i]->mask) == 0) { + heroy = platforms[i]->mask.y - 40; + if (vsp != 0) { + landed = 1; + } + vsp = 0; + onground = 1; + if (hasItem[12] == 1) { + canJump = 1; + } + } + } + } + } + } + } + } + + //Land on ground after a hit + if (landed == 1 && (state == HIT || state == STONE)) { + timer = 60; + PHL_PlaySound(sounds[sndHit01], CHN_HERO); + + createEffectExtra(3, herox - 30, heroy + 8, -1, 0, 0); + createEffectExtra(3, herox - 10, heroy + 8, 1, 0, 0); + } + + } + + } + + } + + //Water stuff + { + //Drown/bubble + if (inWater == 1) { + drownTimer -= 1; + if (drownTimer <= 0) { + drownTimer = 60; + if (hasItem[6] == 0) { + herohp -= 4; + } + createEffect(12, herox, heroy + 20); + PHL_PlaySound(sounds[sndPi06], CHN_SOUND); + } + } + + //Splash + if (checkTileCollision(4, getHeroMask())) { + if (inWater == 0) { + drownTimer = 60; + //Splash effect + createSplash(herox, heroy); + } + inWater = 1; + }else{ + if (checkTileCollision(6, getHeroMask()) == 0) { + if (inWater == 1) { + //Splash effect + createSplash(herox, heroy); + } + inWater = 0; + } + } + } + + //Poison + { + if (poisoned > 0) { + poisoned -= 1; + if (poisoned % 20 == 0) { + herohp -= 1; + createEffect(7, herox, heroy); + } + } + } + + //Switch weapon + { + int axis = btnR.pressed - btnL.pressed; + + if (axis != 0) { + PHL_PlaySound(sounds[sndPi01], CHN_SOUND); + + int i; + for (i = 1; i <= 5; i++) { + int thisweapon = heroWeapon + (i * axis); + + if (thisweapon >= 5) { + thisweapon -= 5; + } + if (thisweapon < 0) { + thisweapon += 5; + } + + if (hasWeapon[thisweapon] == 1) { + heroWeapon = thisweapon; + i = 6; + } + } + } + } + + //Collide with lava + { + heroy -= 20; + if (checkTileCollision(5, getHeroMask())) { + herohp = 0; + setDrawHP(0); + } + heroy += 20; + } + + //Collide with spikes + { + PHL_Rect spike = getTileCollision(6, getHeroMask()); + if (spike.x != -1) { + Mask spikeMask; + spikeMask.circle = spikeMask.unused = 0; + spikeMask.x = spike.x + 10; + spikeMask.y = spike.y + 10; + spikeMask.w = spikeMask.h = 20; + + if (checkCollision(spikeMask, getHeroMask())) { + heroHit(15, spike.x + 20); + } + } + } + + //Death + { + if (getDrawHP() <= 0) { //Based on the hud's opinion on player's health, apparently + state = DEATH; + timer = 0; + imageIndex = 0; + PHL_StopMusic(); + PHL_PlaySound(sounds[sndHit02], CHN_HERO); + } + } + } + + //Manage charge + { + if (state != NORMAL) { + canCharge = 0; + chargeTimer = 0; + } + + if (canCharge == 1) { + if (btnFaceLeft.held == 0 && btnFaceLeft.pressed == 0) { + canCharge = 0; + } + } + } + + //Screen transitions + { + if (herox < -20) { + herox = 620; + heroChangeScreen(-1, 0); + } + else if (herox > 660) { + herox = 20; + heroChangeScreen(1, 0); + } + else if (state == LADDER && heroy < -40) { + heroy = 440; + heroChangeScreen(0, -1); + } + else if (heroy > 480) { + heroy = 0; + heroChangeScreen(0, 1); + } + } + + return result; +} + +void heroChangeScreen(int dx, int dy) +{ + vsp = 0; + chargeTimer = 0; + canCharge = 0; + if (hasItem[12] == 1) { + canJump = 1; + } + if (state == HIT || state == SLASH || state == CHARGE) { + state = NORMAL; + } + + //Force a black screen + PHL_DrawRect(0, 0, 640, 480, PHL_NewRGB(0, 0, 0)); + PHL_ForceScreenUpdate(); + + changeScreen(dx, dy); +} + + +void heroDraw() +{ + int cropX = 0, cropY = 0; + int drawShield = 0; + + if (state == DOOR) { + cropY = 160; + cropX = (int)imageIndex * 40; + } + + + else if (state == GETITEM) { + int animation[7] = {0, 1, 2, 3, 2, 0, 1}; + cropY = 40; + cropX = 320 + (animation[(int)imageIndex] * 40); + } + + //Climbing + else if (state == LADDER) { + cropX = 80; + cropY = 80; + int animation[8] = {0, 1, 2, 1, 0, 3, 4, 3}; + cropX += 40 * animation[(int)floor(imageIndex)]; + } + + + else if (state == NORMAL) + { + if (onground == 1) { + //Walking + if (hsp != 0) { + cropX = floor(imageIndex) * 40; + if (herodir == -1) { + cropX += 80; + } + } + + //Standing + else{ + imageIndex = 0; + cropX = 0; + cropY = 0; + if (hasItem[14] == 1 && shieldTimer <= 0) { + drawShield = 1; + cropY = 120; + if (herodir == -1) { + cropX += 40; + } + if (heldUp == 1) { + cropX += 80; + } + }else{ + if (herodir == -1) { + cropX += 80; + } + } + } + }else{ + //Jumping/falling + if (vsp < 0) { + imageIndex = 0; + }else{ + imageIndex = 1; + } + cropX = 160 + (40 * imageIndex); + if (herodir == -1) { + cropX += 80; + } + } + }else if (state == SLASH) + { + //Sword Slash + int animation[5] = {0, 1, 2, 2, 0}; + + cropY = 40; + cropX = 40 * animation[(int)floor(imageIndex)]; + if (herodir == -1) { + cropX += 120; + } + } + else if (state == CHARGE) { + int animation[5] = {0, 1, 2, 2, 0}; + cropY = 40; + cropX = animation[(int)imageIndex] * 40; + if (herodir == -1) { + cropX += 120; + } + } + else if (state == HIT) { + int thisImage = 12; + + if (onground == 0) { + thisImage = 8; + } + + if (state == STONE) { + thisImage = 28; + } + + thisImage += (int)imageIndex; + if (herodir == -1) { + thisImage += 2; + } + + cropX = 40 * thisImage; + + while (cropX >= 640) { + cropX -= 640; + cropY += 40; + } + } + else if (state == STONE) { + cropY = 40; + + if (stoneState == 0 || stoneState == 1) { //In air/on ground + int thisImage = (int)imageIndex; + if (stoneDir == -1) { + thisImage += 2; + } + + cropX = 480 + (thisImage * 40); + } + else if (stoneState == 2) { //Break free + int animation[17] = {0, 1, 2, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 2, 0, 1, 0}; + cropX = 320 + (animation[(int)imageIndex] * 40); + } + } + else if (state == DEATH) { + if (timer >= 130) { + char tempDark = roomDarkness; + roomDarkness = 0; + + PHL_DrawTextBold("GAME OVER", 248, 240, YELLOW); + + roomDarkness = tempDark; + }else{ + int frame = 0; + if (herodir == 1) { + int animation[4] = {0, 3, 6, 9}; + frame = animation[(int)imageIndex]; + } + if (herodir == -1) { + int animation[4] = {2, 1, 4, 11}; + frame = animation[(int)imageIndex]; + } + cropX = frame * 40; + } + } + else if (state == QUAKE) { + cropY = 80; + if (herodir == -1) { + cropX = 40; + } + } + + if ((state == HIT && invincible % 6 < 3) || invincible % 2 == 0) { + PHL_DrawSurfacePart(herox - 20, heroy, cropX, cropY, 40, 40, images[imgHero]); + if (drawShield == 1) { + int scx = 320; //Shield crop x + int sdx = herox - 2, sdy = heroy + 10; //Shield draw x/y + if (herodir == -1) { + sdx -= 36; + scx += 40; + } + if (heldUp == 1) { + scx += 80; + sdy -= 26; + sdx -= 8 * herodir; + } + PHL_DrawSurfacePart(sdx, sdy, scx, 240, 40, 40, images[imgHero]); + } + } + + //Draw stun effect + if (stun == 1) { + int frame = (int)(((300 - stunTimer) % 32) / 4); + if (frame == 0) { + PHL_PlaySound(sounds[sndHit05], CHN_SOUND); + } + + int animation[8] = {0, 1, 2, 1, 0, -1, -1, -1}; + + if (animation[frame] != -1) { + PHL_DrawSurfacePart(herox - 32, heroy - 12, 384 + (animation[frame] * 64), 64, 64, 64, images[imgMisc32]); + } + + if (stunTimer <= 0) { + stun = 0; + PHL_PlaySound(sounds[sndPower01], CHN_SOUND); + }else{ + stunTimer -= 1; + } + } + + //PHL_DrawRect(mask.x, mask.y, mask.w, mask.h, PHL_NewRGB(0x00, 0x00, 0xFF)); + updateMask(); + //PHL_DrawMask(shieldMask); +} + +void updateMask() +{ + heroMask.x = herox - 12; + heroMask.y = heroy + 14; + + //Update shield mask + { + shieldMask.unused = 1; + if (hasItem[14] == 1) { //has shield + if (state == NORMAL && onground == 1 && hsp == 0 && shieldTimer == 0) { + shieldMask.unused = 0; + + //Shield held in front + if (heldUp == 0) { + shieldMask.w = 14; + shieldMask.h = 20; + shieldMask.x = herox + 10; + shieldMask.y = heroy + 20; + if (herodir == -1) { + shieldMask.x -= 34; + } + } + + //Shield above head + else{ + shieldMask.w = 24; + shieldMask.h = 8; + shieldMask.x = herox - 2; + shieldMask.y = heroy - 2; + if (herodir == -1) { + shieldMask.x -= 20; + } + } + } + } + } + +} + +int heroHit(int damage, int centerx) +{ + if (state != HIT && state != DEATH && state != DOOR && (invincible <= 0 || (state == STONE && invincible == 60))) { + if (state != STONE || (state == STONE && stoneState != 2)) { + PHL_PlaySound(sounds[sndHit02], CHN_HERO); + herohp -= damage; + + vsp = -4; + onground = 0; + + if (herox - centerx > 0) { + herodir = -1; + hsp = herodir * -2; + } + + if (herox - centerx < 0) { + herodir = 1; + hsp = herodir * -2; + } + + if (state != STONE) { + state = HIT; + } + + return 1; + } + } + return 0; +} + +void heroPoison() +{ + if (hasItem[8] != 1) { //Does not have poison resistance ring + if (poisoned <= 0) { + poisoned = 300; + } + } +} + +void heroStone() +{ + //if (((state != HIT && state != DEATH && state != DOOR ) || (state == STONE && stoneState != 2)) && invincible <= 0) { + if (state != HIT && state != DEATH && state != DOOR && (invincible <= 0 || (state == STONE && invincible == 60))) { + if (state != STONE || (state == STONE && stoneState != 2)) { + if (hasItem[9] != 1) { //Does not have green ring + if (state == STONE) { + herodir = stoneDir; + } + setHeroState(STONE); + } + } + } +} + +//Get-ers and set-ers +Mask getHeroMask() +{ + updateMask(); + return heroMask; +} + +int getHeroState() +{ + return state; +} + +void setHeroState(int s) +{ + state = s; + + //Special cases + if (s == GETITEM) { + heldUp = 0; + //timer = 0; + //subPosition = GETITEM; + } + + if (s == DOOR) { + imageIndex = 0; + } + + if (s == STONE) { + if (stoneTimer <= 0) { + stoneTimer = 350; + } + stoneState = 0; + invincible = 60; + stoneDir = herodir; + } +} + +int getHeroInvincible() +{ + return invincible; +} + +int getHeroDirection() +{ + return herodir; +} + +void setHeroDirection(int d) +{ + herodir = d; +} + +double getHeroImageIndex() +{ + return imageIndex; +} + +void setHeroImageIndex(double index) +{ + imageIndex = index; +} + +double getHeroVsp() +{ + return vsp; +} + +double getHeroHsp() +{ + return hsp; +} + +void setHeroHsp(double newHsp) +{ + hsp = newHsp; +} + +void setHeroVsp(double newVsp) +{ + vsp = newVsp; +} + +int getHeroOnground() +{ + return onground; +} + +void setHeroOnground(int val) +{ + onground = val; +} + +void setHeroTimer(int t) +{ + timer = t; +} + +int getHeroPoisoned() +{ + return poisoned; +} + +void heroStun() +{ + if (hasItem[10] == 0) { //Does not have cloak + stun = 1; + if (stunTimer <= 0) { + stunTimer = 300; + } + } +} + +void setHeroCanjump(int set) +{ + canJump = set; +} \ No newline at end of file diff --git a/src/hero.h b/src/hero.h new file mode 100644 index 0000000..89fbf4b --- /dev/null +++ b/src/hero.h @@ -0,0 +1,55 @@ +#ifndef HERO_H +#define HERO_H + +#include "PHL.h" +#include "collision.h" + +double herox, heroy; +double herohp, maxhp; +int heroAmmo, maxAmmo; +int heroWeapon; + +Mask heroMask; +Mask shieldMask; + +void heroSetup(); +void heroCleanup(); +int heroStep(); +void heroDraw(); + +int heroHit(int damage, int centerx); + +void heroPoison(); +void heroStone(); + +Mask getHeroMask(); + +int getHeroState(); +void setHeroState(int s); + +int getHeroInvincible(); + +int getHeroDirection(); +void setHeroDirection(int d); + +double getHeroImageIndex(); +void setHeroImageIndex(double index); + +double getHeroVsp(); +double getHeroHsp(); + +void setHeroHsp(double newHsp); +void setHeroVsp(double newVsp); + +int getHeroOnground(); +void setHeroOnground(int val); + +void setHeroTimer(int t); + +int getHeroPoisoned(); + +void heroStun(); + +void setHeroCanjump(int set); + +#endif \ No newline at end of file diff --git a/src/ini.c b/src/ini.c new file mode 100644 index 0000000..99b2f62 --- /dev/null +++ b/src/ini.c @@ -0,0 +1,304 @@ +#include "ini.h" +#include "game.h" +#include "PHL.h" +#include +#include +#include "text.h" + +//char* getFileLocation(); +char* trimString(char* orig); + +void screenLoad(char* first, char* second); +void sizeLoad(char* first, char* second); +void blurLoad(char* first, char* second); +void languageLoad(char* first, char* second); +void autosaveLoad(char* first, char* second); + +void iniInit() +{ + //Build filepath + char fullPath[128]; + { + #ifdef _SDL + strcpy(fullPath, getenv("HOME")); + strcat(fullPath, "/.hydracastlelabyrinth/"); + #else + strcpy(fullPath, ""); + #endif + #ifdef _3DS + strcat(fullPath, "sdmc:/3ds/appdata/HydraCastleLabyrinth/"); + #endif + strcat(fullPath, "system.ini"); + } + + FILE* f; + + if ( (f = fopen(fullPath, "rt")) ) + { + //File exists - read it + fclose(f); + loadSettings(); + }else{ + //File does not exists - create it (with default hardcoded settings) + saveSettings(); + } + +} + +void saveSettings() +{ + //Build filepath + char fullPath[128]; + { + #ifdef _SDL + strcpy(fullPath, getenv("HOME")); + strcat(fullPath, "/.hydracastlelabyrinth/"); + #else + strcpy(fullPath, ""); + #endif + #ifdef _3DS + strcat(fullPath, "sdmc:/3ds/appdata/HydraCastleLabyrinth/"); + #endif + strcat(fullPath, "system.ini"); + } + + FILE* f; + + if ( (f = fopen(fullPath, "wt")) ) + { + fprintf(f, "[disp]"); + #ifdef _3DS + //Screen + fprintf(f, "\r\nscreen="); + if (activeScreen->screen == GFX_BOTTOM) { + fprintf(f, "bottom"); + }else{ + fprintf(f, "top"); + } + #endif + + #ifdef _PSP + //Screen Size + fprintf(f, "\r\nsize="); + if (getScreenSize() == 1) { + fprintf(f, "1"); + } + else if (getScreenSize() == 2) { + fprintf(f, "2"); + } + else { + fprintf(f, "0"); + } + + //Screen Blur + fprintf(f, "\r\nblur="); + if (getBlur() == 1) { + fprintf(f, "on"); + }else{ + fprintf(f, "off"); + } + #endif + + fprintf(f, "\r\n[system]"); + + //Language + fprintf(f, "\r\nlanguage="); + if (getLanguage() == 0) { + fprintf(f, "jp"); + } + if (getLanguage() == 1) { + fprintf(f, "en"); + } + + //Autosave + fprintf(f, "\r\nautosave="); + if (getAutoSave() == 1) { + fprintf(f, "on"); + }else{ + fprintf(f, "off"); + } + fclose(f); + } + + + +} + +void loadSettings() +{ + //Build filepath + char fullPath[128]; + { + #ifdef _SDL + strcpy(fullPath, getenv("HOME")); + strcat(fullPath, "/.hydracastlelabyrinth/"); + #else + strcpy(fullPath, ""); + #endif + #ifdef _3DS + strcat(fullPath, "sdmc:/3ds/appdata/HydraCastleLabyrinth/"); + #endif + strcat(fullPath, "system.ini"); + } + + FILE* f; + + if ( (f = fopen(fullPath, "rt")) ) + { + char line[80]; + + while ( (fgets(line, 80, f) != NULL) ) + { + char* lineptr = line; + lineptr = trimString(lineptr); + + if (lineptr != NULL) { + //Ignore catigory lines + if (lineptr[0] != '[') + { + //Check if it has a = delimiter first + int i; + for (i = 0; i < 80; i++) { + if (line[i] == '=') + { + //Begin line splitting + char* half; + if ( (half = strsep(&lineptr, "=")) != NULL) + { + //first half + char* fhalf = half; + + if ( (half = strsep(&lineptr, "=")) != NULL) { + //Second half + char* shalf = half; + + //Load options + screenLoad(fhalf, shalf); + sizeLoad(fhalf, shalf); + blurLoad(fhalf, shalf); + languageLoad(fhalf, shalf); + autosaveLoad(fhalf, shalf); + } + } + + //End + i = 81; + } + } + } + } + } + fclose(f); + } + +} + +//Build file path +/* +char* getFileLocation() +{ + char fullPath[128]; + strcpy(fullPath, ""); + #ifdef _CIA + strcat(fullPath, "sdmc:/3ds/HydraCastleLabyrinth/"); + #endif + strcat(fullPath, "system.ini"); + + return fullPath; +} +*/ + +char* trimString(char* orig) +{ + char* output = orig; + + int i, r = 0; + for (i = 0; i < strlen(orig); i++) { + if (orig[i] != ' ' && orig[i] != '\n' && orig[i] != '\r') { + output[r] = orig[i]; + r++; + } + } + + orig[r] = 0; + + return output; +} + +void screenLoad(char* first, char* second) +{ + #ifdef _3DS + if (strcmp(first, "screen") == 0) { + if (strcmp(second, "top") == 0) { + swapScreen(GFX_TOP, GFX_LEFT); + } + if (strcmp(second, "bottom") == 0) { + swapScreen(GFX_BOTTOM, GFX_LEFT); + } + } + #endif +} + +void sizeLoad(char* first, char* second) +{ + #ifdef _PSP + if (strcmp(first, "size") == 0) { + if (second[0] == '0') { + //fprintf(debug, "\nsize is 0"); + setScreenSize(0); + } + if (second[0] == '1') { + //fprintf(debug, "\nsize is 1"); + setScreenSize(1); + } + if (second[0] == '2') { + //fprintf(debug, "\nsize is 2"); + setScreenSize(2); + } + } + #endif +} + +void blurLoad(char* first, char* second) +{ + #ifdef _PSP + if (strcmp(first, "blur") == 0) { + if (strcmp(second, "on") == 0) { + //fprintf(debug, "\nblur is on"); + //oslSetBilinearFilter(1); + setBlur(1); + } + if (strcmp(second, "off") == 0) { + //fprintf(debug, "\nblur is off"); + //oslSetBilinearFilter(0); + setBlur(0); + } + } + #endif +} + +void languageLoad(char* first, char* second) +{ + if (strcmp(first, "language") == 0) { + if (strcmp(second, "en") == 0) { + setLanguage(ENGLISH); + } + if (strcmp(second, "jp") == 0) { + setLanguage(JAPANESE); + } + } +} + +void autosaveLoad(char* first, char* second) +{ + if (strcmp(first, "autosave") == 0) { + if (strcmp(second, "on") == 0) { + //fprintf(debug, "\nautosave is on"); + setAutoSave(1); + } + if (strcmp(second, "off") == 0) { + //fprintf(debug, "\nautosave is off"); + setAutoSave(0); + } + } +} \ No newline at end of file diff --git a/src/ini.h b/src/ini.h new file mode 100644 index 0000000..c97fd01 --- /dev/null +++ b/src/ini.h @@ -0,0 +1,10 @@ +#ifndef INI_H +#define INI_H + +//Functions to handle the "system.ini" file +void iniInit(); + +void saveSettings(); +void loadSettings(); + +#endif \ No newline at end of file diff --git a/src/inventory.c b/src/inventory.c new file mode 100644 index 0000000..ae6845d --- /dev/null +++ b/src/inventory.c @@ -0,0 +1,149 @@ +#include "inventory.h" +#include "PHL.h" +#include "game.h" +#include "text.h" + +int cursorX = 0; +int cursorY = 0; + +void inventory() +{ + char tempDark = roomDarkness; + roomDarkness = 0; + + char loop = 1; + PHL_PlaySound(sounds[sndPi04], CHN_SOUND); + while (PHL_MainLoop() && loop == 1) + { + PHL_StartDrawing(); + PHL_ScanInput(); + + if (inventoryStep() == 1) { + loop = 0; + } + + inventoryDraw(); + + PHL_EndDrawing(); + } + + roomDarkness = tempDark; +} + +int inventoryStep() +{ + secretCountdown(); + + //Input + char playsnd = 0; + + if (btnRight.pressed - btnLeft.pressed != 0) { + cursorX += btnRight.pressed - btnLeft.pressed; + playsnd = 1; + } + if (btnDown.pressed - btnUp.pressed != 0) { + cursorY += btnDown.pressed - btnUp.pressed; + playsnd = 1; + } + + if (playsnd == 1) { + PHL_PlaySound(sounds[sndPi01], CHN_SOUND); + } + + //Limit cursor + if (cursorX < 0) { cursorX = 6; } + if (cursorX > 6) { cursorX = 0; } + if (cursorY < 0) { cursorY = 3; } + if (cursorY > 3) { cursorY = 0; } + + if (btnStart.pressed == 1 || btnDecline.pressed == 1) + { + return 1; + } + + return 0; +} + +void inventoryDraw() +{ + //Black background + PHL_DrawRect(0, 0, 640, 480, PHL_NewRGB(0, 0, 0)); + + //Labels + PHL_DrawTextBold("SUB WEAPON", 16, 16, YELLOW); + PHL_DrawTextBold("ITEM", 16, 96, YELLOW); + PHL_DrawTextBold("KEY", 16, 320, YELLOW); + + //Blue rectangles + int i, a, cx, cy; + //Weapons + for (i = 0; i < 5; i++) { + PHL_DrawRect(18 + (48 * i), 34, 44, 44, PHL_NewRGB(255, 255, 255)); + PHL_DrawRect(20 + (48 * i), 36, 40, 40, PHL_NewRGB(119, 166, 219)); + if (hasWeapon[i] == 1) { + cx = (i + 1) * 40; + }else{ + cx = 0; + } + PHL_DrawSurfacePart(20 + (48 * i), 36, cx, 0, 40, 40, images[imgItems]); + } + //Items + int count = 0; + int imageOrder[28] = { 13, 17, 16, 15, 8, 10, 9, + 18, 4, 6, 5, 7, 3, 11, + 14, 12, 1, 2, 22, 26, 27, + 21, 25, 28, 23, 20, 19, 24 }; + + for (i = 0; i < 4; i++) { + for (a = 0; a < 7; a++) { + PHL_DrawRect(18 + (48 * a), 114 + (48 * i), 44, 44, PHL_NewRGB(255, 255, 255)); + PHL_DrawRect(20 + (48 * a), 116 + (48 * i), 40, 40, PHL_NewRGB(119, 166, 219)); + + if (hasItem[count] == 0) { + cx = 0; + cy = 0; + }else{ + cy = 0; + cx = (imageOrder[count] + 6) * 40; + while (cx >= 640) { + cy += 40; + cx -= 640; + } + } + + PHL_DrawSurfacePart(20 + (48 * a), 116 + (48 * i), cx, cy, 40, 40, images[imgItems]); + count++; + } + } + //Keys + for (i = 0; i < 8; i++) { + PHL_DrawRect(18 + (48 * i), 338, 44, 44, PHL_NewRGB(255, 255, 255)); + PHL_DrawRect(20 + (48 * i), 340, 40, 40, PHL_NewRGB(119, 166, 219)); + if (hasKey[i] == 1) { + cx = 120 + (i * 40); + cy = 80; + }else{ + cx = 0; + cy = 0; + } + PHL_DrawSurfacePart(20 + (48 * i), 340, cx, cy, 40, 40, images[imgItems]); + } + + //Text box + PHL_DrawRect(16, 400, 606, 46, PHL_NewRGB(255, 255, 255)); + PHL_DrawRect(18, 402, 602, 42, PHL_NewRGB(0, 0, 255)); + + //Text + if (hasItem[cursorX + (cursorY * 7)] == 1) { + int drawX = 32, drawY = 402; + + //Draw item name + drawX = drawText(itemName[cursorX + (cursorY * 7) + 5], drawX, drawY); + //Draw item description + drawX = drawCharacter(6, 0, drawX, drawY); + drawText(itemDescription[cursorX + (cursorY * 7)], drawX, drawY); + } + + //Cursor + PHL_DrawSurfacePart(16 + (cursorX * 48), 112 + (cursorY * 48), 0, 96, 48, 48, images[imgHud]); +} \ No newline at end of file diff --git a/src/inventory.h b/src/inventory.h new file mode 100644 index 0000000..548b951 --- /dev/null +++ b/src/inventory.h @@ -0,0 +1,9 @@ +#ifndef INVENTORY_H +#define INVENTORY_H + +void inventory(); + +int inventoryStep(); +void inventoryDraw(); + +#endif \ No newline at end of file diff --git a/src/main.c b/src/main.c new file mode 100644 index 0000000..54f61e2 --- /dev/null +++ b/src/main.c @@ -0,0 +1,57 @@ +#include "PHL.h" +#include "game.h" +#include +#include +#include + +void createSaveLocations() +{ + //Force create save data folders + #ifdef _3DS + //3DS builds + mkdir("sdmc:/3ds", 0777); + mkdir("sdmc:/3ds/appdata", 0777); + mkdir("sdmc:/3ds/appdata/HydraCastleLabyrinth", 0777); + mkdir("sdmc:/3ds/appdata/HydraCastleLabyrinth/data", 0777); + mkdir("sdmc:/3ds/appdata/HydraCastleLabyrinth/map", 0777); + #elif defined(_SDL) + char buff[4096]; + strcpy(buff, getenv("HOME")); + strcat(buff, "/.hydracastlelabyrinth"); + // if exist first? + struct stat sb; + if(!(stat(buff, &sb)==0 && S_ISDIR(sb.st_mode))) + mkdir(buff, 0777); + + #else + //psp, wii + mkdir("/data", 0777); + mkdir("/map", 0777); + #endif +} + + +int main(int argc, char **argv) +{ + //Setup + #ifdef _3DS + sdmcInit(); + osSetSpeedupEnable(false); + #endif + + srand(time(NULL)); + createSaveLocations(); + + game(); + + //System specific cleanup + #ifdef _PSP + sceKernelExitGame(); + #endif + + #ifdef _3DS + sdmcExit(); + #endif + + return 0; +} \ No newline at end of file diff --git a/src/object.c b/src/object.c new file mode 100644 index 0000000..1e3cd3b --- /dev/null +++ b/src/object.c @@ -0,0 +1,1525 @@ +#include "object.h" +#include "game.h" +#include "hero.h" +#include +#include +#include "enemies/slug.h" +#include "game.h" +#include + +void nullFunction(void* v); + +void ammoStep(Ammo* a); +void ammoDraw(Ammo* a); + +void destroyableStep(Destroyable* d); + +void secretTriggerStep(SecretTrigger* s); + +void chestStep(Chest* c); +void chestDraw(Chest* c); + +void savePointStep(SavePoint* s); +void savePointDraw(SavePoint* s); + +void doorStep(Door* d); +void doorDraw(Door* d); + +void lockBlockStep(LockBlock* l); +void lockBlockDraw(LockBlock* l); + +void switchStep(Switch* s); +int switchActivate(Switch* s); +void switchResult(Switch* s); +void switchDraw(Switch* s); + +void gateStep(Gate* g); +void gateDraw(Gate* g); + +void statueStep(Statue* s); +void statueDraw(Statue* s); + +void buttonStep(FloorPad* f); +void buttonDraw(FloorPad* f); + +void ladderStep(Ladder* l); +void ladderActivate(int x, int y); + +void generatorStep(Generator* g); +void generatorDraw(Generator* g); + +void shockgateStep(Shockgate* s); +void shockgateDraw(Shockgate* s); + +void crownStep(Crown* c); +void crownDraw(Crown* c); + +void nullFunction(void* v) +{ + //Wow, it's literally nothing! +} + +void objectDestroy(int id) +{ + if (objects[id] != NULL) { + if (objects[id]->data != NULL) { + free(objects[id]->data); + } + objects[id]->data = NULL; + + free(objects[id]); + } + objects[id] = NULL; +} + +//Ammo/Health +void spawnCollectable(int x, int y) +{ + int num = (rand() % 100) + 1; + int result = -1; + + int heartchance = 15; + int ammochance = 10; + + if (hasItem[3] == 1) { //Has golden seed + heartchance *= 2; + ammochance *= 2; + } + + if (num <= heartchance) { + result = 1; //Heart + } + else if (num > heartchance && num <= heartchance + ammochance) { + result = 0; //Ammo + } + + //result = rand() % 2; + + if (result != -1) { + createAmmo(x, y, result); + } +} + +void createAmmo(int x, int y, int type) +{ + int i; + for (i = 0; i < MAX_OBJECTS; i++) { + if (objects[i] == NULL) { + Object* o = malloc(sizeof *o); + Ammo* a = malloc(sizeof *a); + a->id = i; + + a->x = x; + a->y = y; + a->type = type; + + a->vsp = -2.5; + a->grav = 0.2; + a->blink = 30; + a->canLand = 0; + a->bounce = 0; + + o->data = a; + o->objectStep = ammoStep; + o->objectDraw = ammoDraw; + o->type = -1; + + objects[i] = o; + i = MAX_OBJECTS; + } + } +} + +void ammoStep(Ammo* a) +{ + char dead = 0; + + //Flashing animation + { + if (a->blink > 0) { + a->blink -= 1; + } + } + + //Setup Mask + Mask mask; + { + mask.circle = mask.unused = 0; + mask.w = 2; + mask.h = 1; + mask.x = a->x - (mask.w / 2); + mask.y = a->y + (40 - mask.h); + } + + //Movement + { + a->y += a->vsp; + a->vsp += a->grav; + + mask.y = a->y + (40 - mask.h); + } + + //Destroy if it falls in a pit + { + if (a->y > 480) { + dead = 1; + } + } + + //Falling + if (a->vsp >= 0) { + //Inside of a block + if (a->canLand == 0) { + if (checkTileCollision(1, mask) == 0 && checkTileCollision(3, mask) == 0) { + a->canLand = 1; + } + } + + //Land on ground + if (a->canLand == 1) { + PHL_Rect collide = getTileCollision(1, mask); + if (collide.x == -1) { + collide = getTileCollision(3, mask); + } + if (collide.x != -1) { + + a->y = collide.y - 40; + a->vsp = 0; + + //Bounce + if (a->bounce <= 2) { + double bounceVsp[3] = {-2, -1, 0}; + + if (a->bounce > 2) { + a->bounce = 2; + } + + a->vsp = bounceVsp[a->bounce]; + a->bounce += 1; + PHL_PlaySound(sounds[sndPi02], 2); + } + } + } + }else{ + a->canLand = 0; + } + + //Setup hero collision mask + { + mask.w = 20; + mask.h = 32; + //Heart + if (a->type == 1) { + mask.w = 28; + mask.h = 26; + } + mask.x = a->x - (mask.w / 2); + mask.y = a->y + (40 - mask.h); + } + + //Collect + { + if (a->blink <= 0 && checkCollision(mask, heroMask)) { + //Ammo + if (a->type == 0) { + heroAmmo += 5; + if (heroAmmo > maxAmmo) { + heroAmmo = maxAmmo; + } + } + //Heart + else if (a->type == 1) { + herohp += 10; + if (herohp > 128) { + herohp = 128; + } + } + + dead = 1; + PHL_PlaySound(sounds[sndGet02], 2); + } + } + + //Destroy object + { + if (dead == 1) { + objectDestroy(a->id); + } + } + +} + +void ammoDraw(Ammo* a) +{ + if (a->blink % 2 == 0) { + int cropX[2] = {40, 0}; + + PHL_DrawSurfacePart(a->x - 20, a->y + 2, cropX[a->type], 120, 40, 40, images[imgMisc20]); + } +} + +//Destroyable Block +void createDestroyable(int x, int y, int secret) +{ + int i; + for (i = 0; i < MAX_OBJECTS; i++) { + if (objects[i] == NULL) { + Object* o = malloc(sizeof *o); + Destroyable* d = malloc(sizeof *d); + d->id = i; + + d->x = x; + d->y = y; + d->secret = secret; + + d->hp = 3; + //d->invulnerable = 0; + + d->mask.x = x; + d->mask.y = y; + d->mask.w = d->mask.h = 40; + d->mask.unused = d->mask.circle = 0; + + o->data = d; + o->objectStep = destroyableStep; + o->objectDraw = nullFunction; + o->type = 3; + + objects[i] = o; + i = MAX_OBJECTS; + } + } +} + +void destroyableStep(Destroyable* d) +{ + //if (d->invulnerable <= 0) { + int i; + for (i = 0; i < MAX_WEAPONS; i++) { + if (weapons[i] != NULL) { + if (weapons[i]->cooldown <= 0) { + if (checkCollision(d->mask, weapons[i]->weaponMask)) { + if (hasItem[0] == 1) { //Has copper pick + d->hp -= 1; + if (hasItem[1] == 0) { + PHL_PlaySound(sounds[sndHit03], CHN_WEAPONS); + }else{ //Has silver pick + d->hp -= 2; + } + } + + //d->invulnerable = 15; + weaponHit(weapons[i]); + + if (d->hp <= 0) { + createRockSmash(d->x + 20, d->y + 20); + int sx = d->x / 40; + int sy = d->y / 40; + foreground.tileX[sx][sy] = 0; + foreground.tileY[sx][sy] = 0; + collisionTiles[sx][sy] = 0; + PHL_UpdateBackground(background, foreground); + + if (d->secret == 0) { + spawnCollectable(d->x + 20, d->y); + }else if (d->secret == 1) { + roomSecret = 1; + } + + objectDestroy(d->id); + + } + + if (hasItem[0] == 0) { + PHL_PlaySound(sounds[sndHit03], 1); + } + } + } + } + } + /*}else{ + d->invulnerable -= 1; + } + */ +} + +//Secret Trigger +void createSecretTrigger(int type, int enemyType, int flag) +{ + if (flag != 0 && flags[flag] == 1) { + roomSecret = 1; + }else{ + + int i; + for (i = 0; i < MAX_OBJECTS; i++) { + if (objects[i] == NULL) { + Object* o = malloc(sizeof *o); + SecretTrigger* s = malloc(sizeof *s); + s->id = i; + s->flag = flag; + + s->type = type; + s->enemyType = enemyType; + + o->data = s; + o->objectStep = secretTriggerStep; + o->objectDraw = nullFunction; + o->type = -1; + + objects[i] = o; + i = MAX_OBJECTS; + } + } + } +} + +void secretTriggerStep(SecretTrigger* s) +{ + int i, result = 1; + for (i = 0; i < MAX_ENEMIES; i++) { + if (s->type == 0) { //Destroy all enemies + if (enemies[i] != NULL && enemies[i]->type != -1) { + i = MAX_ENEMIES; + result = 0; + } + } + else if (s->type == 1) { //Destroy one type of enemy + if (enemies[i] != NULL && enemies[i]->type == s->enemyType) { + i = MAX_ENEMIES; + result = 0; + } + } + else{ //No trigger, only activate on creation + result = 0; + } + } + + if (result == 1) { + if (s->flag != 0) { + flags[s->flag] = 1; + } + roomSecret = 1; + objectDestroy(s->id); + } +} + +//Chest +void createChest(int x, int y, int item, int secret) +{ + //Don't create if the player already has the item + int dospawn = 1; + if (item <= 4) { + if (hasWeapon[item] == 1) { dospawn = 0; } + } + else if (item <= 32) { + if (hasItem[item - 5] == 1) { dospawn = 0; } + } + else if (item <= 40) { + if (hasKey[item - 33] == 1) { dospawn = 0; } + } + + if (dospawn == 1) { + int i; + for (i = 0; i < MAX_OBJECTS; i++) { + if (objects[i] == NULL) { + Object* o = malloc(sizeof *o); + Chest* c = malloc(sizeof *c); + c->id = i; + + c->x = x; + c->y = y; + c->item = item; + c->secret = secret; + c->timer = 10; + + c->visible = 1; + if (secret == 1 && roomSecret == 0) { //Assume secret trigger is loaded before chest + c->visible = 0; + if (hasItem[2] == 1) { //Has Bell + PHL_PlaySound(sounds[sndBell01], CHN_SOUND); + //bellFlag = 1; + } + } + + c->mask.x = x; + c->mask.y = y; + c->mask.w = c->mask.h = 40; + c->mask.unused = c->mask.circle = 0; + + o->data = c; + o->objectStep = chestStep; + o->objectDraw = chestDraw; + o->type = 4; + + objects[i] = o; + i = MAX_OBJECTS; + } + } + } +} + +void chestStep(Chest* c) +{ + if (c->visible == 1) { + c->timer -= 1; + if (c->timer <= 0) { + createEffectExtra(5, c->x + (rand() % 40) + 1, c->y + 4 + (rand() % 40) + 1, 0, 0, 0); + c->timer = 12; + } + + if (btnUp.pressed == 1) { + if (getHeroOnground() == 1 && checkCollisionXY(c->mask, herox, heroy + 20) == 1) { + PHL_PlaySound(sounds[sndGet02], CHN_HERO); + if (c->item <= 4) { + hasWeapon[c->item] = 1; + itemGotX = 40 + (c->item * 40); + itemGotY = 0; + } + else if (c->item <= 32) { + hasItem[c->item - 5] = 1; + int itemorder[28] = { 12, 16, 15, 14, 7, 9, 8, + 17, 3, 5, 4, 6, 2, 10, + 13, 11, 0, 1, 21, 25, 26, + 20, 24, 27, 22, 19, 18, 23 }; + itemGotX = 280 + (itemorder[c->item - 5] * 40); + itemGotY = 0; + while (itemGotX >= 640) { + itemGotX -= 640; + itemGotY += 40; + } + } + else if (c->item <= 40) { + hasKey[c->item - 33] = 1; + itemGotX = 120 + ((c->item - 33) * 40); + itemGotY = 80; + } + + //Fix no 2nd jump immediatly after getting boots + if (c->item == 17) { + setHeroCanjump(1); + } + + //setHeroState(6); //Set hero state to GETITEM + int saveItem = c->item; + + objectDestroy(c->id); + + getItem(saveItem); + } + } + } + else { + if (roomSecret == 1) { + c->visible = 1; + playSecret(); + } + //Visible if the boss is already defeated + if (bossDefeatedFlag == 1) { + c->visible = 1; + } + } +} + +void chestDraw(Chest* c) +{ + if (c->visible == 1) { + int dx = 520, dy = 0; + + if (c->item > 32) { + dx = 240; + dy = 120; + } + + PHL_DrawSurfacePart(c->x, c->y, dx, dy, 40, 40, images[imgMisc20]); + } +} + +//Save point +void createSavePoint(int x, int y, int hidden) +{ + if (hidden == 0 || hasKey[7] == 1) { + int i; + for (i = 0; i < MAX_OBJECTS; i++) { + if (objects[i] == NULL) { + Object* o = malloc(sizeof *o); + SavePoint* s = malloc(sizeof *s); + s->id = i; + + s->x = x; + s->y = y; + + s->imageIndex = 0; + + s->mask.x = x + 6; + s->mask.y = y; + s->mask.w = 28; + s->mask.h = 40; + s->mask.unused = s->mask.circle = 0; + + o->data = s; + o->objectStep = savePointStep; + o->objectDraw = savePointDraw; + o->type = -1; + + objects[i] = o; + i = MAX_OBJECTS; + } + } + } +} + +void savePointStep(SavePoint* s) +{ + s->imageIndex += 0.15; + if (s->imageIndex >= 4) { + s->imageIndex -= 4; + } + + if (btnUp.pressed == 1 && getHeroOnground() == 1) { + if (checkCollisionXY(s->mask, herox, heroy)) { + saveScreen(); + } + } +} + +void savePointDraw(SavePoint* s) +{ + PHL_DrawSurfacePart(s->x, s->y, (int)s->imageIndex * 40, 320, 40, 40, images[imgMisc20]); +} + +//Door +unsigned char unlockedDoor[8] = {0, 2, 6, 7, 32, 22, 39, 48}; + +void createDoor(int x, int y, int level, int coords, int warpx, int warpy, int secret) +{ + if (level != 8 || hasKey[7] == 1) { + int i; + for (i = 0; i < MAX_OBJECTS; i++) { + if (objects[i] == NULL) { + Object* o = malloc(sizeof *o); + Door* d = malloc(sizeof *d); + d->id = i; + + d->x = x; + d->y = y; + + d->visible = 1; + d->secret = secret; + if (d->secret == 1) { + d->visible = 0; + } + + d->open = 0; + if (level == 0) { + d->open = 1; + }else{ + d->open = flags[unlockedDoor[level-1]]; + } + + d->warplevel = level; + d->warpcoords = coords; + d->warpx = warpx; + d->warpy = warpy; + + + d->mask.x = x + 6; + d->mask.y = y; + d->mask.w = 28; + d->mask.h = 40; + d->mask.unused = d->mask.circle = 0; + + o->data = d; + o->objectStep = doorStep; + o->objectDraw = doorDraw; + o->type = -1; + + objects[i] = o; + i = MAX_OBJECTS; + } + } + } +} + +void doorStep(Door* d) +{ + if (d->visible == 1) { + if (btnUp.pressed == 1 && getHeroOnground() == 1 && getHeroState() != 6) { + if (checkCollisionXY(d->mask, herox, heroy)) { + if (d->open == 0) { + if (hasKey[d->warplevel - 1] == 1) { + d->open = 1; + flags[unlockedDoor[d->warplevel - 1]] = 1; + //doorUnlocked[d->warplevel-1] = 1; + PHL_PlaySound(sounds[sndDoor00], CHN_SOUND); + } + }else{ + //Setup Door event + herox = d->x + 20; + heroy = d->y; + + lastDoor = d; + setHeroState(7); //Set state to DOOR + PHL_PlaySound(sounds[sndStep01], CHN_HERO); + } + } + } + }else{ + if (bossDefeatedFlag == 1 && hasKey[getLevel()]) { + d->visible = 1; + } + //Display key and door if the boss didn't show up + else if (d->secret == 1 && bossFlag == 0) { + bossFlag = 1; + bossDefeatedFlag = 1; + PHL_StopMusic(); + } + } +} + +void doorDraw(Door* d) +{ + if (d->visible == 1) { + PHL_DrawSurfacePart(d->x, d->y, 600 - (40 * d->open), 0, 40, 40, images[imgMisc20]); + } +} + +//Lock Block +void createLockBlock(int x, int y, int flag) +{ + if (flags[flag] == 0) { + + int i; + for (i = 0; i < MAX_OBJECTS; i++) { + if (objects[i] == NULL) { + Object* o = malloc(sizeof *o); + LockBlock* l = malloc(sizeof *l); + + l->id = i; + l->x = x; + l->y = y; + + l->invincible = 0; + + int tx = x / 40; + int ty = y / 40; + l->tile = collisionTiles[tx][ty]; + collisionTiles[tx][ty] = 1; + + l->flag = flag; + + o->data = l; + o->objectStep = lockBlockStep; + o->objectDraw = lockBlockDraw; + o->type = -1; + + objects[i] = o; + i = MAX_OBJECTS; + } + } + + } +} + +void lockBlockStep(LockBlock* l) +{ + //Collide with weapons + if (l->invincible <= 0) { + Mask mask; + mask.circle = mask.unused = 0; + mask.x = l->x; + mask.y = l->y; + mask.w = mask.h = 40; + + int i; + for (i = 0; i < MAX_WEAPONS; i++) { + if (weapons[i] != NULL) { + if (checkCollision(mask, weapons[i]->weaponMask)) { + l->invincible = 15; + //Sound + weaponHit(weapons[i]); + PHL_PlaySound(sounds[sndHit03], CHN_WEAPONS); + + i = MAX_WEAPONS; + } + } + } + }else{ + l->invincible -= 1; + } + + if (roomSecret == 1) { + playSecret(); + + int tx = l->x / 40; + int ty = l->y / 40; + collisionTiles[tx][ty] = l->tile; + flags[l->flag] = 1; + //Destroy + objectDestroy(l->id); + /* + free(objects[l->id]); + objects[l->id] = NULL; + */ + } +} + +void lockBlockDraw(LockBlock* l) +{ + PHL_DrawSurfacePart(l->x, l->y, 120, 400, 40, 40, images[imgMisc20]); +} + + +//Green light switch +void createSwitch(int x, int y, int flag) +{ + int i; + for (i = 0; i < MAX_OBJECTS; i++) { + if (objects[i] == NULL) { + Object* o = malloc(sizeof *o); + Switch* s = malloc(sizeof *s); + + s->id = i; + s->x = x; + s->y = y; + + s->imageIndex = 0; + + s->flag = flag; + s->activated = 0; + + if (flags[s->flag] == 1) { + switchResult(s); + s->activated = 1; + } + + o->data = s; + o->objectStep = switchStep; + o->objectDraw = switchDraw; + o->type = 76; + + objects[i] = o; + i = MAX_OBJECTS; + } + } +} + +void switchStep(Switch* s) +{ + if (s->activated == 0) { + if (btnUp.pressed == 1) { + Mask mask; + mask.unused = mask.circle = 0; + mask.w = 16; + mask.h = 30; + mask.x = s->x + 12; + mask.y = s->y + 10; + + if (checkCollision(mask, getHeroMask())) { + if (switchActivate(s) == 1) { + PHL_PlaySound(sounds[sndPi02], CHN_SOUND); + playSecret(); + } + } + } + }else{ + s->imageIndex += 0.2; + if (s->imageIndex >= 3) { + s->imageIndex -= 2; + } + } +} + +int switchActivate(Switch* s) +{ + int success = 0; + + if ((s->flag == 24 && hasItem[23] == 1) || //Switch in level 2 + (s->flag == 26 && hasItem[24] == 1)) { //Switch in level 6 + success = 1; + } + + //Switches in level 7 + if ((s->flag == 44 && hasItem[25] == 1) || //Left Switch + (s->flag == 43 && hasItem[26] == 1)) + { + PHL_PlaySound(sounds[sndPi02], CHN_SOUND); + s->activated = 1; + flags[s->flag] = 1; + + if (flags[44] == 1 && flags[43] == 1) { + flags[45] = 1; + roomSecret = 1; + success = 1; + } + } + + if (success == 1) { + switchResult(s); + s->activated = 1; + flags[s->flag] = 1; + } + + return success; +} + +void switchResult(Switch* s) +{ + if (s->flag == 24) { //Switch in level 2 + createPlatform(0, 320, 240, 320, 360, 1, 0); + flags[23] = 1; + } + + if (s->flag == 26) { //Switch in level 6 + roomSecret = 1; + } +} + +void switchDraw(Switch* s) +{ + PHL_DrawSurfacePart(s->x, s->y, 240 + (40 * (int)s->imageIndex), 560, 40, 40, images[imgMisc20]); +} + +//Gates +void createGate(int x, int y, int col) +{ + int i; + for (i = 0; i < MAX_OBJECTS; i++) { + if (objects[i] == NULL) { + Object* o = malloc(sizeof *o); + Gate* g = malloc(sizeof *g); + g->id = i; + + g->x = x; + g->y = y; + + g->col = col; + + g->imageIndex = 0; + g->timer = 0; + g->open = 0; + + //g->invincible = 0; + + o->data = g; + o->objectStep = gateStep; + o->objectDraw = gateDraw; + o->type = 53; + + objects[i] = o; + i = MAX_OBJECTS; + } + } +} + +void gateStep(Gate* g) +{ + //Animate + { + if (g->open == 1) { + if (g->imageIndex < 4) { + g->imageIndex += 0.1; + } + } + } + + //Not (fully) opened + if (g->imageIndex < 4) + { + //Setup Mask + Mask mask; + { + mask.unused = mask.circle = 0; + mask.x = g->x + 11; + mask.y = g->y; + mask.w = 18; + mask.h = 40; + } + + //Collide with player + { + if (checkCollision(mask, getHeroMask())) { + if (getHeroHsp() < 0) { + herox = mask.x + mask.w + (getHeroMask().w / 2); + } + else if (getHeroHsp() > 0) { + herox = mask.x - (getHeroMask().w / 2); + } + } + } + + //Collide with weapons + { + if (g->open == 0) { + int i; + for (i = 0; i < MAX_WEAPONS; i++) { + if (weapons[i] != NULL) { + if (weapons[i]->cooldown == 0) { + if (checkCollision(mask, weapons[i]->weaponMask)) { + PHL_PlaySound(sounds[sndHit03], CHN_WEAPONS); + + //Is a sword + if (weapons[i]->type == 5) { + if ((g->col == 0 && hasItem[20] == 1) || (g->col == 1 && hasItem[19] == 1)) { + g->open = 1; + PHL_PlaySound(sounds[sndDoor00], CHN_SOUND); + } + } + + weaponHit(weapons[i]); + + i = MAX_WEAPONS; + } + } + } + } + } + } + + } +} + +void gateDraw(Gate* g) +{ + int cx = (int)g->imageIndex * 40, + cy = 520; + + if (g->col == 0) { //Red Gate + cx += 200; + } + + PHL_DrawSurfacePart(g->x, g->y, cx, cy, 40, 40, images[imgMisc20]); +} + +//Statue +void createStatue(int x, int y, int type) +{ + int i; + for (i = 0; i < MAX_OBJECTS; i++) { + if (objects[i] == NULL) { + Object* o = malloc(sizeof *o); + Statue* s = malloc(sizeof *s); + s->id = i; + + s->x = x; + s->y = y; + + s->type = type; + + s->invincible = 0; + s->hp = 3; + + o->data = s; + o->objectStep = statueStep; + o->objectDraw = statueDraw; + o->type = 54; + + objects[i] = o; + i = MAX_OBJECTS; + } + } +} + +void statueStep(Statue* s) +{ + Mask mask; + mask.unused = mask.circle = 0; + mask.w = 40; + mask.h = 40; + mask.x = s->x + ((40 - mask.w) / 2); + mask.y = s->y; + + //Collide with hero + if (checkCollision(mask, getHeroMask()) == 1) { + if (getHeroHsp() < 0) { + herox = mask.x + mask.w + (getHeroMask().w / 2) + 1; + } + if (getHeroHsp() > 0) { + herox = mask.x - (getHeroMask().w / 2); + } + } + + //Collide with weapons + if (s->invincible <= 0) { + int i; + for (i = 0; i < MAX_WEAPONS; i++) { + if (weapons[i] != NULL) { + if (checkCollision(mask, weapons[i]->weaponMask)) { + s->invincible = 15; + //Sound + weaponHit(weapons[i]); + + //Break if you have the right item + if ((s->type == 0 && hasItem[22] == 1) || (s->type == 1 && hasItem[21] == 1)) { + s->hp -= 1; + + if (s->hp <= 0) { + createRockSmash(s->x + 20, s->y + 20); + objectDestroy(s->id); + } + }else{ + PHL_PlaySound(sounds[sndHit03], CHN_WEAPONS); + } + + i = MAX_WEAPONS; + } + } + } + }else{ + s->invincible -= 1; + } +} + +void statueDraw(Statue* s) +{ + int cx = 200, + cy = 400; + + if (s->type == 1) { + cx += 200; + } + + PHL_DrawSurfacePart(s->x, s->y, cx, cy, 40, 40, images[imgMisc20]); +} + +//Button +void createFloorPad(int x, int y, int flag) +{ + int i; + for (i = 0; i < MAX_OBJECTS; i++) { + char found = 0; + + if (objects[i] == NULL) { + Object* o = malloc(sizeof *o); + FloorPad* f = malloc(sizeof *f); + f->id = i; + + f->x = x; + f->y = y; + + f->pressed = 0; + f->flag = flag; + + if (flag != 0 && flags[flag] == 1) { + roomSecret = 1; + } + + o->data = f; + o->objectStep = buttonStep; + o->objectDraw = buttonDraw; + o->type = 77; + + objects[i] = o; + i = MAX_OBJECTS; + found = 1; + } + + if (found == 1) { + //Create a breakable block if it's covered by a solid block + int roundx = x / 40, + roundy = y / 40; + + if (collisionTiles[roundx][roundy] == 1) { + createDestroyable(x, y, 2); + } + } + } +} + +void buttonStep(FloorPad* f) +{ + if (f->pressed == 0) { + if (getHeroVsp() > 0) { + Mask mask; + mask.unused = mask.circle = 0; + mask.w = 16; + mask.h = 10; + mask.x = f->x + ((40 - mask.w) /2); + mask.y = f->y + (40 - mask.h); + + if (checkCollision(mask, getHeroMask()) == 1) { + f->pressed = 1; + PHL_PlaySound(sounds[sndHit02], CHN_SOUND); + + //Check if there are other buttons unpressed + int found = 0; + int i; + for (i = 0; i < MAX_OBJECTS; i++) { + if (objects[i] != NULL) { + if (objects[i]->type == 77) { + FloorPad* btemp = objects[i]->data; + if (btemp->pressed == 0) { + found = 1; + i = MAX_OBJECTS; + } + } + } + } + + //Activate flag + if (found == 0) { + roomSecret = 1; + + if (f->flag != 0 && flags[f->flag] == 0) { + flags[f->flag] = 1; + } + } + } + + } + } +} + +void buttonDraw(FloorPad* f) +{ + char covered = 0; + + int roundx = f->x / 40, + roundy = f->y / 40; + + //Covered by breakable block + if (collisionTiles[roundx][roundy] == 1) { + covered = 1; + } + + if (covered == 0) { + PHL_DrawSurfacePart(f->x, f->y, 160 + (f->pressed * 40), 320, 40, 40, images[imgMisc20]); + } +} + +//Ladder +void createLadder(int x, int y, int flag) +{ + if (flags[flag] == 1) { + ladderActivate(x, y); + }else{ + int i; + for (i = 0; i < MAX_OBJECTS; i++) { + if (objects[i] == NULL) { + Object* o = malloc(sizeof *o); + Ladder* l = malloc(sizeof *l); + l->id = i; + + l->x = x; + l->y = y; + + l->flag = flag; + + if (getLevel() != 6) { + if (hasItem[2] == 1) { //Has Bell + PHL_PlaySound(sounds[sndBell01], CHN_SOUND); + } + } + + o->data = l; + o->objectStep = ladderStep; + o->objectDraw = nullFunction; + o->type = 79; + + objects[i] = o; + i = MAX_OBJECTS; + } + } + } +} + +void ladderStep(Ladder* l) +{ + if (roomSecret == 1) { + playSecret(); + flags[l->flag] = 1; + ladderActivate(l->x, l->y); + objectDestroy(l->id); + } +} + +void ladderActivate(int x, int y) +{ + y = (int)(y / 40); + x = (int)(x / 40); + + while (y >= 0) { + foreground.tileX[x][y] = 3; + foreground.tileY[x][y] = 0; + + collisionTiles[x][y] = 2; + y -= 1; + } + + PHL_UpdateBackground(background, foreground); +} + +//Generator +void createGenerator(int x, int y, int flag) +{ + if (flags[flag] == 0) { + int i; + for (i = 0; i < MAX_OBJECTS; i++) { + if (objects[i] == NULL) { + Object* o = malloc(sizeof *o); + Generator* g = malloc(sizeof *g); + g->id = i; + g->hp = 3; + g->blink = 0; + + g->x = x; + g->y = y; + + g->imageIndex = 0; + + g->flag = flag; + + o->data = g; + o->objectStep = generatorStep; + o->objectDraw = generatorDraw; + o->type = 80; + + objects[i] = o; + i = MAX_OBJECTS; + } + } + } + +} + +void generatorStep(Generator* g) +{ + //Animate + { + g->imageIndex += 0.33; + if (g->imageIndex >= 2) { + g->imageIndex -= 2; + } + + if (g->blink > 0) { + g->blink -= 1; + } + } + + //Setup Mask + Mask mask; + { + mask.circle = mask.unused = 0; + mask.w = 24; + mask.h = 40; + mask.x = g->x + ((40 - mask.w) / 2); + mask.y = g->y; + } + + //Weapon collision + { + int i; + for (i = 0; i < MAX_WEAPONS; i++) { + if (weapons[i] != NULL) { + if (weapons[i]->cooldown == 0) { + if (checkCollision(mask, weapons[i]->weaponMask)) { + weaponHit(weapons[i]); + + g->hp -= 1; + g->blink = 15; + + i = MAX_WEAPONS; + } + } + } + } + } + + //Destroy + { + if (g->hp <= 0) { + createRockSmash(g->x + 20, g->y + 20); + flags[g->flag] = 1; + //if no generators exist + { + char found = 0; + + int i; + for (i = 0; i < MAX_OBJECTS; i++) { + if (i != g->id && objects[i] != NULL) { + if (objects[i]->type == 80) { + found = 1; + i = MAX_OBJECTS; + } + } + } + + if (found == 0) { + roomSecret = 1; + playSecret(); + } + } + objectDestroy(g->id); + } + } + +} + +void generatorDraw(Generator* g) +{ + if (g->blink % 2 == 0) { + PHL_DrawSurfacePart(g->x, g->y, 80 + ((int)g->imageIndex * 40), 600, 40, 40, images[imgMisc20]); + } +} + +//Electric gate +void createShockgate(int x, int y, int flag) +{ + if (flags[flag] == 0) { + int i; + for (i = 0; i < MAX_OBJECTS; i++) { + if (objects[i] == NULL) { + Object* o = malloc(sizeof *o); + Shockgate* s = malloc(sizeof *s); + s->id = i; + + s->x = x; + s->y = y; + + s->imageIndex = 0; + + s->flag = flag; + + o->data = s; + o->objectStep = shockgateStep; + o->objectDraw = shockgateDraw; + o->type = 56; + + objects[i] = o; + i = MAX_OBJECTS; + } + } + } + +} + +void shockgateStep(Shockgate* s) +{ + //Animate + { + s->imageIndex += 0.33; + if (s->imageIndex >= 4) { + s->imageIndex -= 4; + } + } + + //Setup Mask + Mask mask; + { + mask.circle = mask.unused = 0; + mask.w = 40 * 3; + mask.h = 8; + mask.x = s->x; + mask.y = s->y + ((40 - mask.h) / 2); + } + + //Hero Collision + { + if (checkCollision(mask, getHeroMask()) == 1) { + heroy = mask.y - 40; + setHeroVsp(0); + } + } + + //Destroy + if (roomSecret == 1) { + flags[s->flag] = 1; + objectDestroy(s->id); + } +} + +void shockgateDraw(Shockgate* s) +{ + int i; + for (i = 0; i < 3; i++) { + PHL_DrawSurfacePart(s->x + (40 * i), s->y, 80 + ((int)s->imageIndex * 40), 560, 40, 40, images[imgMisc20]); + } +} + +//Crown +void createCrown(int x, int y) +{ + int i; + for (i = 0; i < MAX_OBJECTS; i++) { + if (objects[i] == NULL) { + Object* o = malloc(sizeof *o); + Crown* c = malloc(sizeof *c); + c->id = i; + + c->x = x; + c->y = y; + + c->ystart = c->y; + c->bobRot = 0; + + c->imageIndex = 0; + + c->visible = 0; + c->timer = 0; + + o->data = c; + o->objectStep = crownStep; + o->objectDraw = crownDraw; + o->type = 57; + + objects[i] = o; + i = MAX_OBJECTS; + } + } +} + +void crownStep(Crown* c) +{ + if (c->visible == 0) { + if (roomSecret == 1) { + c->visible = 1; + playSecret(); + } + }else{ + //Animate + { + c->imageIndex += 0.33; + if (c->imageIndex >= 2) { + c->imageIndex -= 2; + } + + //Movement + c->bobRot += 6; + if (c->bobRot >= 360) { + c->bobRot -= 360; + } + c->y = c->ystart + sin(c->bobRot * 3.14159 / 180) * 5; + } + + //Sparkle + c->timer -= 1; + if (c->timer <= 0) { + createEffectExtra(5, c->x + (rand() % 40) + 1, c->y + 4 + (rand() % 40) + 1, 0, 0, 0); + c->timer = 12; + } + + //Setup Mask + Mask mask; + { + mask.circle = mask.unused = 0; + mask.w = 15; + mask.h = 11; + mask.x = c->x + 20 - (mask.w / 2); + mask.y = c->y + 20 - (mask.h / 2); + } + + if (checkCollision(mask, getHeroMask()) == 1) { + objectDestroy(c->id); + gameEnding(); + } + + } +} + +void crownDraw(Crown* c) +{ + if (c->visible == 1) { + int cropX = (int)c->imageIndex * 40; + + PHL_DrawSurfacePart(c->x, c->y, cropX, 200, 40, 40, images[imgHero]); + } +} \ No newline at end of file diff --git a/src/object.h b/src/object.h new file mode 100644 index 0000000..8da299a --- /dev/null +++ b/src/object.h @@ -0,0 +1,181 @@ +#ifndef OBJECT_H +#define OBJECT_H + +#include "collision.h" + +typedef struct { + void* data; //Specific object struct + void (*objectStep)(); + void (*objectDraw)(); + int type; +} Object; + +void objectDestroy(int id); + +//Health/Ammo collectables +typedef struct { + int id, type; //0 for ammo, 1 for heart + double x, y, + vsp, grav; + + int blink, canLand, bounce; +} Ammo; + +void spawnCollectable(int x, int y); +void createAmmo(int x, int y, int type); + +//Destroyable blocks +typedef struct { + int id; + int x, y; + int hp/*, invulnerable*/; + int secret; + Mask mask; +} Destroyable; + +void createDestroyable(int x, int y, int secret); + +//Secret Trigger +typedef struct { + int id, flag; + int type, enemyType; +} SecretTrigger; + +void createSecretTrigger(int type, int enemyType, int flag); + +//Chest +typedef struct { + int id; + int x, y; + int item, secret; + int visible; + int timer; + + Mask mask; +} Chest; + +void createChest(int x, int y, int item, int secret); + +//Save point +typedef struct { + int id; + int x, y; + double imageIndex; + + Mask mask; +} SavePoint; + +void createSavePoint(int x, int y, int hidden); + +//Door +typedef struct { + int id; + int x, y; + int open, secret, visible; + + int warplevel, warpcoords; + int warpx, warpy; + + Mask mask; +} Door; + +void createDoor(int x, int y, int level, int coords, int warpx, int warpy, int secret); + +//Lock Block +typedef struct { + int id, flag; + int x, y; + int tile; + int invincible; +} LockBlock; + +void createLockBlock(int x, int y, int flag); + +//Light Switch +typedef struct { + int id, flag; + int x, y; + int activated; + double imageIndex; +} Switch; + +void createSwitch(int x, int y, int flag); + +//Blue/Red Gates +typedef struct { + int id; + int x, y; + int col; + int timer, open; + //int invincible; + double imageIndex; +} Gate; + +void createGate(int x, int y, int col); + +//Statue +typedef struct { + int id; + int x, y; + int type; + int invincible; + int hp; +} Statue; + +void createStatue(int x, int y, int type); + +//Button +typedef struct { + int id; + int x, y; + int flag; + int pressed; +} FloorPad; + +void createFloorPad(int x, int y, int flag); + +//Ladder +typedef struct { + int id; + int x, y; + int flag; +} Ladder; + +void createLadder(int x, int y, int flag); + +//Generator +typedef struct { + int id; + int hp; + int blink; + int x, y; + double imageIndex; + int flag; +} Generator; + +void createGenerator(int x, int y, int flag); + +//Electric gate +typedef struct { + int id; + int x, y; + double imageIndex; + int flag; +} Shockgate; + +void createShockgate(int x, int y, int flag); + +//Ending crown +typedef struct { + int id; + int x, ystart; + double y; + double bobRot; + double imageIndex; + int timer; + char visible; +} Crown; + +void createCrown(int x, int y); + +#endif \ No newline at end of file diff --git a/src/options.c b/src/options.c new file mode 100644 index 0000000..4c31314 --- /dev/null +++ b/src/options.c @@ -0,0 +1,264 @@ +#include "options.h" +#include "PHL.h" +#include "game.h" +#include "ini.h" +#include "text.h" + +char page = 0; +int optCursor = 0; +int lastOption = -1; + +int options() +{ + char tempDark = roomDarkness; + roomDarkness = 0; + + int result = -1; + char loop = 1; + + while (PHL_MainLoop() && loop == 1) + { + PHL_StartDrawing(); + + optionsDraw(); + + PHL_ScanInput(); + result = optionsStep(); + + PHL_EndDrawing(); + + if (page == 0 && result != -1 && result != 2) { + loop = 0; + } + } + + roomDarkness = tempDark; + + return result; +} + +int optionsStep() +{ + int result = -1; + + secretCountdown(); + + //input + if (btnDown.pressed == 1) { + optCursor += 1; + PHL_PlaySound(sounds[sndPi01], 1); + + }else if (btnUp.pressed == 1) { + optCursor -= 1; + PHL_PlaySound(sounds[sndPi01], 1); + } + + //First screen (continue/reset/exit) + if (page == 0) { + //Limit cursor + if (optCursor >= 4) { + optCursor = 0; + } + + if (optCursor < 0) { + optCursor = 3; + } + + if (btnAccept.pressed == 1 || btnStart.pressed == 1) { + result = optCursor; + if (result == 1) { + PHL_StopMusic(); + } + if (result == 2) { + page = 1; + optCursor = 0; + } + + //No blip on game exit + if (optCursor != 3) { + PHL_PlaySound(sounds[sndOk], 1); + } + } + + if (btnSelect.pressed == 1) { + result = 0; + PHL_PlaySound(sounds[sndOk], 1); + } + } + //Actual options screen + else if (page == 1) { + //Limit cursor + if (optCursor > lastOption) { + optCursor = 0; + } + + if (optCursor < 0) { + optCursor = lastOption; + } + + if (btnAccept.pressed == 1 || btnStart.pressed == 1) { + //Universal settings + //Language + if (optCursor == 0) { + if (getLanguage() == JAPANESE) { + setLanguage(ENGLISH); + }else{ + setLanguage(JAPANESE); + } + } + + //Autosave + if (optCursor == 1) { + if (getAutoSave() == 0) { + setAutoSave(1); + }else{ + setAutoSave(0); + } + } + + #ifdef _3DS + if (optCursor == 2) { + if (activeScreen->screen == GFX_TOP) { + swapScreen(GFX_BOTTOM, GFX_LEFT); + }else{ + swapScreen(GFX_TOP, GFX_LEFT); + } + } + #endif + + #ifdef _PSP + if (optCursor == 2) { + if (getScreenSize() == 0) { + setScreenSize(1); + }else if (getScreenSize() == 1) { + setScreenSize(2); + }else{ + setScreenSize(0); + } + } + + //Blur + if (optCursor == 3) { + if (getBlur() == 0) { + setBlur(1); + }else{ + setBlur(0); + } + } + #endif + + //Back + if (optCursor == lastOption) { + page = 0; + } + } + else if (btnDecline.pressed == 1) { + page = 0; + } + + if (page == 0) { + saveSettings(); + } + } + + return result; +} + +void optionsDraw() +{ + PHL_DrawRect(0, 0, 640, 480, PHL_NewRGB(0, 0, 0)); + + if (page == 0) + { + PHL_DrawTextBold("CONTINUE", 254, 144, YELLOW); + PHL_DrawTextBold("RESET", 278, 176, YELLOW); + PHL_DrawTextBold("OPTIONS", 262, 208, YELLOW); + PHL_DrawTextBold("EXIT", 286, 240, YELLOW); + + PHL_DrawTextBold("<", 232, 144 + (32 * optCursor), RED); + PHL_DrawTextBold(">", 390, 144 + (32 * optCursor), RED); + } + else if (page == 1) + { + int xleft = 216; + int xright = xleft + 160; + + int ydrawstart = 144; + int ydraw = ydrawstart; + int ystep = 32; + int optioncount = 0; + + //Language + PHL_DrawTextBold("LANGUAGE", xleft, ydraw, YELLOW); + if (getLanguage() == 1) { + PHL_DrawTextBold("EN", xright, ydraw, YELLOW); + } + if (getLanguage() == 0) { + PHL_DrawTextBold("JP", xright, ydraw, YELLOW); + } + + ydraw += ystep; + optioncount++; + + //AutoSave + PHL_DrawTextBold("SAFE SAVE", xleft, ydraw, YELLOW); + if (getAutoSave() == 1) { + PHL_DrawTextBold("ON", xright, ydraw, YELLOW); + }else{ + PHL_DrawTextBold("OFF", xright, ydraw, YELLOW); + } + + ydraw += ystep; + optioncount++; + + #ifdef _3DS + //Screen + PHL_DrawTextBold("SCREEN", xleft, ydraw, YELLOW); + if (activeScreen->screen == GFX_TOP) { + PHL_DrawTextBold("TOP", xright, ydraw, YELLOW); + }else{ + PHL_DrawTextBold("BOTTOM", xright, ydraw, YELLOW); + } + + ydraw += ystep; + optioncount++; + #endif + + #ifdef _PSP + //Screen Size + PHL_DrawTextBold("SCREEN", xleft, ydraw, YELLOW); + if (getScreenSize() == 0) { + PHL_DrawTextBold("1:1", xright, ydraw, YELLOW); + } + else if (getScreenSize() == 1) { + PHL_DrawTextBold("FILL", xright, ydraw, YELLOW); + } + else if (getScreenSize() == 2) { + PHL_DrawTextBold("FULL", xright, ydraw, YELLOW); + } + + ydraw += ystep; + optioncount++; + + //Blur + PHL_DrawTextBold("BLUR", xleft, ydraw, YELLOW); + if (getBlur() == 1) { + PHL_DrawTextBold("ON", xright, ydraw, YELLOW); + } + else{ + PHL_DrawTextBold("OFF", xright, ydraw, YELLOW); + } + + ydraw += ystep; + optioncount++; + #endif + + PHL_DrawTextBold("BACK", xleft, ydraw, YELLOW); + + if (lastOption == -1) { + lastOption = optioncount; + } + + PHL_DrawTextBold(">", xleft - 32, ydrawstart + (ystep * optCursor), RED); + } +} \ No newline at end of file diff --git a/src/options.h b/src/options.h new file mode 100644 index 0000000..2e8414b --- /dev/null +++ b/src/options.h @@ -0,0 +1,9 @@ +#ifndef OPTIONS_H +#define OPTIONS_H + +int options(); + +int optionsStep(); +void optionsDraw(); + +#endif \ No newline at end of file diff --git a/src/platform.c b/src/platform.c new file mode 100644 index 0000000..4435aa7 --- /dev/null +++ b/src/platform.c @@ -0,0 +1,223 @@ +#include "platform.h" +#include "game.h" +#include "PHL.h" +#include "hero.h" +#include + +void createPlatform(int type, int xstart, int ystart, int xend, int yend, int spd, int secret) +{ + int i; + for (i = 0; i < MAX_PLATFORMS; i++) { + if (platforms[i] == NULL) { + Platform* p = malloc(sizeof *p); + p->id = i; + p->type = type; + + p->x = p->xstart = xstart; + p->y = p->ystart = ystart; + + p->xend = xend; + p->yend = yend; + + p->timer = 0; + + p->secret = secret; + + if (roomSecret == 1) { + p->secret = 0; + } + + p->visible = 1; + if (p->secret == 1) { + p->visible = 0; + } + + if (type == 1) { + p->xend = p->x + 1; + p->yend = p->y; + } + + p->state = 0; + p->spd = spd; + + p->mask.circle = 0; + p->mask.unused = 0; + if (p->secret == 1) { + p->mask.unused = 1; + } + p->mask.x = xstart; + p->mask.y = ystart; + p->mask.w = p->mask.h = 40; + + platforms[i] = p; + i = MAX_PLATFORMS; + } + } +} + +void platformStep(Platform* p) +{ + char isSolid = p->visible; + + //Megaman blocks + if (p->type == 2) { + int myStep = p->xend, + maxSteps = p->yend; + + p->timer += 1; + if (p->timer >= maxSteps * 60 + 60) { + p->timer = 0; + } + + //Play sound + if (p->timer == myStep * 60) { + PHL_PlaySound(sounds[sndPi03], CHN_SOUND); + } + + if (p->timer > myStep * 60 && p->timer <= (myStep * 60) + 60) { + isSolid = 1; + p->visible = 1; + + //Blink effect + if (p->timer > myStep * 60 + 30) { + p->visible = p->timer % 2; + } + }else{ + isSolid = 0; + p->mask.unused = 1; + p->visible = 0; + + //Fall off platform + if (getHeroMask().x > p->mask.x + p->mask.w || getHeroMask().x + getHeroMask().w < p->mask.x) { + }else if (heroy == p->y - 40 && getHeroVsp() >= 0) { + setHeroOnground(0); + } + } + } + + if (isSolid == 1) { + p->mask.unused = 0; + + int targx = p->xend, + targy = p->yend; + + if (p->state == 1) { + targx = p->xstart; + targy = p->ystart; + } + + //Check if the player is standing on top + int isontop = 0; + int isabove = 0; + if (getHeroMask().x > p->mask.x + p->mask.w || getHeroMask().x + getHeroMask().w < p->mask.x) { + }else if (heroy == p->y - 40 && getHeroVsp() >= 0) { + isontop = 1; + }else if (heroy < p->y - 40) { + isabove = 1; + } + + //Move platform + if (p->y != targy) { + if (p->y < targy) { + p->y += p->spd; + if (isontop == 1) { + heroy += p->spd; + } + }else{ + p->y -= p->spd; + if (isontop == 1) { + heroy -= p->spd; + }else{ + p->mask.y = p->y; + if (checkCollision(p->mask, getHeroMask()) && isabove == 1) { + heroy = p->y - 40; + } + } + } + } + + if (p->x != targx) { + if (p->x < targx) { + p->x += p->spd; + if (isontop == 1) { + herox += p->spd; + } + }else{ + p->x -= p->spd; + if (isontop == 1) { + herox -= p->spd; + } + } + } + + if (p->x == targx && p->y == targy) { + if (p->state == 0) { + p->state = 1; + }else{ + p->state = 0; + } + } + + if (p->type == 0) //Moving platform + { + + } + else if (p->type == 1) { //Loose block + if (p->spd != 0) { + p->timer -= 1; + if (p->timer <= 0) { + createRockSmash(p->x + 20, p->y + 20); + if (isontop == 1) { + setHeroOnground(0); + } + platformDestroy(p->id); + /*createEffectExtra(4, p->x, p->y, -1, 0, 0); + createEffectExtra(4, p->x, p->y, -1, 0, 1); + createEffectExtra(4, p->x, p->y, 1, 0, 0); + createEffectExtra(4, p->x, p->y, 1, 0, 1); + */ + } + } + + if (p->spd == 0 && isontop == 1) { + p->spd = 2; + p->timer = 30; + } + } + + //Update Mask + p->mask.x = p->x; + p->mask.y = p->y; + }else{ + //p->mask.unused = 1; + if (p->secret == 1) { + if (roomSecret == 1) { + p->mask.unused = 0; + p->visible = 1; + p->secret = 0; + playSecret(); + } + } + } +} + +void platformDraw(Platform* p) +{ + if (p->visible == 1) { + int cropX = p->type * 40; + + if (p->type == 3) { + cropX = 9 * 40; + } + + PHL_DrawSurfacePart(p->x, p->y, cropX, 400, 40, 40, images[imgMisc20]); + } +} + +void platformDestroy(int id) +{ + if (platforms[id] != NULL) { + free(platforms[id]); + } + platforms[id] = NULL; +} \ No newline at end of file diff --git a/src/platform.h b/src/platform.h new file mode 100644 index 0000000..8375d46 --- /dev/null +++ b/src/platform.h @@ -0,0 +1,26 @@ +#ifndef PLATFORM_H +#define PLATFORM_H + +#include "collision.h" + +typedef struct { + int id, type; //0 = moving platform + double x, y; + int xstart, ystart; + int xend, yend; + int state; + double spd; + int timer; + int secret, visible; + + Mask mask; +} Platform; + +void createPlatform(int type, int xstart, int ystart, int xend, int yend, int spd, int secret); + +void platformStep(Platform* p); +void platformDraw(Platform* p); + +void platformDestroy(int id); + +#endif \ No newline at end of file diff --git a/src/psp/audio.c b/src/psp/audio.c new file mode 100644 index 0000000..7b4242f --- /dev/null +++ b/src/psp/audio.c @@ -0,0 +1,87 @@ +#include "audio.h" +#include "../game.h" + +void PHL_AudioInit() +{ + oslInitAudio(); +} + +void PHL_AudioClose() +{ + oslDeinitAudio(); +} + +//Each system can use a custom music file format +OSL_SOUND* PHL_LoadMusic(char* fname, int loop) +{ + char stream = 0; + + OSL_SOUND* result = NULL; + + char fullPath[40]; + strcpy(fullPath, fname); + strcat(fullPath, ".bgm"); + + FILE* file; + + if ( (file = fopen(fullPath, "r")) ) { + fclose(file); + result = oslLoadSoundFile(fullPath, stream); + oslSetSoundLoop(result, loop); + } + + return result; +} + +OSL_SOUND* PHL_LoadSound(char* fname) +{ + char stream = 0; + + OSL_SOUND* result = NULL; + + char fullPath[40]; + strcpy(fullPath, fname); + + FILE* file; + + if ( (file = fopen(fullPath, "r")) ) { + fclose(file); + result = oslLoadSoundFile(fullPath, stream); + oslSetSoundLoop(result, 0); + } + + return result; +} + + +void PHL_PlayMusic(OSL_SOUND* snd) +{ + //Music always plays on the first channel + PHL_PlaySound(snd, 0); +} + +void PHL_PlaySound(OSL_SOUND* snd, int channel) +{ + if (snd) { + oslPlaySound(snd, channel); + } +} + +void PHL_StopMusic() +{ + PHL_StopSound(bgmMusic, 0); +} + +void PHL_StopSound(OSL_SOUND* snd, int channel) +{ + if (snd) { + oslStopSound(snd); + } +} + +void PHL_FreeSound(OSL_SOUND* snd) +{ + if (snd) { + oslDeleteSound(snd); + } +} \ No newline at end of file diff --git a/src/psp/audio.h b/src/psp/audio.h new file mode 100644 index 0000000..c77c983 --- /dev/null +++ b/src/psp/audio.h @@ -0,0 +1,24 @@ +#ifndef PSPAUDIO_H +#define PSPAUDIO_H + +#include + +#define PHL_Music OSL_SOUND +#define PHL_Sound OSL_SOUND + +void PHL_AudioInit(); +void PHL_AudioClose(); + +OSL_SOUND* PHL_LoadMusic(char* fname, int loop); //Same as PHL_LoadSound, but expects a file name without extension +OSL_SOUND* PHL_LoadSound(char* fname); + +void PHL_PlayMusic(OSL_SOUND* snd); +void PHL_PlaySound(OSL_SOUND* snd, int channel); + +void PHL_StopMusic(); +void PHL_StopSound(OSL_SOUND* snd, int channel); + +#define PHL_FreeMusic PHL_FreeSound +void PHL_FreeSound(OSL_SOUND* snd); + +#endif \ No newline at end of file diff --git a/src/psp/graphics.c b/src/psp/graphics.c new file mode 100644 index 0000000..469c12e --- /dev/null +++ b/src/psp/graphics.c @@ -0,0 +1,249 @@ +#include "graphics.h" +#include "../game.h" +#include "../qda.h" + +int screenOffsetX = 80; +int screenOffsetY = 16; +int fullscreen = 0; +int blurFilter = 0; + +//One time graphics setup +void PHL_GraphicsInit() +{ + oslInit(0); + oslInitGfx(OSL_PF_8888, 1); + + screen = oslCreateImage(320, 240, OSL_IN_VRAM, OSL_PF_5650); + + PHL_StartDrawing(); +} + +void PHL_GraphicsExit() +{ + oslEndGfx(); + oslDeleteImage(screen); + + //closeQDA(); +} + +void PHL_StartDrawing() +{ + oslStartDrawing(); + oslSetDrawBuffer(screen); +} + +void PHL_EndDrawing() +{ + oslSetDrawBuffer(OSL_DEFAULT_BUFFER); + + oslSetBilinearFilter(blurFilter); + oslDrawImageXY(screen, screenOffsetX, screenOffsetY); + oslSetBilinearFilter(0); + + oslEndDrawing(); + oslEndFrame(); + oslSyncFrame(); +} + +void PHL_ForceScreenUpdate() +{ + oslSetDrawBuffer(OSL_DEFAULT_BUFFER); + oslDrawImageXY(screen, screenOffsetX, screenOffsetY); + oslEndDrawing(); + oslAudioVSync(); + oslSyncFrame(); + + oslStartDrawing(); + oslSetDrawBuffer(screen); +} + +void PHL_SetDrawbuffer(OSL_IMAGE* surf) +{ + //unused in psp version +} + +void PHL_ResetDrawbuffer() +{ + //unused in psp version +} + +void PHL_SetColorKey(OSL_IMAGE* surf, int r, int g, int b) +{ + +} + +OSL_IMAGE* PHL_NewSurface(int w, int h) +{ + OSL_IMAGE* surf = oslCreateImage(w, h, OSL_IN_RAM, OSL_PF_8BIT); + + return surf; +} + +void PHL_FreeSurface(OSL_IMAGE* surf) +{ + if (surf) { + oslDeleteImage(surf); + surf = NULL; + } +} + +OSL_IMAGE* PHL_LoadBMP(int index) +{ + OSL_IMAGE* result = NULL; + + FILE* f; + if ( (f = fopen("bmp.qda", "rb")) ) { + //Load QDA file data + unsigned char* QDAFile = (unsigned char*)malloc(headers[index].size); + fseek(f, headers[index].offset, SEEK_SET); + fread(QDAFile, headers[index].size, 1, f); + + OSL_COLOR palette[20][18]; + + //Read data from header + unsigned short w, h; + + memcpy(&w, &QDAFile[18], 2); + memcpy(&h, &QDAFile[22], 2); + + result = oslCreateImage(w, h, OSL_IN_RAM, OSL_PF_8888); + oslUnswizzleImage(result); + oslClearImage(result, RGBA(255, 255, 255, 255)); + + //Load Palette + int dx, dy; + int count = 0; + for (dx = 0; dx < 20; dx++) { + for (dy = 0; dy < 16; dy++) { + palette[dx][dy] = RGB(QDAFile[54 + count + 2], QDAFile[54 + count + 1], QDAFile[54 + count]); + count += 4; + } + } + + OSL_COLOR alphaKey = palette[0][0]; + + //Darkness special case + if (index == 27) { + alphaKey = RGB(0, 0, 0); + } + + //Edit surface pixels + oslLockImage(result); + for (dx = w; dx > 0; dx--) { + for (dy = h; dy >= 0; dy--) { + int pix = w - dx + w * dy; + + int px = QDAFile[1078 + pix] / 16; + int py = QDAFile[1078 + pix] % 16; + + if (palette[px][py] == alphaKey) { + oslSetImagePixel(result, w - dx, h - dy - 1, RGBA(0, 0, 0, 0)); + }else{ + oslSetImagePixel(result, w - dx, h - dy - 1, palette[px][py]); + } + } + } + oslUnlockImage(result); + + oslSwizzleImage(result); + + free(QDAFile); + } + + fclose(f); + + return result; +} + +void PHL_DrawRect(int x, int y, int w, int h, OSL_COLOR col) +{ + oslDrawFillRect(x/2, y/2, (x + w) / 2, (y + h) / 2, col); +} + +void PHL_DrawSurface(double x, double y, OSL_IMAGE* surface) +{ + oslDrawImageXY(surface, (int)(x/2), (int)(y/2)); +} + +void PHL_DrawSurfacePart(double x, double y, int cropx, int cropy, int cropw, int croph, OSL_IMAGE* surface) +{ + //Consider earthquake + if (quakeTimer > 0) { + int val = quakeTimer % 4; + if (val == 0) { + y -= 2; + }else if (val == 2) { + y += 2; + } + } + + OSL_IMAGE *crop = oslCreateImageTileSize(surface, cropx/2, cropy/2, cropw/2, croph/2); + oslDrawImageXY(crop, (int)(x/2), (int)(y/2)); + oslDeleteImage(crop); +} + +void PHL_DrawBackground(PHL_Background back, PHL_Background fore) +{ + int xx, yy; + + for (yy = 0; yy < 12; yy++) + { + for (xx = 0; xx < 16; xx++) + { + //Draw Background tiles + PHL_DrawSurfacePart(xx * 40, yy * 40, back.tileX[xx][yy] * 40, back.tileY[xx][yy] * 40, 40, 40, images[imgTiles]); + + //Only draw foreground tile if not a blank tile + if (fore.tileX[xx][yy] != 0 || fore.tileY[xx][yy] != 0) { + PHL_DrawSurfacePart(xx * 40, yy * 40, fore.tileX[xx][yy] * 40, fore.tileY[xx][yy] * 40, 40, 40, images[imgTiles]); + } + } + } +} + +void PHL_UpdateBackground(PHL_Background back, PHL_Background fore) +{ + //Unused in psp version +} + +void setBlur(int blur) +{ + blurFilter = blur; +} + +int getBlur() +{ + return blurFilter; +} + +void setScreenSize(int size) +{ + int screenW = 320; + int screenH = 240; + + if (size == 1) { + screenW = 368; + screenH = 272; + } + if (size == 2) { + screenW = 480; + screenH = 272; + } + + screenOffsetX = (480 - screenW) / 2; + screenOffsetY = (272 - screenH) / 2; + + screen->stretchX = screenW; + screen->stretchY = screenH; +} + +int getScreenSize() +{ + if (screen->stretchX == 368) { + return 1; + } + if (screen->stretchX == 480) { + return 2; + } + return 0; +} \ No newline at end of file diff --git a/src/psp/graphics.h b/src/psp/graphics.h new file mode 100644 index 0000000..4dec54e --- /dev/null +++ b/src/psp/graphics.h @@ -0,0 +1,66 @@ +#ifndef GRAPHICS_H +#define GRAPHICS_H + +#include +#include + +#define PHL_Surface OSL_IMAGE + +#define PHL_RGB OSL_COLOR +#define PHL_NewRGB RGB + +typedef struct { + int tileX[16][12]; + int tileY[16][12]; +} PHL_Background; + +/* +typedef struct { + unsigned int r, g, b; +} PHL_RGB; +*/ +/* +typedef struct { + OSL_IMAGE* pxdata; + int width; + int height; + PHL_RGB colorKey; +} PHL_Surface; +*/ +OSL_IMAGE* screen; + +void PHL_GraphicsInit(); +void PHL_GraphicsExit(); + +void PHL_StartDrawing(); +void PHL_EndDrawing(); + +void PHL_ForceScreenUpdate(); + +void PHL_SetDrawbuffer(OSL_IMAGE* surf); +void PHL_ResetDrawbuffer(); + +//PHL_RGB PHL_NewRGB(int r, int g, int b); +void PHL_SetColorKey(OSL_IMAGE* surf, int r, int g, int b); + +OSL_IMAGE* PHL_NewSurface(int w, int h); +void PHL_FreeSurface(OSL_IMAGE* surf); + +//PHL_Surface PHL_LoadBMP(char* fname); +OSL_IMAGE* PHL_LoadBMP(int index); + +void PHL_DrawRect(int x, int y, int w, int h, OSL_COLOR col); + +void PHL_DrawSurface(double x, double y, OSL_IMAGE* surface); +void PHL_DrawSurfacePart(double x, double y, int cropx, int cropy, int cropw, int croph, OSL_IMAGE* surface); + +void PHL_DrawBackground(PHL_Background back, PHL_Background fore); +void PHL_UpdateBackground(PHL_Background back, PHL_Background fore); + +void setBlur(int blur); +void setScreenSize(int size); + +int getBlur(); +int getScreenSize(); + +#endif \ No newline at end of file diff --git a/src/psp/input.c b/src/psp/input.c new file mode 100644 index 0000000..f54abfa --- /dev/null +++ b/src/psp/input.c @@ -0,0 +1,75 @@ +#include "input.h" +#include "../text.h" + +int deadZone = 64; + +void updateKey(Button* btn, int state); + +void PHL_ScanInput() +{ + oslReadKeys(); + + int pUp = 0, pDown = 0, pLeft = 0, pRight = 0; + + if (osl_keys->held.up || osl_keys->analogY < -deadZone) { pUp = 1; } + if (osl_keys->held.down || osl_keys->analogY > deadZone) { pDown = 1; } + if (osl_keys->held.left || osl_keys->analogX < -deadZone) { pLeft = 1; } + if (osl_keys->held.right || osl_keys->analogX > deadZone) { pRight = 1; } + + updateKey(&btnUp, pUp); + updateKey(&btnDown, pDown); + updateKey(&btnLeft, pLeft); + updateKey(&btnRight, pRight); + + updateKey(&btnL, osl_keys->held.L); + updateKey(&btnR, osl_keys->held.R); + + updateKey(&btnStart, osl_keys->held.start); + updateKey(&btnSelect, osl_keys->held.select); + + updateKey(&btnFaceRight, osl_keys->held.circle); + updateKey(&btnFaceDown, osl_keys->held.cross); + updateKey(&btnFaceLeft, osl_keys->held.square); + updateKey(&btnFaceUp, osl_keys->held.triangle); + + btnAccept = btnFaceDown; + btnDecline = btnFaceRight; + //Swap buttons for japanese + if (getLanguage() == JAPANESE) { + btnAccept = btnFaceRight; + btnDecline = btnFaceDown; + } + + /* + if (btnFaceUp.pressed == 1) { + FILE* f; + + if ( (f = fopen("debug.txt", "a")) ) { + fprintf(f, "\n%i bytes available", oslGetRamStatus().maxAvailable); + } + + fclose(f); + } + */ +} + +void updateKey(Button* btn, int state) +{ + if (state) { + if (btn->held == 1) { + btn->pressed = 0; + }else{ + btn->pressed = 1; + } + btn->held = 1; + btn->released = 0; + }else{ + if (btn->held == 1) { + btn->released = 1; + }else{ + btn->released = 0; + } + btn->held = 0; + btn->pressed = 0; + } +} \ No newline at end of file diff --git a/src/psp/input.h b/src/psp/input.h new file mode 100644 index 0000000..99adb9d --- /dev/null +++ b/src/psp/input.h @@ -0,0 +1,21 @@ +#ifndef INPUT_H +#define INPUT_H + +#include + +typedef struct { + int pressed, + held, + released; +} Button; + +Button btnUp, btnDown, btnLeft, btnRight; +Button btnFaceUp, btnFaceDown, btnFaceLeft, btnFaceRight; +Button btnL, btnR; +Button btnStart, btnSelect; +Button btnAccept, btnDecline; +int axisX, axisY; + +void PHL_ScanInput(); + +#endif \ No newline at end of file diff --git a/src/psp/system.c b/src/psp/system.c new file mode 100644 index 0000000..b14bf40 --- /dev/null +++ b/src/psp/system.c @@ -0,0 +1,48 @@ +#include "system.h" +#include "../text.h" + +PSP_MODULE_INFO("Hydra Castle Labrynth", 0, 1, 1); +PSP_MAIN_THREAD_ATTR(THREAD_ATTR_USER | THREAD_ATTR_VFPU); +//PSP_HEAP_SIZE_KB(12*1024); +PSP_HEAP_SIZE_KB(-1024); + +int PHL_MainLoop() +{ + if (quitGame == 1) { + return 0; + } + return 1; +} + +void PHL_ConsoleInit() +{ + pspDebugScreenInit(); +} + +void PHL_GameQuit() +{ + quitGame = 1; +} + +void PHL_ErrorScreen(char* message) +{ + /*consoleInit(GFX_TOP, NULL); + + printf(message); + printf("\n\nPress START to close"); + + u32 kDown; + while (PHL_MainLoop()) { + //gfxFlushBuffers(); + //gfxSwapBuffers(); + hidScanInput(); + kDown = hidKeysHeld(); + + if (kDown & KEY_START) { break; } + + gspWaitForVBlank(); + } + */ + + exit(1); +} \ No newline at end of file diff --git a/src/psp/system.h b/src/psp/system.h new file mode 100644 index 0000000..d7adc9b --- /dev/null +++ b/src/psp/system.h @@ -0,0 +1,16 @@ +#ifndef SYSTEM_H +#define SYSTEM_H + +#include +#include +#include "../PHL.h" + +char quitGame; + +int PHL_MainLoop(); +void PHL_ConsoleInit(); +void PHL_GameQuit(); + +void PHL_ErrorScreen(char* message); + +#endif \ No newline at end of file diff --git a/src/qda.c b/src/qda.c new file mode 100644 index 0000000..663fa00 --- /dev/null +++ b/src/qda.c @@ -0,0 +1,62 @@ +#include "qda.h" +#include +#include +#include + +//Load headers for each image +//Returns: 0 = file not found | 1 = success | 2 = Invalid file +int initQDA() +{ + int result = 0; + FILE* f; + + char fullPath[80]; + #ifdef _SDL + strcpy(fullPath, "data/"); + #else + strcpy(fullPath, ""); + #endif + #ifdef _3DS + strcat(fullPath, "romfs:/"); + #endif + strcat(fullPath, "bmp.qda"); + + if ( (f = fopen(fullPath, "rb")) ) { + result = 1; + + //Read header data into memory + int allHeadersSize = 0x1F5C; + unsigned char* QDAFile = (unsigned char*)malloc(allHeadersSize); + fread(QDAFile, allHeadersSize, 1, f); + + //Check if QDA file is valid + { + if (QDAFile[4] == 0x51 && QDAFile[5] == 0x44 && QDAFile[6] == 0x41 && QDAFile[7] == 0x30) { + //Load headers separately + { + int numofsheets = 29; + int headerSize = 0x10C; + + int i; + for (i = 0; i < numofsheets; i++) { + //memcpy(&headers[i], &QDAFile[0x100 + (i * headerSize)], sizeof(QDAHeader)); + int offset = 256 + (i * headerSize); + memcpy(&headers[i].offset, &QDAFile[offset], 4); + memcpy(&headers[i].size, &QDAFile[offset + 4], 4); + memcpy(&headers[i].bytes, &QDAFile[offset + 8], 4); + memcpy(&headers[i].fileName, &QDAFile[offset + 12], 0x100); + } + } + }else{ + result = 2; + } + } + + //Cleanup + free(QDAFile); + } + + fclose(f); + + return result; +} \ No newline at end of file diff --git a/src/qda.h b/src/qda.h new file mode 100644 index 0000000..316777e --- /dev/null +++ b/src/qda.h @@ -0,0 +1,14 @@ +#ifndef QDA_H +#define QDA_H + +typedef struct { + unsigned long offset; + unsigned long size; + unsigned long bytes; + unsigned char* fileName[256]; +} QDAHeader; + +QDAHeader headers[29]; //names, offsets, and sizes of each sheet +int initQDA(); + +#endif \ No newline at end of file diff --git a/src/sdl/audio.c b/src/sdl/audio.c new file mode 100644 index 0000000..8e19bf9 --- /dev/null +++ b/src/sdl/audio.c @@ -0,0 +1,70 @@ +#include "audio.h" +#include + +void PHL_AudioInit() +{ + SDL_InitSubSystem(SDL_INIT_AUDIO); + Mix_Init(0); + Mix_OpenAudio(44100, MIX_DEFAULT_FORMAT, 2, 4096); +} + +void PHL_AudioClose() +{ + Mix_CloseAudio(); + Mix_Quit(); +} + +//Same as PHL_LoadSound, but expects a file name without extension +PHL_Music PHL_LoadMusic(char* fname, int loop) +{ + PHL_Music ret; + ret.loop = loop; + char buff[4096]; + strcpy(buff, "data/"); + strcat(buff, fname); + strcat(buff, ".mid"); + ret.snd = Mix_LoadMUS(buff); + return ret; +} + +PHL_Sound PHL_LoadSound(char* fname) +{ + char buff[4096]; + strcpy(buff, "data/"); + strcat(buff, fname); + return Mix_LoadWAV(buff); +} + + +void PHL_PlayMusic(PHL_Music snd) +{ + if(snd.snd) + Mix_PlayMusic(snd.snd, snd.loop?-1:0); +} + +void PHL_PlaySound(PHL_Sound snd, int channel) +{ + Mix_PlayChannel(channel, snd, 0); +} + +void PHL_StopMusic() +{ + Mix_HaltMusic(); +} + +void PHL_StopSound(PHL_Sound snd, int channel) +{ + Mix_HaltChannel(channel); +} + +void PHL_FreeMusic(PHL_Music snd) +{ + if(snd.snd) + Mix_FreeMusic(snd.snd); + snd.snd = NULL; +} + +void PHL_FreeSound(PHL_Sound snd) +{ + Mix_FreeChunk(snd); +} diff --git a/src/sdl/audio.h b/src/sdl/audio.h new file mode 100644 index 0000000..2cf30d3 --- /dev/null +++ b/src/sdl/audio.h @@ -0,0 +1,30 @@ +#ifndef SDLAUDIO_H +#define SDLAUDIO_H + +#include + +//#define PHL_Music Mix_Music* +#define PHL_Sound Mix_Chunk* +typedef struct +{ + int loop; + Mix_Music* snd; + +} PHL_Music; + +void PHL_AudioInit(); +void PHL_AudioClose(); + +PHL_Music PHL_LoadMusic(char* fname, int loop); //Same as PHL_LoadSound, but expects a file name without extension +PHL_Sound PHL_LoadSound(char* fname); + +void PHL_PlayMusic(PHL_Music snd); +void PHL_PlaySound(PHL_Sound snd, int channel); + +void PHL_StopMusic(); +void PHL_StopSound(PHL_Sound snd, int channel); + +void PHL_FreeMusic(PHL_Music snd); +void PHL_FreeSound(PHL_Sound snd); + +#endif \ No newline at end of file diff --git a/src/sdl/graphics.c b/src/sdl/graphics.c new file mode 100644 index 0000000..fa03f49 --- /dev/null +++ b/src/sdl/graphics.c @@ -0,0 +1,220 @@ +#include +#include "../PHL.h" +#include "../game.h" +#include "../qda.h" +#include "graphics.h" + +SDL_Surface* screen = NULL; +SDL_Surface* drawbuffer = NULL; +SDL_Surface* backbuffer = NULL; + +SDL_Color PHL_NewRGB(uint8_t r, uint8_t g, uint8_t b) +{ + SDL_Color ret = {.r = r, .b = b, .g = g}; + return ret; +} + +void PHL_GraphicsInit() +{ + if ( SDL_Init(SDL_INIT_VIDEO) < 0) { + fprintf(stderr, "Error"); + SDL_Delay(5000); + exit(EXIT_FAILURE); + } + + SDL_ShowCursor(SDL_DISABLE); + + uint32_t flags = SDL_HWSURFACE|SDL_DOUBLEBUF; + #ifdef PANDORA + flags |= SDL_FULLSCREEEN; + #endif + screen = SDL_SetVideoMode(640, 480, 0, flags); + drawbuffer = screen; + backbuffer = PHL_NewSurface(640, 480); +} + +void PHL_GraphicsExit() +{ + SDL_FreeSurface(backbuffer); + SDL_Quit(); +} + +static uint32_t tframe; +void PHL_StartDrawing() +{ + tframe = SDL_GetTicks() + 1000/60; + PHL_ResetDrawbuffer(); +} +void PHL_EndDrawing() +{ + SDL_Flip(screen); + while(SDL_GetTicks()format, r, g, b)); +} + +PHL_Surface PHL_NewSurface(int w, int h) +{ + return SDL_CreateRGBSurface(0, w, h, 32, 0, 0, 0, 0); +} +void PHL_FreeSurface(PHL_Surface surf) +{ + SDL_FreeSurface(surf); +} + +//PHL_Surface PHL_LoadBMP(char* fname); +PHL_Surface PHL_LoadBMP(int index) +{ + SDL_Surface* surf; + + FILE* f; + if ( (f = fopen("data/bmp.qda", "rb")) ) { + uint8_t* QDAFile = (uint8_t*)malloc(headers[index].size); + fseek(f, headers[index].offset, SEEK_SET); + fread(QDAFile, 1, headers[index].size, f); + fclose(f); + + PHL_RGB palette[20][18]; + + uint16_t w, h; + + //Read data from header + memcpy(&w, &QDAFile[18], 2); + memcpy(&h, &QDAFile[22], 2); + + surf = PHL_NewSurface(w * 2, h * 2); + //surf = PHL_NewSurface(200, 200); + + //Load Palette + int dx, dy; + int count = 0; + for (dx = 0; dx < 20; dx++) { + for (dy = 0; dy < 16; dy++) { + palette[dx][dy].b = QDAFile[54 + count]; + palette[dx][dy].g = QDAFile[54 + count + 1]; + palette[dx][dy].r = QDAFile[54 + count + 2]; + count += 4; + } + } + + for (dx = 0; dx < w; dx++) { + for (dy = 0; dy < h; dy++) { + + int pix = dx + w * dy; + int px = QDAFile[1078 + pix] / 16; + int py = QDAFile[1078 + pix] % 16; + //Get transparency from first palette color + if (dx == 0 && dy == 0) { + //Darkness special case + if (index == 27) { + SDL_SetColorKey(surf, SDL_SRCCOLORKEY, SDL_MapRGB(surf->format, 0x00, 0x00, 0x00)); + }else{ + SDL_SetColorKey(surf, SDL_SRCCOLORKEY, SDL_MapRGB(surf->format, palette[0][0].r, palette[0][0].g, palette[0][0].b)); + } + } + + PHL_RGB c = palette[px][py]; + + //PHL_DrawRect(dx * 2, dy * 2, 2, 2, c); + SDL_Rect rect = {dx * 2, (h-1-dy) * 2, 2, 2}; + SDL_FillRect(surf, &rect, SDL_MapRGB(surf->format, c.r, c.g, c.b)); + } + } + free(QDAFile); + } + + return surf; +} + +void PHL_DrawRect(int x, int y, int w, int h, SDL_Color col) +{ + SDL_Rect rect = {x, y, w, h}; + + SDL_FillRect(drawbuffer, &rect, SDL_MapRGB(drawbuffer->format, col.r, col.g, col.b)); +} + +void PHL_DrawSurface(double x, double y, PHL_Surface surface) +{ + if (quakeTimer > 0) { + int val = quakeTimer % 4; + if (val == 0) { + y -= 1; + } else if (val == 2) { + y += 1; + } + } + + SDL_Rect offset; + offset.x = x; + offset.y = y; + + SDL_BlitSurface(surface, NULL, drawbuffer, &offset); +} +void PHL_DrawSurfacePart(double x, double y, int cropx, int cropy, int cropw, int croph, PHL_Surface surface) +{ + if (quakeTimer > 0) { + int val = quakeTimer % 4; + if (val == 0) { + y -= 1; + }else if (val == 2) { + y += 1; + } + } + + SDL_Rect crop, offset; + crop.x = cropx; + crop.y = cropy; + crop.w = cropw; + crop.h = croph; + + offset.x = x; + offset.y = y; + + SDL_BlitSurface(surface, &crop, drawbuffer, &offset); +} + +void PHL_DrawBackground(PHL_Background back, PHL_Background fore) +{ + PHL_DrawSurface(0, 0, backbuffer); +} +void PHL_UpdateBackground(PHL_Background back, PHL_Background fore) +{ + PHL_SetDrawbuffer(backbuffer); + + int xx, yy; + + for (yy = 0; yy < 12; yy++) + { + for (xx = 0; xx < 16; xx++) + { + //Draw Background tiles + PHL_DrawSurfacePart(xx * 40, yy * 40, back.tileX[xx][yy] * 40, back.tileY[xx][yy] * 40, 40, 40, images[imgTiles]); + + //Only draw foreground tile if not a blank tile + if (fore.tileX[xx][yy] != 0 || fore.tileY[xx][yy] != 0) { + PHL_DrawSurfacePart(xx * 40, yy * 40, fore.tileX[xx][yy] * 40, fore.tileY[xx][yy] * 40, 40, 40, images[imgTiles]); + } + } + } + + PHL_ResetDrawbuffer(); +} diff --git a/src/sdl/graphics.h b/src/sdl/graphics.h new file mode 100644 index 0000000..3635294 --- /dev/null +++ b/src/sdl/graphics.h @@ -0,0 +1,65 @@ +#ifndef GRAPHICS_H +#define GRAPHICS_H + +#include + +#define PHL_Surface SDL_Surface* + +#define PHL_RGB SDL_Color + +typedef struct { + int tileX[16][12]; + int tileY[16][12]; +} PHL_Background; + +/* +typedef struct { + unsigned int r, g, b; +} PHL_RGB; +*/ +/* +typedef struct { + OSL_IMAGE* pxdata; + int width; + int height; + PHL_RGB colorKey; +} PHL_Surface; +*/ +extern PHL_Surface screen; + +SDL_Color PHL_NewRGB(uint8_t r, uint8_t g, uint8_t b); +/* +{ + SDL_Color ret = {.r = r, .b = b, .g = g}; + return ret; +} +*/ +void PHL_GraphicsInit(); +void PHL_GraphicsExit(); + +void PHL_StartDrawing(); +void PHL_EndDrawing(); + +void PHL_ForceScreenUpdate(); + +void PHL_SetDrawbuffer(PHL_Surface surf); +void PHL_ResetDrawbuffer(); + +//PHL_RGB PHL_NewRGB(int r, int g, int b); +void PHL_SetColorKey(PHL_Surface surf, int r, int g, int b); + +PHL_Surface PHL_NewSurface(int w, int h); +void PHL_FreeSurface(PHL_Surface surf); + +//PHL_Surface PHL_LoadBMP(char* fname); +PHL_Surface PHL_LoadBMP(int index); + +void PHL_DrawRect(int x, int y, int w, int h, SDL_Color col); + +void PHL_DrawSurface(double x, double y, PHL_Surface surface); +void PHL_DrawSurfacePart(double x, double y, int cropx, int cropy, int cropw, int croph, PHL_Surface surface); + +void PHL_DrawBackground(PHL_Background back, PHL_Background fore); +void PHL_UpdateBackground(PHL_Background back, PHL_Background fore); + +#endif \ No newline at end of file diff --git a/src/sdl/input.c b/src/sdl/input.c new file mode 100644 index 0000000..c5f5a65 --- /dev/null +++ b/src/sdl/input.c @@ -0,0 +1,88 @@ +#include "input.h" +#include + +Button btnUp = {0}, btnDown = {0}, btnLeft = {0}, btnRight = {0}; +Button btnFaceUp = {0}, btnFaceDown = {0}, btnFaceLeft = {0}, btnFaceRight = {0}; +Button btnL = {0}, btnR = {0}; +Button btnStart = {0}, btnSelect = {0}; +Button btnAccept = {0}, btnDecline = {0}; +int axisX = 0, axisY = 0; + +int bUp = 0, bDown = 0, bLeft = 0, bRight = 0; +int bFaceUp = 0, bFaceDown = 0, bFaceLeft = 0, bFaceRight = 0; +int bR = 0, bL = 0; +int bStart = 0, bSelect = 0; +int bAccept = 0, bDecline = 0; + +void Input_KeyEvent(SDL_Event* evt) +{ + int w = (evt->type==SDL_KEYDOWN)?1:0; + switch(evt->key.keysym.sym) + { + case SDLK_UP: bUp = w; break; + case SDLK_DOWN: bDown = w; break; + case SDLK_LEFT: bLeft = w; break; + case SDLK_RIGHT: bRight = w; break; +#ifdef PANDORA + case SDLK_PAGEUP: bFaceUp = w; break; + case SDLK_PAGEDOWN: bFaceDown = w; break; + case SDLK_END: bFaceLeft = w; break; // reversing, so (B) is sword + case SDLK_HOME: bFaceRight = w; break; + case SDLK_RCTRL: bR = w; break; + case SDLK_RSHIFT: bL = w; break; + case SDLK_LCTRL: bSelect = w; break; + case SDLK_LALT: bStart = w; break; +#else + case SDLK_e: bFaceUp = w; break; + case SDLK_x: bFaceDown = w; break; + case SDLK_s: bFaceLeft = w; break; + case SDLK_d: bFaceRight = w; break; + case SDLK_r: bR = w; break; + case SDLK_w: bL = w; break; + case SDLK_SPACE: bSelect = w; break; + case SDLK_RETURN: bStart = w; break; +#endif + } +} + +void updateKey(Button* btn, int state) +{ + if (state) { + if (btn->held == 1) { + btn->pressed = 0; + }else{ + btn->pressed = 1; + } + btn->held = 1; + btn->released = 0; + }else{ + if (btn->held == 1) { + btn->released = 1; + }else{ + btn->released = 0; + } + btn->held = 0; + btn->pressed = 0; + } +} + +void PHL_ScanInput() +{ + updateKey(&btnUp, bUp); + updateKey(&btnDown, bDown); + updateKey(&btnLeft, bLeft); + updateKey(&btnRight, bRight); + + updateKey(&btnStart, bStart); + updateKey(&btnSelect, bSelect); + + updateKey(&btnL, bL); + updateKey(&btnR, bR); + + updateKey(&btnFaceLeft, bFaceLeft); + updateKey(&btnFaceDown, bFaceDown); + updateKey(&btnFaceRight, bFaceRight); + + updateKey(&btnAccept, bFaceLeft); + updateKey(&btnDecline, bFaceDown); +} diff --git a/src/sdl/input.h b/src/sdl/input.h new file mode 100644 index 0000000..ec627f6 --- /dev/null +++ b/src/sdl/input.h @@ -0,0 +1,21 @@ +#ifndef INPUT_H +#define INPUT_H + +#include + +typedef struct { + int pressed, + held, + released; +} Button; + +extern Button btnUp, btnDown, btnLeft, btnRight; +extern Button btnFaceUp, btnFaceDown, btnFaceLeft, btnFaceRight; +extern Button btnL, btnR; +extern Button btnStart, btnSelect; +extern Button btnAccept, btnDecline; +extern int axisX, axisY; + +void PHL_ScanInput(); + +#endif \ No newline at end of file diff --git a/src/sdl/system.c b/src/sdl/system.c new file mode 100644 index 0000000..7e27d05 --- /dev/null +++ b/src/sdl/system.c @@ -0,0 +1,43 @@ +#include +#include "system.h" + + +char quitGame = 0; + +void Input_KeyEvent(SDL_Event* evt); + +int PHL_MainLoop() +{ + SDL_Event evt; + while(SDL_PollEvent(&evt)) + { + switch(evt.type) + { + case SDL_QUIT: + return 0; + case SDL_KEYDOWN: + case SDL_KEYUP: + Input_KeyEvent(&evt); + break; + //TODO: joystick... + } + } + if (quitGame == 1) + { + return 0; + } + return 1; +} +void PHL_ConsoleInit() +{ + +} +void PHL_GameQuit() +{ + quitGame = 1; +} + +void PHL_ErrorScreen(char* message) +{ + fprintf(stderr, "%s\n", message); +} diff --git a/src/sdl/system.h b/src/sdl/system.h new file mode 100644 index 0000000..2657796 --- /dev/null +++ b/src/sdl/system.h @@ -0,0 +1,15 @@ +#ifndef SYSTEM_H +#define SYSTEM_H + +#include +#include "../PHL.h" + +extern char quitGame; + +int PHL_MainLoop(); +void PHL_ConsoleInit(); +void PHL_GameQuit(); + +void PHL_ErrorScreen(char* message); + +#endif \ No newline at end of file diff --git a/src/stagedata.c b/src/stagedata.c new file mode 100644 index 0000000..3366438 --- /dev/null +++ b/src/stagedata.c @@ -0,0 +1,93 @@ +#include "stagedata.h" + +int stage[9][96] = { + //Overworld + { -1, -1, 20, 21, 22, 23, 24, 25, 26, 27, -1, -1, + -1, -1, 28, 29, 30, 31, 32, 33, 34, 35, -1, -1, + -1, -1, 36, 37, 38, 39, 40, 41, 42, 43, -1, -1, + -1, -1, 44, 45, 46, 47, 48, 49, 50, 51, -1, -1, + -1, -1, 52, 53, 54, 55, 56, 57, 58, 59, -1, -1, + -1, -1, 60, 61, 62, 63, 64, 65, 66, 67, -1, -1, + -1, -1, 68, 69, 70, 71, 72, 73, 74, 75, -1, -1, + -1, -1, 76, 77, 78, 79, 80, 81, 82, 83, -1, -1 }, + + //Level 1 + { -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, + -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, + -1, -1, -1, 84, 85, 86, -1, -1, -1, -1, -1, -1, + -1, -1, -1, -1, 87, 88, 89, 90, -1, -1, -1, -1, + -1, -1, -1, -1, -1, -1, 91, 92, 93, 94, -1, -1, + -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, + -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, + -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 }, + + //Level 2 + { -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, + -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, + -1, -1, -1, 95, -1, -1, -1, 96, -1, -1, -1, -1, + -1, -1, -1, 97, 98, 99, 100, 101, -1, -1, -1, -1, + -1, -1, -1, 102, 103, 104, 105, 106, -1, -1, -1, -1, + -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, + -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, + -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 }, + + //Level 3 + { -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, + -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, + -1, -1, 108, -1, 109, 110, 111, -1, 112, -1, -1, -1, + -1, -1, 113, 114, 115, 107, 116, 117, 118, -1, -1, -1, + -1, -1, -1, -1, 119, 120, 121, -1, -1, -1, -1, -1, + -1, -1, -1, -1, -1, 122, -1, -1, -1, -1, -1, -1, + -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, + -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 }, + + //Level 4 + { -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, + -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, + -1, -1, -1, -1, 129, 130, 131, 132, -1, 135, 136, -1, + -1, -1, 123, 124, 125, 126, 127, 128, 133, 134, -1, -1, + -1, -1, 137, 138, -1, -1, -1, -1, -1, -1, -1, -1, + -1, -1, -1, 139, -1, -1, -1, -1, -1, -1, -1, -1, + -1, -1, -1, 140, -1, -1, -1, -1, -1, -1, -1, -1, + -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 }, + + //Level 5 + { -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, + -1, -1, -1, 159, -1, 162, -1, -1, -1, -1, -1, -1, + -1, -1, 158, 160, 161, 163, 164, -1, -1, -1, -1, -1, + -1, -1, -1, 165, 166, 167, -1, -1, -1, -1, -1, -1, + -1, -1, 168, 169, 170, 171, 172, -1, -1, -1, -1, -1, + -1, -1, -1, 173, -1, 174, -1, -1, -1, -1, -1, -1, + -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, + -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 }, + + //Level 6 + { -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, + -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, + -1, -1, -1, -1, -1, -1, -1, 156, 157, -1, -1, -1, + -1, -1, -1, -1, 149, 150, 154, 155, -1, -1, -1, -1, + -1, -1, 151, -1, 152, 153, -1, -1, -1, -1, -1, -1, + -1, -1, 141, 142, 143, 144, 145, 146, -1, -1, -1, -1, + -1, -1, -1, -1, -1, 147, 148, -1, -1, -1, -1, -1, + -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 }, + + //Level 7 + { -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, + -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, + -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, + -1, -1, -1, 188, 189, 190, 191, -1, -1, -1, -1, -1, + -1, -1, -1, 182, 183, 184, 185, -1, -1, -1, -1, -1, + -1, 186, 175, 176, 177, 178, 179, 180, 181, -1, -1, -1, + -1, 187, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, + -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 }, + + //Level 8 + { -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, + -1, -1, -1, -1, -1, 192, -1, -1, -1, -1, -1, -1, + -1, -1, -1, 195, 194, 193, 198, 199, -1, -1, -1, -1, + -1, -1, -1, 196, 197, 202, 201, 200, -1, -1, -1, -1, + -1, -1, -1, 205, 204, 203, 208, 209, -1, -1, -1, -1, + -1, -1, -1, 206, 207, 212, 211, 210, -1, -1, -1, -1, + -1, -1, -1, -1, -1, 213, -1, -1, -1, -1, -1, -1, + -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, } + }; \ No newline at end of file diff --git a/src/stagedata.h b/src/stagedata.h new file mode 100644 index 0000000..62943fd --- /dev/null +++ b/src/stagedata.h @@ -0,0 +1,6 @@ +#ifndef STAGEDATA_H +#define STAGEDATA_H + +int stage[9][96]; + +#endif \ No newline at end of file diff --git a/src/text.c b/src/text.c new file mode 100644 index 0000000..acfa375 --- /dev/null +++ b/src/text.c @@ -0,0 +1,205 @@ +#include "text.h" +#include +#include +#include +#include "PHL.h" +#include "game.h" + +void loadMessage(Message* m, FILE* f); +void trimMessage(Message* m); + +void textInit() +{ + gameLanguage = JAPANESE; + + saving = (Message*)malloc(sizeof(Message)); + + int i; + for (i = 0; i < 3; i++) { + saveError[i] = (Message*)malloc(sizeof(Message)); + } + + for (i = 0; i < 8; i++) { + dungeon[i] = (Message*)malloc(sizeof(Message)); + } + + for (i = 0; i < 41; i++) { + itemName[i] = (Message*)malloc(sizeof(Message)); + } + + found = (Message*)malloc(sizeof(Message)); + + for (i = 0; i < 28; i++) { + itemDescription[i] = (Message*)malloc(sizeof(Message)); + } + + //printf("\nText init"); +} + +void textFree() +{ + free(saving); + + int i; + for (i = 0; i < 3; i++) { + free(saveError[i]); + } + + for (i = 0; i < 8; i++) { + free(dungeon[i]); + } + + for (i = 0; i < 41; i++) { + free(itemName[i]); + } + + free(found); + + for (i = 0; i < 28; i++) { + free(itemDescription[i]); + } +} + +void loadText() +{ + FILE* f; + + char fullPath[30]; + #ifdef _3DS + strcpy(fullPath, "romfs:/"); + #elif defined(_SDL) + strcpy(fullPath, "data/"); + #else + strcpy(fullPath, "romfs/"); + #endif + + if (gameLanguage == ENGLISH) { + strcat(fullPath, "en.dat"); + }else{ + strcat(fullPath, "jp.dat"); + } + + //printf("\n"); + //printf(fullPath); + + if ( (f = fopen(fullPath, "rb")) ) + { + //printf("\ntext.dat found"); + + //Load saving message + loadMessage(saving, f); + + printf("\n%d", saving->length); + + //Load save error message + int i; + for (i = 0; i < 3; i++) { + loadMessage(saveError[i], f); + } + + //Load dungeon intros + for (i = 0; i < 8; i++) { + loadMessage(dungeon[i], f); + } + + //Load item names + for (i = 0; i < 41; i++) { + loadMessage(itemName[i], f); + } + + //Found! + loadMessage(found, f); + + //Load item descriptions + for (i = 0; i < 28; i++) { + loadMessage(itemDescription[i], f); + } + + }else{ + //printf("\ntext.dat was not found"); + } + + fclose(f); +} + +//Returns the next character X position +int drawText(Message* m, int x, int y) +{ + int textw = 20; + int texth = 32; + + int dx = x; + + int i; + for (i = 0; i < m->length; i++) { + PHL_DrawSurfacePart(dx, y, m->x[i] * textw, m->y[i] * texth, textw, texth, images[imgFontKana]); + dx += textw - 2; + } + + return dx; +} + +int drawCharacter(int cx, int cy, int x, int y) +{ + int textw = 20; + int texth = 32; + + PHL_DrawSurfacePart(x, y, cx * textw, cy * texth, textw, texth, images[imgFontKana]); + + return x + textw - 2; +} + +void drawTextCentered(Message* m, int x, int y) +{ + int textw = 20; + int texth = 32; + + x -= (m->length / 2) * (textw - 2); + + int i; + for (i = 0; i < m->length; i++) { + PHL_DrawSurfacePart(x + (i * (textw - 2)), y, m->x[i] * textw, m->y[i] * texth, textw, texth, images[imgFontKana]); + } +} + +void setLanguage(char lan) +{ + gameLanguage = lan; + loadText(); +} + +char getLanguage() +{ + return gameLanguage; +} + +void loadMessage(Message* m, FILE* f) +{ + unsigned char* buffer = (unsigned char*)malloc(sizeof(unsigned char) * 64); + + fread(buffer, 1, 64, f); + + int i; + for (i = 0; i < 64; i+=2) { + m->x[i/2] = buffer[i]; + m->y[i/2] = buffer[i+1]; + } + trimMessage(m); + + free(buffer); +} + +void trimMessage(Message* m) +{ + m->length = 32; + + int i; + for (i = 31; i >= 0; i--) + { + if (m->x[i] == 0 && m->y[i] == 0) { + m->length -= 1; + }else{ + i = -1; + } + } +} \ No newline at end of file diff --git a/src/text.h b/src/text.h new file mode 100644 index 0000000..231d327 --- /dev/null +++ b/src/text.h @@ -0,0 +1,34 @@ +#ifndef TEXT_HEADER +#define TEXT_HEADER + +#define JAPANESE 0 +#define ENGLISH 1 + +char gameLanguage; + +typedef struct { + unsigned char x[32]; + unsigned char y[32]; + char length; +} Message; + +Message* saving; +Message* saveError[3]; +Message* itemName[41]; +Message* found; +Message* itemDescription[28]; +Message* dungeon[8]; + +void textInit(); +void textFree(); + +void loadText(); + +int drawText(Message* m, int x, int y); +int drawCharacter(int cx, int cy, int x, int y); +void drawTextCentered(Message* m, int x, int y); + +void setLanguage(char lan); +char getLanguage(); + +#endif \ No newline at end of file diff --git a/src/titlescreen.c b/src/titlescreen.c new file mode 100644 index 0000000..4369ac4 --- /dev/null +++ b/src/titlescreen.c @@ -0,0 +1,120 @@ +#include "titlescreen.h" +#include "game.h" +#include +#include "text.h" + +int tempsave = 0; +int cursor = 0; + +#ifdef _SDL +extern char savename[4096]; +#else +#define savename "data/save.tmp" +#endif + +void titleScreenSetup(); + +int titleScreenStep(); +void titleScreenDraw(); + +int titleScreen() +{ + titleScreenSetup(); + + char loop = 1; + int result = -1; + + while (PHL_MainLoop() && loop == 1) + { + //Get input + PHL_ScanInput(); + + //Titlescreen step + result = titleScreenStep(); + + //Draw titlescreen + PHL_StartDrawing(); + + titleScreenDraw(); + + if (result != -1) { + loop = 0; + //Force screen to black + PHL_DrawRect(0, 0, 640, 480, PHL_NewRGB(0, 0, 0)); + } + + PHL_EndDrawing(); + } + + return result; +} + +void titleScreenSetup() +{ + cursor = 0; + + //Move cursor if save file exists + if ( fileExists("map/018.map") ) { + cursor = 1; + } + + //Check if temp save file exists + tempsave = 0; + if ( fileExists(savename) ) { + tempsave = 1; + cursor = 1; + } +} + +int titleScreenStep() +{ + //Move cursor + if (btnDown.pressed == 1 || btnSelect.pressed == 1) { + cursor += 1; + if (cursor > 2) { + cursor = 0; + } + PHL_PlaySound(sounds[sndPi01], 1); + } + + if (btnUp.pressed == 1) { + cursor -= 1; + if (cursor < 0) { + cursor = 2; + } + PHL_PlaySound(sounds[sndPi01], 1); + } + + //Selection + if (btnAccept.pressed == 1 || btnStart.pressed == 1) { + PHL_PlaySound(sounds[sndOk], 1); + return cursor; + } + + return -1; +} + +void titleScreenDraw() +{ + //Blackdrop + PHL_DrawRect(0, 0, 640, 480, PHL_NewRGB(0, 0, 0)); + + if (tempsave == 0) { + //Title image + PHL_DrawSurfacePart(168, 72, 0, 0, 304, 168, images[imgTitle01]); + }else{ + //Save error message + drawTextCentered(saveError[0], 320, 80); + drawTextCentered(saveError[1], 320, 80 + 50); + drawTextCentered(saveError[2], 320, 80 + 96); + } + + //Cursor + PHL_DrawSurfacePart(228, 264 + (cursor * 32), 4, 176, 184, 32, images[imgTitle01]); + + //Text + PHL_DrawTextBold("NEW GAME", 256, 272, YELLOW); + PHL_DrawTextBold("LOAD GAME", 248, 304, YELLOW); + PHL_DrawTextBold("EXIT", 288, 336, YELLOW); + PHL_DrawTextBold("(C) 2011 E.HASHIMOTO", 160, 400, WHITE); +} \ No newline at end of file diff --git a/src/titlescreen.h b/src/titlescreen.h new file mode 100644 index 0000000..d60aeca --- /dev/null +++ b/src/titlescreen.h @@ -0,0 +1,6 @@ +#ifndef TITLESCREEN_H +#define TITLESCREEN_H + +int titleScreen(); + +#endif \ No newline at end of file diff --git a/src/weapon.c b/src/weapon.c new file mode 100644 index 0000000..999c7b1 --- /dev/null +++ b/src/weapon.c @@ -0,0 +1,601 @@ +#include "weapon.h" +#include +#include +#include +#include "hero.h" +#include "PHL.h" +#include "game.h" +#include "object.h" + +void updateWeaponMask(Weapon* w); + +void addWeapon(int type, int x, int y) +{ + if (heroAmmo > 0 || type == SWORD) { + int i, weaponcount = 0; + + //Count + for (i = 0; i < MAX_WEAPONS; i++) { + if (weapons[i] != NULL) { + if (weapons[i]->type != SWORD) { + weaponcount += 1; + } + } + } + + if (type == BOMB) { //Bombs have one lower limit + weaponcount += 1; + } + + if (type == SWORD || weaponcount < (2 + hasItem[15])) { //3 projectiles if have green scroll + + if (type != SWORD) { + PHL_PlaySound(sounds[sndShot01], CHN_WEAPONS); + } + + for (i = 0; i < MAX_WEAPONS; i++) { + if (weapons[i] == NULL) { + Weapon* w = malloc(sizeof *w); + w->id = i; + w->type = type; + + w->x = x; + w->y = y; + + w->hsp = 0; + w->vsp = 0; + + w->grav = 0; + w->imageIndex = 0; + + w->power = 1; + w->timer = 0; + w->state = 0; + w->cooldown = 0; + w->hitflag = 0; + + w->dir = getHeroDirection(); + + w->weaponMask.circle = 0; + w->weaponMask.unused = 0; + w->weaponMask.x = 0; + w->weaponMask.y = 0; + w->weaponMask.w = 10; + w->weaponMask.h = 10; + + if (w->type == SWORD) + { + w->weaponMask.unused = 1; + }else if (w->type == ARROW) + { + w->hsp = 8 * getHeroDirection(); + w->weaponMask.y += 16; + w->weaponMask.w = 32; + w->weaponMask.h = 6; + }else if (w->type == AXE) + { + w->hsp = 2 * getHeroDirection(); + w->vsp = -6; + w->grav = 0.2; + w->weaponMask.w = w->weaponMask.h = 24; + w->y += (40 - getHeroMask().h) - ((40 - w->weaponMask.h) / 2); + w->weaponMask.x = w->x + ((40 - w->weaponMask.w) / 2); + w->weaponMask.y = w->y + ((40 - w->weaponMask.h) / 2); + + }else if (w->type == BOOMERANG) + { + w->x += 20; + w->y += 20; + updateWeaponMask(w); + w->weaponMask.circle = 1; + w->weaponMask.w = 16; + + w->hsp = 6 * getHeroDirection(); + w->vsp = getHeroDirection(); //Vsp is used for starting direction + w->timer = 120; + }else if (w->type == FIREBALL) + { + w->vsp = -2; + w->grav = 0.1; + + w->hsp = 2 * getHeroDirection(); + + w->x += 20; + w->y += 20; + + w->weaponMask.circle = 1; + w->weaponMask.w = 15; + updateWeaponMask(w); + + Mask tempMask; + tempMask.circle = tempMask.unused = 0; + tempMask.y = w->y - w->weaponMask.w; + tempMask.x = w->x - w->weaponMask.w; + tempMask.w = tempMask.h = w->weaponMask.w * 2; + + PHL_Rect collide = getTileCollision(1, tempMask); + if (collide.x == -1) { + collide = getTileCollision(3, tempMask); + } + if (collide.x != -1) { + w->y = collide.y + 40 + w->weaponMask.w; + } + + }else if (w->type == BOMB) + { + w->dir = 0; + w->vsp = -2; + w->grav = 0.1; + w->hsp = getHeroDirection(); + + w->weaponMask.unused = 1; + + Mask tempMask; + tempMask.y = w->y + 8; + tempMask.x = w->x + 8; + tempMask.w = tempMask.h = 24; + tempMask.circle = tempMask.unused = 0; + + PHL_Rect collide = getTileCollision(1, tempMask); + if (collide.x == -1) { + collide = getTileCollision(3, tempMask); + } + if (collide.x != -1) { + w->y = collide.y + 40 - 8; + } + } + + updateWeaponMask(w); + + weapons[i] = w; + i = MAX_WEAPONS; + + if (type != SWORD) { + heroAmmo -= 1; + } + } + } + } + } +} + +//When a weapon lands a hit +void weaponHit(Weapon* w) +{ + //w->cooldown = 15; + w->hitflag = 1; + + if (w->type == SWORD) { + createEffect(1, w->weaponMask.x + (w->weaponMask.w / 2) - 10 + (rand() % 20) - 10, w->weaponMask.y + (w->weaponMask.h / 2) - 10 + (rand() % 20) - 10); + PHL_PlaySound(sounds[sndHit02], CHN_WEAPONS); + }else if (w->type == ARROW || w->type == AXE) { + PHL_PlaySound(sounds[sndPi02], CHN_WEAPONS); + createEffect(1, w->x, w->y); + weaponDestroy(w->id); + }else if (w->type == BOOMERANG) { + PHL_PlaySound(sounds[sndPi02], CHN_WEAPONS); + createEffect(1, w->x, w->y - 40 + (rand() % 40) + 1); + }else if (w->type == FIREBALL) { + PHL_PlaySound(sounds[sndPi02], CHN_WEAPONS); + createEffect(1, w->x - 20, w->y - 20); + //weaponDestroy(w->id); + }else if (w->type == BOMB) { + + } +} + +void weaponStep(Weapon* w) +{ + char dead = 0; + + if (w->cooldown > 0) { + w->cooldown -= 1; + } + + if (w->hitflag == 1) { + w->hitflag = 0; + w->cooldown = 15; + } + + if (w->type == SWORD) + { + double imgind = getHeroImageIndex(); + if ((getHeroState() != 1 && getHeroState() != 5) || imgind >= 4) { //Not slashing + dead = 1; + } + + w->x = herox; + w->y = heroy; + updateWeaponMask(w); + + }else if (w->type == ARROW) + { + //Yes, this does have to be done before movement + //Destroy if out of view or collides with wall + if (w->x > 640 || w->x + 40 < 0 || checkTileCollision(1, w->weaponMask) == 1) { + weaponHit(w); + } + + w->x += w->hsp; + updateWeaponMask(w); + }else if (w->type == AXE) + { + w->y += w->vsp; + w->vsp += w->grav; + updateWeaponMask(w); + + PHL_Rect collide = getTileCollisionWeapon(1, w->weaponMask); + if (collide.x == -1) { + collide = getTileCollision(3, w->weaponMask); + } + + if (collide.x != -1) { + if (w->vsp <= 0) { + w->y = collide.y + 40 - ((40 - w->weaponMask.h) / 2); + w->vsp = 0; + }else{ + weaponHit(w); + } + } + + w->x += w->hsp; + updateWeaponMask(w); + + collide = getTileCollisionWeapon(1, w->weaponMask); + + if (collide.x != -1) { + if (w->hsp > 0) { + w->x = collide.x - 40 + ((40 - w->weaponMask.w) / 2); + }else{ + w->x = collide.x + 40 - ((40 - w->weaponMask.w) / 2); + } + } + + //Animate + { + w->imageIndex += 0.33; + if (w->imageIndex >= 8) { + w->imageIndex -= 8; + } + } + + if (w->x > 640 || w->x + 40 < 0 || w->y > 520) { + dead = 1; + } + }else if (w->type == BOOMERANG) + { + w->x += w->hsp; + w->hsp -= 0.125 * (w->vsp); //Vsp is recycled to be the starting direction + if (w->hsp >= 6) { + w->hsp = 6; + } + if (w->hsp <= -6) { + w->hsp = -6; + } + + w->dir = 1; + if (w->hsp < 0) { + w->dir = -1; + } + + w->y = heroy + 20; + updateWeaponMask(w); + + w->imageIndex -= (0.33 * w->vsp); + if (w->imageIndex < 0) { + w->imageIndex += 8; + } + if (w->imageIndex >= 8) { + w->imageIndex -= 8; + } + + w->timer -= 1; + if (w->timer <= 0) { + createEffect(5, w->x, w->y); + dead = 1; + } + }else if (w->type == FIREBALL) + { + //Move vertically + w->y += w->vsp; + w->vsp += w->grav; + + Mask tempMask; + tempMask.circle = tempMask.unused = 0; + tempMask.y = w->y - w->weaponMask.w; + tempMask.w = tempMask.h = w->weaponMask.w * 2; + tempMask.x = w->x - w->weaponMask.w; + + //Check vertical collision + PHL_Rect collide = getTileCollisionWeapon(1, tempMask); + if (collide.x == -1) { + collide = getTileCollisionWeapon(3, tempMask); + } + + if (collide.x != -1) { + if (w->vsp <= 0) { + w->y = collide.y + 40 + w->weaponMask.w; + w->vsp = 0; + }else{ + w->y = collide.y - w->weaponMask.w; + if (w->timer == 0) { + w->vsp = -2; + PHL_PlaySound(sounds[sndPi02], 2); + }else if (w->timer == 1) { + w->vsp = -1; + PHL_PlaySound(sounds[sndPi02], 2); + }else{ + weaponHit(w); + dead = 1; + } + w->timer += 1; + } + + tempMask.y = w->y - w->weaponMask.w; + } + + //Move horizontally + w->x += w->hsp; + tempMask.x = w->x - w->weaponMask.w; + + //Check horizontal collision + collide = getTileCollisionWeapon(1, tempMask); + if (collide.x != -1) { + if (w->hsp > 0) { + w->x = collide.x - (tempMask.w / 2) ; + }else{ + w->x = collide.x + 40 + (tempMask.w / 2); + } + w->hsp *= -1; + tempMask.x = w->x - w->weaponMask.w; + + w->dir = 1; + if (w->hsp < 0) { + w->dir = -1; + } + } + + updateWeaponMask(w); + + //animate + { + w->imageIndex += 0.5; + if (w->imageIndex >= 8) { + w->imageIndex -= 8; + } + } + + if (w->x > 640 || w->x + 40 < 0 || w->y > 520) { + dead = 1; + } + }else if (w->type == BOMB) + { + if (w->state == 0) { //Bouncing bomb + Mask tempMask; + tempMask.y = w->y + 8; + tempMask.w = tempMask.h = 24; + tempMask.circle = tempMask.unused = 0; + + w->x += w->hsp; + tempMask.x = w->x + 8; + PHL_Rect collide = getTileCollision(1, tempMask); + if (collide.x != -1) { + if (w->hsp > 0) { + w->x = collide.x - 40 + 8; + }else{ + w->x = collide.x + 40 - 8; + } + w->hsp *= -1; + tempMask.x = w->x + 8; + } + + w->imageIndex -= (0.33 * w->hsp); + if (w->imageIndex < 0) { + w->imageIndex += 8; + } + if (w->imageIndex >= 8) { + w->imageIndex -= 8; + } + + w->y += w->vsp; + w->vsp += w->grav; + + tempMask.y = w->y + 8; + collide = getTileCollision(1, tempMask); + if (collide.x == -1) { + collide = getTileCollision(3, tempMask); + } + if (collide.x != -1) { + if (w->vsp <= 0) { + w->y = collide.y + 40 - 8; + w->vsp = 0; + }else{ + w->y = collide.y - 40 + 8; + if (w->timer == 0) { + w->vsp = -2; + PHL_PlaySound(sounds[sndPi02], CHN_WEAPONS); + }else if (w->timer == 1) { + w->vsp = -1; + PHL_PlaySound(sounds[sndPi02], CHN_WEAPONS); + }else{ + w->state = 1; + w->imageIndex = 0; + w->weaponMask.unused = 0; + PHL_PlaySound(sounds[sndBom03], CHN_WEAPONS); + } + w->timer += 1; + } + } + + updateWeaponMask(w); + + if (w->x > 640 || w->x + 40 < 0 || w->y > 520) { + //weaponDestroy(w->id); + dead = 1; + } + } + else if (w->state == 1) { //Explosion + updateWeaponMask(w); + + if (checkCollision(getHeroMask(), w->weaponMask) == 1) { + heroHit(20, w->x + 20); + } + + w->imageIndex += 0.34; + if (w->imageIndex >= 11) { + //weaponDestroy(w->id); + dead = 1; + } + } + } + + if (dead == 1) { + weaponDestroy(w->id); + } +} + +void weaponDraw(Weapon* w) +{ + if (w->type == SWORD) { + //PHL_DrawMask(w->weaponMask); + + //Draw Sword + double imgind = getHeroImageIndex(); + int dir = getHeroDirection(); + if (imgind < 4) { + int swordx = 0, swordy = 0; + int scropx = 40 * (int)floor(imgind), + scropy = 240; + if (imgind < 1) { + swordy = -16; + swordx = -8; + }else if (imgind < 2) { + swordy = -8; + swordx = 24; + }else if (imgind < 4) { + swordy = 14; + swordx = 26; + } + + if (dir == -1) { + swordx *= -1; + scropx += 160; + } + + if (getHeroInvincible() % 2 == 0) { + PHL_DrawSurfacePart(herox - 20 + swordx, heroy + swordy, scropx, scropy, 40, 40, images[imgHero]); + } + + //PHL_DrawSurfacePart(w->x, w->y, 0, 240, 40, 40, images[imgHero]); + } + + } + else if (w->type == ARROW) + { + int dx = 240; + if (w->hsp <= 0) { + dx += 40; + } + PHL_DrawSurfacePart(w->x, w->y, dx, 200, 40, 40, images[imgMisc20]); + }else if (w->type == AXE) + { + int dx = 0; + if (w->hsp <= 0) { + dx = 320; + } + PHL_DrawSurfacePart(w->x, w->y, dx + ((int)w->imageIndex * 40), 240, 40, 40, images[imgMisc20]); + }else if (w->type == BOOMERANG) + { + PHL_DrawSurfacePart(w->x - 20, w->y - 20, 320 + ((int)w->imageIndex * 40), 160, 40, 40, images[imgMisc20]); + }else if (w->type == FIREBALL) + { + int dx = 0; + if (w->hsp <= 0) { + dx = 320; + } + PHL_DrawSurfacePart(w->x - 20, w->y - 20, dx + ((int)w->imageIndex * 40), 280, 40, 40, images[imgMisc20]); + }else if (w->type == BOMB) + { + if (w->state == 0) { + PHL_DrawSurfacePart(w->x, w->y, (int)w->imageIndex * 40, 160, 40, 40, images[imgMisc20]); + } + else if (w->state == 1) { + //PHL_DrawMask(w->weaponMask); + + int cx = (int)w->imageIndex * 128; + int cy = 0; + while (cx >= 640) { + cx -= 640; + cy += 96; + } + PHL_DrawSurfacePart(w->weaponMask.x, w->weaponMask.y, cx, cy, 128, 96, images[imgExplosion]); + } + } + //PHL_DrawMask(w->weaponMask); +} + +void updateWeaponMask(Weapon* w) +{ + if (w->type == SWORD) { + //Sword mask + //swordMask.unused = 0; + double imgind = getHeroImageIndex(); + int dir = getHeroDirection(); + w->weaponMask.unused = 0; + + if (imgind < 1) { + /*w->weaponMask.w = 8; + w->weaponMask.h = 24; + w->weaponMask.x = herox - 4 + (dir * -8); //herox - 20 + (direction * -8) + 16 + w->weaponMask.y = heroy - 8; //heroy - 16 + 8 + */ + w->weaponMask.unused = 1; + }else if (imgind < 2) { + w->weaponMask.w = 32; + w->weaponMask.h = 38; + w->weaponMask.x = herox - 20 + (dir * 28) + 4; + w->weaponMask.y = heroy - 6; + }else if (imgind < 3) { + w->weaponMask.w = 24; + w->weaponMask.h = 20; + w->weaponMask.x = herox - 20 + (dir * 26) + 8; + w->weaponMask.y = heroy + 18; + }else if (imgind < 4) { + w->weaponMask.w = 24; + w->weaponMask.h = 6; + w->weaponMask.x = herox - 20 + (dir * 26) + 8; + w->weaponMask.y = heroy + 30; + } + }else if (w->type == ARROW) { + w->weaponMask.x = w->x; + w->weaponMask.y = w->y + 16; + if (w->hsp > 0) { + w->weaponMask.x += 8; + } + + }else if (w->type == AXE) { + w->weaponMask.x = w->x + ((40 - w->weaponMask.w) / 2); + w->weaponMask.y = w->y + ((40 - w->weaponMask.h) / 2); + }else if (w->type == BOOMERANG) { + w->weaponMask.x = w->x; + w->weaponMask.y = w->y; + }else if (w->type == FIREBALL) { + w->weaponMask.x = w->x; + w->weaponMask.y = w->y; + }else if (w->type == BOMB) { + if (w->state == 1) { //Update mask on explosion + w->weaponMask.x = w->x - 44; + w->weaponMask.y = w->y - 44 - 8; + w->weaponMask.w = 128; + w->weaponMask.h = 90; //Hits blocks below + } + } +} + +void weaponDestroy(int id) +{ + if (weapons[id] != NULL) { + free(weapons[id]); + } + weapons[id] = NULL; +} \ No newline at end of file diff --git a/src/weapon.h b/src/weapon.h new file mode 100644 index 0000000..5f47be6 --- /dev/null +++ b/src/weapon.h @@ -0,0 +1,34 @@ +#ifndef WEAPON_H +#define WEAPON_H + +#include "collision.h" + +#define ARROW 0 +#define AXE 1 +#define BOOMERANG 2 +#define FIREBALL 3 +#define BOMB 4 +#define SWORD 5 + +typedef struct { + int id, type; + + double x, y; + double vsp, hsp; + double grav; + double imageIndex; + int dir; + + int power, timer, state, cooldown, hitflag; + + Mask weaponMask; +} Weapon; + +void addWeapon(int type, int x, int y); +void weaponStep(Weapon* w); +void weaponDraw(Weapon* w); + +void weaponHit(Weapon* w); +void weaponDestroy(int id); + +#endif \ No newline at end of file diff --git a/src/wii/audio.c b/src/wii/audio.c new file mode 100644 index 0000000..d60242b --- /dev/null +++ b/src/wii/audio.c @@ -0,0 +1,98 @@ +#include "audio.h" + +void PHL_AudioInit() +{ + //oslInitAudio(); + if (Mix_OpenAudio(MIX_DEFAULT_FREQUENCY, MIX_DEFAULT_FORMAT, 1, 4096)) + { + fprintf(stderr, "SDL_Mixer Error: %s\n", Mix_GetError()); + exit(EXIT_FAILURE); + } +} + +void PHL_AudioClose() +{ + Mix_CloseAudio(); +} + +//Each system can use a custom music file extension +PHL_Music PHL_LoadMusic(char* fname, int loop) +{ + PHL_Music result; + result.loop = loop; + + char fullPath[40]; + strcpy(fullPath, fname); + strcat(fullPath, ".ogg"); + + FILE* file; + if ((file = fopen(fullPath, "r"))) { + fclose(file); + + result.data = Mix_LoadMUS(fullPath); + result.used = 1; + }else{ + result.used = 0; + } + + return result; +} + +//PHL_Sound PHL_LoadSound(char* fname, int loop, int stream, int channel) +PHL_Sound PHL_LoadSound(char* fname) +{ + PHL_Sound result; + + FILE* file; + if ((file = fopen(fname, "r"))) { + fclose(file); + + result.data = Mix_LoadWAV(fname); + result.used = 1; + }else{ + result.used = 0; + } + + return result; +} + +void PHL_PlayMusic(PHL_Music snd) +{ + if (snd.used == 1) { + int loop = 1; + if (snd.loop == 1) { + loop = -1; + } + Mix_PlayMusic(snd.data, loop); + } +} + +//void PHL_PlaySound(PHL_Sound snd) +void PHL_PlaySound(PHL_Sound snd, int channel) +{ + if (snd.used == 1) { + Mix_PlayChannel(channel, snd.data, 0); + } +} + +void PHL_StopMusic() +{ + Mix_HaltMusic(); +} + +void PHL_StopSound(PHL_Sound snd, int channel) +{ + +} + +void PHL_FreeMusic(PHL_Music snd) +{ + Mix_FreeMusic(snd.data); + snd.used = 0; +} + +void PHL_FreeSound(PHL_Sound snd) +{ + Mix_FreeChunk(snd.data); + snd.used = 0; +} \ No newline at end of file diff --git a/src/wii/audio.h b/src/wii/audio.h new file mode 100644 index 0000000..ec32470 --- /dev/null +++ b/src/wii/audio.h @@ -0,0 +1,35 @@ +#ifndef WIIAUDIO_H +#define WIIAUDIO_H + +#include + +typedef struct { + Mix_Music* data; + + int loop; + int used; +} PHL_Music; + +typedef struct { + Mix_Chunk* data; + + int loop; + int used; +} PHL_Sound; + +void PHL_AudioInit(); +void PHL_AudioClose(); + +PHL_Music PHL_LoadMusic(char* fname, int loop); //Same as PHL_LoadSound, but expects a file name without extension +PHL_Sound PHL_LoadSound(char* fname); + +void PHL_PlayMusic(PHL_Music snd); +void PHL_PlaySound(PHL_Sound snd, int channel); + +void PHL_StopMusic(); +void PHL_StopSound(PHL_Sound snd, int channel); + +void PHL_FreeMusic(PHL_Music snd); +void PHL_FreeSound(PHL_Sound snd); + +#endif \ No newline at end of file diff --git a/src/wii/graphics.c b/src/wii/graphics.c new file mode 100644 index 0000000..bd6e4cf --- /dev/null +++ b/src/wii/graphics.c @@ -0,0 +1,290 @@ +//Wii graphics.c +#include "graphics.h" +#include "../PHL.h" +#include "../game.h" +#include "../qda.h" +#include + +//One time graphics setup +void PHL_GraphicsInit() +{ + if ( SDL_Init(SDL_INIT_VIDEO | SDL_INIT_AUDIO) < 0) { + fprintf(stderr, "Error"); + SDL_Delay(5000); + exit(EXIT_FAILURE); + } + + SDL_ShowCursor(SDL_DISABLE); + + surfScreen = SDL_SetVideoMode(640, 480, 16, SDL_HWSURFACE|SDL_DOUBLEBUF); + backBuffer = PHL_NewSurface(640, 480); + db.fb = surfScreen; +} + +void PHL_GraphicsExit() +{ + SDL_FreeSurface(surfScreen); + SDL_Quit(); + //closeQDA(); +} + +void PHL_StartDrawing() +{ + /*int i; + for (i = 0; i < 10; i++) { + xfb[i] = 0xff; + }*/ +} + +void PHL_EndDrawing() +{ + SDL_Flip(surfScreen); + //frameLimit(); + //VIDEO_Flush(); + //VIDEO_WaitVSync(); + //frameLimit(); +} + +void PHL_ForceScreenUpdate() +{ + +} + +void PHL_SetDrawbuffer(PHL_Surface surf) +{ + db.fb = surf.surf; +} + +void PHL_ResetDrawbuffer() +{ + db.fb = surfScreen; +} + +PHL_RGB PHL_NewRGB(int r, int g, int b) { + PHL_RGB col; + col.r = (unsigned int)r; + col.g = (unsigned int)g; + col.b = (unsigned int)b; + return col; +} + +void PHL_SetColorKey(PHL_Surface surf, int r, int g, int b) +{ + Uint32 colorKey = SDL_MapRGB(surf.surf->format, r, g, b); + SDL_SetColorKey(surf.surf, SDL_SRCCOLORKEY, colorKey); +} + +PHL_Surface PHL_NewSurface(int w, int h) +{ + PHL_Surface surf; + surf.surf = SDL_CreateRGBSurface(0, w, h, 32, 0, 0, 0, 0); + return surf; +} + +void PHL_FreeSurface(PHL_Surface surface) +{ + SDL_FreeSurface(surface.surf); +} + +PHL_Surface PHL_LoadBMP(int index) +{ + PHL_Surface surf; + + FILE* f; + if ( (f = fopen("bmp.qda", "rb")) ) { + unsigned char* QDAFile = (unsigned char*)malloc(headers[index].size); + fseek(f, headers[index].offset, SEEK_SET); + fread(QDAFile, 1, headers[index].size, f); + fclose(f); + + FILE* temp; + if ( (temp = fopen("temp.bmp", "wb")) ) { + fwrite(QDAFile, sizeof(unsigned char*), sizeof(QDAFile), temp); + } + fclose(temp); + + PHL_RGB palette[20][18]; + + unsigned short w, h; + + //Read data from header + memcpy(&w, &QDAFile[18], 2); + memcpy(&h, &QDAFile[22], 2); + + surf = PHL_NewSurface(w * 2, h * 2); + //surf = PHL_NewSurface(200, 200); + + //Load Palette + int dx, dy; + int count = 0; + for (dx = 0; dx < 20; dx++) { + for (dy = 0; dy < 16; dy++) { + palette[dx][dy].b = QDAFile[54 + count]; + palette[dx][dy].g = QDAFile[54 + count + 1]; + palette[dx][dy].r = QDAFile[54 + count + 2]; + count += 4; + } + } + + for (dx = w; dx > 0; dx--) { + for (dy = 0; dy < h; dy++) { + + int pix = w - dx + w * dy; + int px = QDAFile[1078 + pix] / 16; + int py = QDAFile[1078 + pix] % 16; + + PHL_RGB c = palette[px][py]; + + //PHL_DrawRect(dx * 2, dy * 2, 2, 2, c); + SDL_Rect rect = {dx * 2, dy * 2, 2, 2}; + SDL_FillRect(surf.surf, &rect, SDL_MapRGB(surf.surf->format, c.r, c.g, c.b)); + } + } + free(QDAFile); + } + + fclose(f); + + return surf; +} + +/* +PHL_Surface PHL_LoadBMP(char* fname) +{ + SDL_Surface* temp; + SDL_Surface* stretchX; + SDL_Surface* final; + + temp = SDL_LoadBMP(fname); + int width = temp->w; + int height = temp->h; + + stretchX = SDL_CreateRGBSurface(0, width * 2, height, 32, 0, 0, 0, 0); + + SDL_Rect crop, offset; + offset.y = 0; + crop.y = 0; + crop.w = 1; + crop.h - height; + + int i; + for (i = 0; i < width; i++) + { + crop.x = i; + offset.x = i * 2; + + SDL_BlitSurface(temp, &crop, stretchX, &offset); + offset.x += 1; + SDL_BlitSurface(temp, &crop, stretchX, &offset); + } + + final = SDL_CreateRGBSurface(0, width * 2, height * 2, 32, 0, 0, 0, 0); + offset.x = 0; offset.y = 0; + crop.x = 0; crop.y = 0; + crop.w = width * 2; + crop.h = 1; + + for (i = 0; i < height; i++) + { + crop.y = i; + offset.y = i * 2; + + SDL_BlitSurface(stretchX, &crop, final, &offset); + offset.y += 1; + SDL_BlitSurface(stretchX, &crop, final, &offset); + } + + //Cleanup + SDL_FreeSurface(temp); + SDL_FreeSurface(stretchX); + + PHL_Surface result; + result.surf = SDL_DisplayFormat(final); + + return result; +} +*/ + +void PHL_DrawRect(int x, int y, int w, int h, PHL_RGB col) { + SDL_Rect rect = {x, y, w, h}; + + SDL_FillRect(db.fb, &rect, SDL_MapRGB(db.fb->format, col.r, col.g, col.b)); +} + +void PHL_DrawSurface(double x, double y, PHL_Surface surface) +{ + if (quakeTimer > 0) { + int val = quakeTimer % 4; + if (val == 0) { + y -= 1; + }else if (val == 2) { + y += 1; + } + } + + SDL_Rect offset; + offset.x = x; + offset.y = y; + + SDL_BlitSurface(surface.surf, NULL, db.fb, &offset); +} + +void PHL_DrawSurfacePart(double x, double y, int cropx, int cropy, int cropw, int croph, PHL_Surface surface) +{ + if (quakeTimer > 0) { + int val = quakeTimer % 4; + if (val == 0) { + y -= 1; + }else if (val == 2) { + y += 1; + } + } + + SDL_Rect crop, offset; + crop.x = cropx; + crop.y = cropy; + crop.w = cropw; + crop.h = croph; + + offset.x = x; + offset.y = y; + + SDL_BlitSurface(surface.surf, &crop, db.fb, &offset); +} + +void PHL_DrawBackground(PHL_Background back, PHL_Background fore) +{ + PHL_DrawSurface(0, 0, backBuffer); +} + +void PHL_UpdateBackground(PHL_Background back, PHL_Background fore) +{ + PHL_SetDrawbuffer(backBuffer); + + int xx, yy; + + for (yy = 0; yy < 12; yy++) + { + for (xx = 0; xx < 16; xx++) + { + //Draw Background tiles + PHL_DrawSurfacePart(xx * 40, yy * 40, back.tileX[xx][yy] * 40, back.tileY[xx][yy] * 40, 40, 40, images[imgTiles]); + + //Only draw foreground tile if not a blank tile + if (fore.tileX[xx][yy] != 0 || fore.tileY[xx][yy] != 0) { + PHL_DrawSurfacePart(xx * 40, yy * 40, fore.tileX[xx][yy] * 40, fore.tileY[xx][yy] * 40, 40, 40, images[imgTiles]); + } + } + } + + PHL_ResetDrawbuffer(); +} + +/*void frameLimit() +{ + currentTime = SDL_GetTicks() - lastTime; + if (currentTime < FRAMEWAIT) { + SDL_Delay(FRAMEWAIT - currentTime); + } + lastTime = SDL_GetTicks(); +}*/ \ No newline at end of file diff --git a/src/wii/graphics.h b/src/wii/graphics.h new file mode 100644 index 0000000..27794cf --- /dev/null +++ b/src/wii/graphics.h @@ -0,0 +1,61 @@ +//Wii graphics.h +#ifndef GRAPHICS_H +#define GRAPHICS_H + +//#include +#include + +typedef struct { + int tileX[16][12]; + int tileY[16][12]; +} PHL_Background; + +typedef struct { + unsigned int r; + unsigned int g; + unsigned int b; +} PHL_RGB; + +typedef struct { + SDL_Surface* fb; + int width; + int height; +} DrawBuffer; + +typedef struct { + SDL_Surface* surf; +} PHL_Surface; + +PHL_Surface backBuffer; +SDL_Surface *surfScreen; +DrawBuffer db; + +void PHL_GraphicsInit(); +void PHL_GraphicsExit(); + +void PHL_StartDrawing(); +void PHL_EndDrawing(); + +void PHL_ForceScreenUpdate(); + +void PHL_SetDrawbuffer(PHL_Surface surf); +void PHL_ResetDrawbuffer(); + +PHL_RGB PHL_NewRGB(int r, int g, int b); +void PHL_SetColorKey(PHL_Surface surf, int r, int g, int b); + +PHL_Surface PHL_NewSurface(int w, int h); +void PHL_FreeSurface(PHL_Surface surface); + +//PHL_Surface PHL_LoadBMP(char* fname); +PHL_Surface PHL_LoadBMP(int index); + +void PHL_DrawRect(int x, int y, int w, int h, PHL_RGB col); + +void PHL_DrawSurface(double x, double y, PHL_Surface surface); +void PHL_DrawSurfacePart(double x, double y, int cropx, int cropy, int cropw, int croph, PHL_Surface surface); + +void PHL_DrawBackground(PHL_Background back, PHL_Background fore); +void PHL_UpdateBackground(PHL_Background back, PHL_Background fore); + +#endif \ No newline at end of file diff --git a/src/wii/input.c b/src/wii/input.c new file mode 100644 index 0000000..53b9400 --- /dev/null +++ b/src/wii/input.c @@ -0,0 +1,162 @@ +//Wii +#include "input.h" +#include +#include + +double deadZone = 0.5; + +void updateKey(Button* btn, int state); + +void PHL_ScanInput() +{ + //btnStartPrev = btnStart; + + WPAD_ScanPads(); + //PAD_ScanPads(); + + u32 pressed = WPAD_ButtonsHeld(0); + //u32 gPressed = PAD_ButtonsHeld(0); + + int pUp = 0, pDown = 0, pLeft = 0, pRight = 0; + int pFaceLeft = 0, pFaceRight = 0, /*pFaceUp = 0,*/ pFaceDown = 0; + int pL = 0, pR = 0; + int pSelect = 0, pStart = 0; + int pAccept = 0, pDecline = 0; + + //if (pressed & WPAD_BUTTON_RIGHT || pressed & WPAD_CLASSIC_BUTTON_UP || gPressed & PAD_BUTTON_RIGHT) { pUp = 1; } + WPADData* cData = WPAD_Data(0); + + if (cData->exp.type == WPAD_EXP_NONE) + { //Normal Wiimote*/ + pUp = pressed & WPAD_BUTTON_RIGHT; + pDown = pressed & WPAD_BUTTON_LEFT; + pLeft = pressed & WPAD_BUTTON_UP; + pRight = pressed & WPAD_BUTTON_DOWN; + + pSelect = pressed & WPAD_BUTTON_MINUS; + pStart = pressed & WPAD_BUTTON_PLUS; + + pR = pressed & WPAD_BUTTON_A; + + pFaceLeft = pressed & WPAD_BUTTON_1; + pFaceDown = pressed & WPAD_BUTTON_2; + pFaceRight = pressed & WPAD_BUTTON_B; + + pAccept = pFaceDown; + pDecline = pFaceLeft; + }else if (cData->exp.type == WPAD_EXP_CLASSIC) + { //Classic Controller + //Get x and y axis from the joystick + //Taken from the Wii SDL source + float mag = 0.0, + ang = 0.0; + + mag = cData->exp.classic.ljs.mag; + ang = cData->exp.classic.ljs.ang; + + double xAxis = mag * sin((3.14159 * ang)/180.0f); + double yAxis = mag * cos((3.14159 * ang)/180.0f); + + if (yAxis < -deadZone || pressed & WPAD_CLASSIC_BUTTON_DOWN) { pDown = 1; } + if (yAxis > deadZone || pressed & WPAD_CLASSIC_BUTTON_UP) { pUp = 1; } + if (xAxis < -deadZone || pressed & WPAD_CLASSIC_BUTTON_LEFT) { pLeft = 1; } + if (xAxis > deadZone || pressed & WPAD_CLASSIC_BUTTON_RIGHT) { pRight = 1; } + + if (pUp == 1 && pDown == 1) { + pUp = 0; + pDown = 0; + } + if (pLeft == 1 && pRight == 1) { + pLeft = 0; + pRight = 0; + } + + pSelect = pressed & WPAD_CLASSIC_BUTTON_MINUS; + pStart = pressed & WPAD_CLASSIC_BUTTON_PLUS; + + pL = pressed & WPAD_CLASSIC_BUTTON_FULL_L; + pR = pressed & WPAD_CLASSIC_BUTTON_FULL_R; + + pFaceLeft = pressed & WPAD_CLASSIC_BUTTON_Y; + pFaceDown = pressed & WPAD_CLASSIC_BUTTON_B; + pFaceRight = pressed & WPAD_CLASSIC_BUTTON_A; + + pAccept = pFaceRight; + pDecline = pFaceDown; + } + + //Gamecube Controller + { + PAD_ScanPads(); + pressed = PAD_ButtonsHeld(0); + + double xAxis = PAD_StickX(0) / 25; + double yAxis = PAD_StickY(0) / 25; + + if (pressed & PAD_BUTTON_UP || yAxis > deadZone) { pUp = 1; } + if (pressed & PAD_BUTTON_DOWN || yAxis < -deadZone) { pDown = 1; } + if (pressed & PAD_BUTTON_LEFT || xAxis < -deadZone) { pLeft = 1; } + if (pressed & PAD_BUTTON_RIGHT || xAxis > deadZone) { pRight = 1; } + + if (pUp == 1 && pDown == 1) { + pUp = 0; + pDown = 0; + } + if (pLeft == 1 && pRight == 1) { + pLeft = 0; + pRight = 0; + } + + if (pressed & PAD_BUTTON_START) { pStart = 1; } + if (pressed & PAD_BUTTON_X) { pSelect = 1; } + + if (pressed & PAD_BUTTON_B) { pFaceLeft = 1; } + if (pressed & PAD_BUTTON_A) { pFaceDown = 1; } + if (pressed & PAD_BUTTON_Y) { pFaceRight = 1; } + + if (pressed & PAD_TRIGGER_L) { pL = 1; } + if (pressed & PAD_TRIGGER_R) { pR = 1; } + + pAccept = pFaceDown; + pDecline = pFaceLeft; + } + + updateKey(&btnUp, pUp); + updateKey(&btnDown, pDown); + updateKey(&btnLeft, pLeft); + updateKey(&btnRight, pRight); + + updateKey(&btnStart, pStart); + updateKey(&btnSelect, pSelect); + + updateKey(&btnL, pL); + updateKey(&btnR, pR); + + updateKey(&btnFaceLeft, pFaceLeft); + updateKey(&btnFaceDown, pFaceDown); + updateKey(&btnFaceRight, pFaceRight); + + updateKey(&btnAccept, pAccept); + updateKey(&btnDecline, pDecline); +} + +void updateKey(Button* btn, int state) +{ + if (state) { + if (btn->held == 1) { + btn->pressed = 0; + }else{ + btn->pressed = 1; + } + btn->held = 1; + btn->released = 0; + }else{ + if (btn->held == 1) { + btn->released = 1; + }else{ + btn->released = 0; + } + btn->held = 0; + btn->pressed = 0; + } +} \ No newline at end of file diff --git a/src/wii/input.h b/src/wii/input.h new file mode 100644 index 0000000..cf76e0e --- /dev/null +++ b/src/wii/input.h @@ -0,0 +1,20 @@ +#ifndef INPUT_H +#define INPUT_H + +typedef struct { + int pressed, + held, + released; +} Button; + +Button btnUp, btnDown, btnLeft, btnRight; +Button btnFaceUp, btnFaceDown, btnFaceLeft, btnFaceRight; +Button btnL, btnR; +Button btnStart, btnSelect; +Button btnAccept, btnDecline; + +int axisX, axisY; + +void PHL_ScanInput(); + +#endif \ No newline at end of file diff --git a/src/wii/system.c b/src/wii/system.c new file mode 100644 index 0000000..d28c5e1 --- /dev/null +++ b/src/wii/system.c @@ -0,0 +1,47 @@ +#include "system.h" +#include +#include +#include + +int quitGame = 0; + +int PHL_MainLoop() +{ + if (quitGame == 1) { + return 0; + } + return 1; +} + +void PHL_ConsoleInit() +{ + //console_init(xfb,20,20,rmode->fbWidth,rmode->xfbHeight,rmode->fbWidth*VI_DISPLAY_PIX_SZ); +} + +void PHL_GameQuit() +{ + quitGame = 1; +} + +void PHL_ErrorScreen(char* message) +{ + /*consoleInit(GFX_TOP, NULL); + + printf(message); + printf("\n\nPress START to close"); + + u32 kDown; + while (PHL_MainLoop()) { + //gfxFlushBuffers(); + //gfxSwapBuffers(); + hidScanInput(); + kDown = hidKeysHeld(); + + if (kDown & KEY_START) { break; } + + gspWaitForVBlank(); + } + */ + + exit(1); +} \ No newline at end of file diff --git a/src/wii/system.h b/src/wii/system.h new file mode 100644 index 0000000..d88444f --- /dev/null +++ b/src/wii/system.h @@ -0,0 +1,12 @@ +#ifndef SYSTEM_H +#define SYSTEM_H + +int quitGame; + +int PHL_MainLoop(); +void PHL_ConsoleInit(); +void PHL_GameQuit(); + +void PHL_ErrorScreen(char* message); + +#endif \ No newline at end of file -- cgit v1.2.3