#include "enemies.h" #include "renderer.h" #include // för fmod() GameObject heart[100], coin[100], alien[30], apples[100]; static void updateEnemies(GameObject* this); static void renderEnemies(GameObject* this); static void init(GameObject* pObj, GfxObject* pGfx, int x, int y, float startFrame, float frameSpeed, int numFrames) { pObj->gfxObj = *pGfx; pObj->gfxObj.outputWidth = 100; pObj->gfxObj.outputHeight = 100; pObj->pos.x = x; pObj->pos.y = y; pObj->speed = 0; pObj->angle = 0; // unused pObj->angleSpeed = 0; // unused pObj->scale = 1.0f; pObj->scaleSpeed = 0; // unused pObj->update = updateEnemies; pObj->render = renderEnemies; pObj->frame = startFrame; pObj->frameSpeed = frameSpeed; pObj->numFrames = numFrames; } void createApple() { static GfxObject gfx; static bool bFirstTime = true; if(bFirstTime) { bFirstTime = false; gfx = createGfxObject( "../apples25st.png" ); } init(&apples[0], &gfx, 100, 100, 0, 0.5f, 25); gameObjects[nGameObjects++] = &apples[0]; } void createHearts() { static GfxObject gfxHeart; static bool bFirstTime = true; // It is unnecesary to load the heart image for every instance. Just do it once: if(bFirstTime) { bFirstTime = false; // Create a GfxObject for the heart image gfxHeart = createGfxObject( "../fireheart_new.png" ); //gfxHeart = createGfxObject( "../explosion50st.png" ); } init(&heart[0], &gfxHeart, 100, 100, 0, 0.5f, 10); init(&heart[1], &gfxHeart, 200, 100, 3, 0.5f, 10); init(&heart[2], &gfxHeart, 300, 100, 6, 0.5f, 10); gameObjects[nGameObjects++] = &heart[0]; gameObjects[nGameObjects++] = &heart[1]; gameObjects[nGameObjects++] = &heart[2]; } void createCoins() { static GfxObject gfx; static bool bFirstTime = true; // It is unnecesary to load the heart image for every instance. Just do it once: if(bFirstTime) { bFirstTime = false; // Create a GfxObject for the heart image gfx = createGfxObject( "../coinstrip40st.png" ); //gfx = createGfxObject( "../apples25st.png" ); //gfx = createGfxObject( "../alienship.png" ); } float frameSpeed = 1.0f; init(&coin[0], &gfx, 400, 100, 0, frameSpeed, 40); init(&coin[1], &gfx, 500, 100, 10, frameSpeed, 40); init(&coin[2], &gfx, 600, 100, 20, frameSpeed, 40); init(&coin[3], &gfx, 700, 100, 30, frameSpeed, 40); gameObjects[nGameObjects++] = &coin[0]; gameObjects[nGameObjects++] = &coin[1]; gameObjects[nGameObjects++] = &coin[2]; gameObjects[nGameObjects++] = &coin[3]; } static void updateEnemies(GameObject* this) { // update which frame in texture to show this->frame = fmod( (this->frame+this->frameSpeed), this->numFrames ); // update positions static int iter = 0; int duration = 200; float ampl = 100.0f, t; iter++; // this frame int frame = iter + (this->pos.x) / 3.5f; t = (frame % duration) / (float)(duration-1); // t: 0 -> duration float current_y = ampl*( fabs(2.0f*(3*t*t - 2*t*t*t) -1)); // snott fr. uppg.3 v1. // previous frame t = ((frame-1) % duration) / (float)(duration-1); float previous_y = ampl*( fabs(2.0f*(3*t*t - 2*t*t*t) -1)); // Compute delta step in y-direction float delta_y = current_y - previous_y; this->pos.y += delta_y; } static void renderEnemies(GameObject* this) { if(this->status != DEAD) { // Render a sub frame to screen // Here, we know that the heart image is a sprite sheet with 10 frames // simply outlined next to each other in the x-direction. // Thus, the x-coordinate for the current frame is easy to compute: int w = this->gfxObj.textureWidth / this->numFrames; int x = w * (int)(this->frame); SDL_Rect srcRect = { x, 0, w, this->gfxObj.textureHeight }; // select the frame image renderGfxSubFrame(&this->gfxObj, this->pos.x, this->pos.y, this->angle, this->scale, srcRect ); } }