Section* Section_new(double x, double y, double z, double width, double height, double length, int enemies) { Section* inst = (Section*) malloc(sizeof(Section)); int i; Vector spawn; double spacing; inst->pos[0] = x, inst->pos[1] = y, inst->pos[2] = z; inst->size[0] = width, inst->size[1] = height, inst->size[2] = length; inst->entities = List_new(); for(i = 0, spacing = 0; i < enemies; ++i) { spawn[0] = randomInterval(x, x + width - Enemy_DEF_SIZE[0]); spawn[1] = randomInterval(y, y + height - Enemy_DEF_SIZE[1]); spawn[2] = randomInterval(z + spacing, z + length); List_pushBack(inst->entities, Enemy_new( spawn[0], spawn[1], spawn[2], random(), random() * 5 + 1, 800)); spacing += Enemy_DEF_SIZE[2] + length/enemies; } return inst; }
Healthbar* Healthbar_create(TextureCache* tc) { Healthbar* this = malloc(sizeof(Healthbar)); this->background = Sprite_create(TextureCache_get(tc, "images/healthbarBg.png")); AnimatedSprite* sprite = AnimatedSprite_create(Sprite_create(TextureCache_get(tc, "images/healthbar.png"))); Animation* idleAnimation = Animation_create("idle", true); List_pushBack(idleAnimation->frames, Frame_create(0, 0, 32, 128, 3000)); List_pushBack(idleAnimation->frames, Frame_create(32,0, 32, 128, 3000)); List_pushBack(idleAnimation->frames, Frame_create(64,0, 32, 128, 3000)); List_pushBack(idleAnimation->frames, Frame_create(96,0, 32, 128, 3000)); List_pushBack(idleAnimation->frames, Frame_create(128,0, 32, 128, 3000)); List_pushBack(sprite->animations, idleAnimation); AnimatedSprite_setFrame(sprite, ((Frame*)idleAnimation->frames->first->data)->rect); sprite->progress.animation = idleAnimation; this->liquid = sprite; this->fullHeight = this->liquid->sprite->bounds.h; Healthbar_set(this, 100, 100); return this; }
Enemy* Enemy_createWhiteKnight(Scene* scene, SDL_Point tilePos) { Enemy* this = malloc(sizeof(Enemy)); AnimatedSprite* sprite = AnimatedSprite_create(Sprite_create(TextureCache_get(scene->engine->textureCache, "images/whiteKnight.png"))); Animation* idleAnimation = Animation_create("idle", true); List_pushBack(idleAnimation->frames, Frame_create(0,0, 32, 32, 200)); List_pushBack(idleAnimation->frames, Frame_create(32,0, 32, 32, 200)); List_pushBack(idleAnimation->frames, Frame_create(64,0, 32, 32, 200)); List_pushBack(idleAnimation->frames, Frame_create(32,0, 32, 32, 200)); List_pushBack(sprite->animations, idleAnimation); Animation* attack1PrepareAnimation = Animation_create(ANIMATION_PREPARE_ATTACK1, false); List_pushBack(attack1PrepareAnimation->frames, Frame_create(0, 32, 32, 32, 60)); List_pushBack(attack1PrepareAnimation->frames, Frame_create(32,32, 32, 32, 60)); List_pushBack(attack1PrepareAnimation->frames, Frame_create(0, 32, 32, 32, 60)); List_pushBack(sprite->animations, attack1PrepareAnimation); Animation* attack1Animation = Animation_create(ANIMATION_ATTACK1, false); List_pushBack(attack1Animation->frames, Frame_create(64,32, 36, 32, 120)); List_pushBack(sprite->animations, attack1Animation); AnimatedSprite_setFrame(sprite, ((Frame*)idleAnimation->frames->first->data)->rect); sprite->progress.animation = idleAnimation; sprite->sprite->flip = false; Entity* entity = Entity_create(this, scene, sprite); entity->draw = Enemy_draw; entity->update = Enemy_update; entity->destroy = Enemy_destroy; entity->physics.bounds.x = tilePos.x * TILE_W * PHYSICS_SCALE; entity->physics.bounds.y = tilePos.y * TILE_H * PHYSICS_SCALE; entity->physics.bounds.w = 15 * PHYSICS_SCALE; // Make sure Mr. Fatty get's though those slim trapdoors entity->physics.bounds.h = 15 * PHYSICS_SCALE; entity->physics.belongsToGroups = COLLISION_GROUP_ENEMY; entity->physics.collidesWithGroupMask = COLLISION_GROUP_TERRAIN | COLLISION_GROUP_PLAYER; entity->offset.x = -10; entity->offset.y = -17; this->entity = entity; return this; }
DungeonMasterUi* DungeonMasterUi_create(struct Player* player) { DungeonMasterUi* this = malloc(sizeof(DungeonMasterUi)); this->cards = List_create(); SDL_Renderer* renderer = player->scene->engine->renderer; TextureCache* tc = player->scene->engine->textureCache; this->player = player; this->background.x = player->scene->camera->translation.x; this->background.y = player->scene->camera->translation.y; this->background.w = player->scene->background->bounds.w; this->background.h = player->scene->background->bounds.h; this->cardFont = TTF_OpenFont("fonts/Black-Chancery.ttf", 16); SDL_Point p = {.x = player->scene->camera->translation.x, .y=250}; List_pushBack(this->cards, Card_create(p, renderer, tc, "images/uiWhiteKnight.png", CARD_SUMMON_WHITKNIGHT, 10, this->cardFont, "Summon Skeleton Knight")); p.x += ((Card*)this->cards->first->data)->background->bounds.w + 5; List_pushBack(this->cards, Card_create(p, renderer, tc, "images/blue/cursor.png", CARD_CONTROL_STRONGEST, 5, this->cardFont, "Control strongest enemy")); p.x += ((Card*)this->cards->first->data)->background->bounds.w + 5; //List_pushBack(this->cards, Card_create(renderer, tc, "images/uiWhiteKnight.png", CARD_SUMMON_WHITKNIGHT, 15, this->cardFont, "Summon Skeleton Knight")); SDL_Color white = {.r=255, .g=255, .b=255, .a=255}; p.x = player->scene->camera->translation.x; char hintActionHotkey[256]; char* hotkeyName = Input_keycodeToPrintable(Input_getHotkeyForAction(player->input, action)); sprintf(hintActionHotkey, "Use: %s", hotkeyName); free(hotkeyName); this->hintSummonHotkey = Sprite_create(SDL_CreateTextureFromSurface( renderer, TTF_RenderUTF8_Blended(this->cardFont, hintActionHotkey, white) ) ); this->hintSummonHotkey->bounds.x = p.x; this->hintSummonHotkey->bounds.y = p.y += ((Card*)this->cards->first->data)->background->bounds.h + 5; char hintNextHotkey[256]; hotkeyName = Input_keycodeToPrintable(Input_getHotkeyForAction(player->input, attackSword)); sprintf(hintNextHotkey, "N. %s", hotkeyName); free(hotkeyName); this->hintNextHotkey = Sprite_create(SDL_CreateTextureFromSurface( renderer, TTF_RenderUTF8_Blended(this->cardFont, hintNextHotkey, white) ) ); this->hintNextHotkey->bounds.x = p.x; this->hintNextHotkey->bounds.y = this->hintSummonHotkey->bounds.y + this->hintSummonHotkey->bounds.h; char hintPreviousHotkey[256]; hotkeyName = Input_keycodeToPrintable(Input_getHotkeyForAction(player->input, attackBow)); sprintf(hintPreviousHotkey, "P. %s", hotkeyName); free(hotkeyName); this->hintPreviousHotkey = Sprite_create(SDL_CreateTextureFromSurface( renderer, TTF_RenderUTF8_Blended(this->cardFont, hintPreviousHotkey, white) ) ); this->hintPreviousHotkey->bounds.x = p.x; this->hintPreviousHotkey->bounds.y = this->hintNextHotkey->bounds.y + this->hintNextHotkey->bounds.h; char indicatorpath[128]; sprintf(indicatorpath, "images/%s/indicator.png", player->scene->colorPrefix); this->indicator = Sprite_create(TextureCache_getForUnconstantPath(tc, indicatorpath)); this->indicator->bounds.y = ((Card*)this->cards->first->data)->background->bounds.y - this->indicator->bounds.h -2; this->selected = this->cards->first; return this; } void DungeonMasterUi_destroy(DungeonMasterUi* this) { FREE_LIST_WITH_ELMENTS(this->cards, Card_destroy); TTF_CloseFont(this->cardFont); } void DungeonMasterUi_summonSelectedCard(DungeonMasterUi* this, struct Scene* scene, SDL_Point tilePosition) { Card* card = this->selected->data; if (card && this->player->money >= card->cost) { Player_earnMoney(this->player, -card->cost); if (card->type == CARD_SUMMON_WHITKNIGHT) { if (Scene_getTile(scene, tilePosition) == NULL) { Enemy* enemy = Enemy_createWhiteKnight(scene, tilePosition); Scene_addEntity(scene, enemy->entity); } } else if (card->type == CARD_CONTROL_STRONGEST) { Entity* strongest = NULL; for (int i=0; i < scene->entities->usedElements; ++i) { Entity* e = scene->entities->elements[i]; if (e && (e->physics.belongsToGroups & COLLISION_GROUP_ENEMY)) { if (!strongest || e->attack1 > strongest->attack1) { strongest = e; } } } if (strongest) { ControlledEntity_set(&this->player->controlledEntity, strongest); } } } } void DungeonMasterUi_selectNext(DungeonMasterUi* this) { if (this->selected->next) { this->selected = this->selected->next; } } void DungeonMasterUi_selectPrevious(DungeonMasterUi* this) { if (this->selected->previous) { this->selected = this->selected->previous; } } void DungeonMasterUi_draw(DungeonMasterUi* this, SDL_Renderer* renderer) { SDL_SetRenderDrawColor(renderer, 100, 100, 100, 205); SDL_SetRenderDrawBlendMode(renderer, SDL_BLENDMODE_BLEND); SDL_RenderFillRect(renderer, &this->background); SDL_SetRenderDrawBlendMode(renderer, SDL_BLENDMODE_NONE); SDL_SetRenderDrawColor(renderer, 0, 0, 0, 0); ListNode* it = this->cards->first; while (it) { Card* card = it->data; if (card) { Card_draw(card, renderer); } it = it->next; } Card* selected = this->selected->data; this->indicator->bounds.x = selected->background->bounds.x + (selected->background->bounds.w/2) - (this->indicator->bounds.w/2); Sprite_draw(this->indicator, renderer); Sprite_draw(this->hintNextHotkey, renderer); Sprite_draw(this->hintPreviousHotkey, renderer); Sprite_draw(this->hintSummonHotkey, renderer); } void DungeonMasterUi_update(DungeonMasterUi* this, RawTime dt) { } Card* Card_create(SDL_Point p, SDL_Renderer* renderer, TextureCache* tc, const char* picture, int type, int cost, TTF_Font* font, const char* description) { Card* this = malloc(sizeof(Card)); this->background = Sprite_create(TextureCache_get(tc, "images/card.png")); this->coin = Sprite_create(TextureCache_get(tc, "images/uiCoin.png")); this->picture = Sprite_create(TextureCache_get(tc, picture)); this->cost = cost; this->type = type; SDL_Color white = {.r=255, .g=255, .b=255, .a=255}; this->description = Sprite_create( SDL_CreateTextureFromSurface( renderer, TTF_RenderUTF8_Blended(font, description, white) ) ); char costString[128]; sprintf(costString, "%d", cost); this->costLabel = Sprite_create( SDL_CreateTextureFromSurface( renderer, TTF_RenderUTF8_Blended(font, costString, white) ) ); this->background->bounds.x = p.x; this->background->bounds.y = p.y; this->costLabel->bounds.x = p.x + 7; this->costLabel->bounds.y = p.y + 90; this->coin->bounds.x = p.x + 7 + this->costLabel->bounds.w; this->coin->bounds.y = p.y + 90; this->picture->bounds.x = p.x + 38; this->picture->bounds.y = p.y + 45; this->description->bounds.x = p.x + 7; this->description->bounds.y = p.y + 90 + this->costLabel->bounds.h + 3; return this; } void Card_draw(Card* this, SDL_Renderer* renderer) { Sprite_draw(this->background, renderer); Sprite_draw(this->picture, renderer); Sprite_draw(this->description, renderer); Sprite_draw(this->costLabel, renderer); Sprite_draw(this->coin, renderer); } void Card_destroy(Card* this) { Sprite_destroy(this->background); Sprite_destroy(this->picture); Sprite_destroy(this->description); Sprite_destroy(this->costLabel); Sprite_destroy(this->coin); free(this); }