int main(int argc, char **argv) { int myints[] = {10, 20, 30, 5, 15}; Vector *v = NewVector(int, 5); for (int i = 0; i < 5; i++) { Vector_Push(v, myints[i]); } Make_Heap(v, 0, v->top, cmp); int n; Vector_Get(v, 0, &n); assert(30 == n); Heap_Pop(v, 0, v->top, cmp); v->top = 4; Vector_Get(v, 0, &n); assert(20 == n); Vector_Push(v, 99); Heap_Push(v, 0, v->top, cmp); Vector_Get(v, 0, &n); assert(99 == n); Vector_Free(v); printf("PASS!\n"); return 0; }
bool Entity_CollisionWithMonsters(Entity* ent, Vector* monsters_vector) { bool collision = false; Box* temp = BoundingBox_CreateTemp(ent); if(ent->t != Cat_Player || (ent->t == Cat_Player && ent->playerC->invulnerability_timer <= 0)) { Entity* collision_sides[5] = {false}; for(int i = 0 ; i < Vector_Count(monsters_vector) ; i++) { if(Vector_Get(monsters_vector, i) != ent) { Entity* mob_to_check = (struct Entity*)Vector_Get(monsters_vector, i); if(Entity_CheckNear(ent, mob_to_check)) { Direction collision_direction = BoundingBox_CheckCollision(&ent->box, temp, &mob_to_check->box); if (collision_direction != None) { collision = true; collision_sides[collision_direction] = mob_to_check; ent->collision_direction = collision_direction; } } } } if ((collision_sides[Bottom] && ent->movementC->dy > 0) || (collision_sides[Top] && ent->movementC->dy < 0)) { ent->movementC->dy = 0; } if ((collision_sides[Right] && ent->movementC->dx > 0) || (collision_sides[Left] && ent->movementC->dx < 0)) { ent->movementC->dx = 0; } if(collision && ent->t == Cat_Player) { Player_TakeDamage(ent, collision_sides); } } free(temp); return collision; }
/*Wave Wave_Create( { Wave wave; wave.zombies[Normal_Zombie] = normal_zombies; wave.zombies[Zombie_Fast] = Zombie_Fasts; wave.zombies[Zombie_Heavy] = Zombie_Heavys; wave.zombies[Zombie_Huge] = Zombie_Huges; wave.zombies[Zombie_Trooper] = Zombie_Troopers; return wave; } */ void Game_StartMap(World* world) { world->player.visible = true; world->player.solid = true; for(int i = 0 ; i < Vector_Count(&world->events_vector) ; i++) { Entity* event = (Entity*)Vector_Get(&world->events_vector, i); if(event->sub_category == Event_Player_Start) { world->player = Player_Create( event->x, event->y, 10, 10); world->player.visible = true; world->player.solid = true; world->player.playerC->cameraX = -screen_width_g/2 + world->player.x; world->player.playerC->cameraY = -screen_height_g/2 + world->player.y; //Graphics_SetCamera((-screen_width_g/2 + world->player.x), -screen_height_g/2 + world->player.y); } } }
TEST_F(RangeTest, testRangeTree) { NumericRangeTree *t = NewNumericRangeTree(); ASSERT_TRUE(t != NULL); for (size_t i = 0; i < 50000; i++) { NumericRangeTree_Add(t, i + 1, (double)(1 + prng() % 5000)); } ASSERT_EQ(t->numRanges, 16); ASSERT_EQ(t->numEntries, 50000); struct { double min; double max; } rngs[] = {{0, 100}, {10, 1000}, {2500, 3500}, {0, 5000}, {4999, 4999}, {0, 0}}; for (int r = 0; rngs[r].min || rngs[r].max; r++) { Vector *v = NumericRangeTree_Find(t, rngs[r].min, rngs[r].max); ASSERT_TRUE(Vector_Size(v) > 0); // printf("Got %d ranges for %f..%f...\n", Vector_Size(v), rngs[r].min, rngs[r].max); for (int i = 0; i < Vector_Size(v); i++) { NumericRange *l; Vector_Get(v, i, &l); ASSERT_TRUE(l); // printf("%f...%f\n", l->minVal, l->maxVal); ASSERT_FALSE(l->minVal > rngs[r].max); ASSERT_FALSE(l->maxVal < rngs[r].min); } Vector_Free(v); } NumericRangeTree_Free(t); }
bool Entity_CheckCanSeeEntity(Entity* ent1, Entity* ent2, World* world) { float ent1MiddleX = 0; float ent1MiddleY = 0; Entity_GetMiddleCoordinates(ent1, &ent1MiddleX, &ent1MiddleY); float ent2MiddleX = 0; float ent2MiddleY = 0; Entity_GetMiddleCoordinates(ent2, &ent2MiddleX, &ent2MiddleY); if(ent2MiddleX < ent1MiddleX) { float tempX = ent1MiddleX; float tempY = ent1MiddleY; ent1MiddleX = ent2MiddleX; ent1MiddleY = ent2MiddleY; ent2MiddleX = tempX; ent2MiddleY = tempY; } float angle = C_AngleBetween2Points(ent1MiddleX, ent1MiddleY, ent2MiddleX, ent2MiddleY); float dx = cos(angle) * 20; float dy = sin(angle) * 20; float pointX = ent1MiddleX; float pointY = ent1MiddleY; bool collision = false; while(!collision && ((pointX - ent1MiddleX <= ent2MiddleX - ent1MiddleX) && (abs(pointY - ent1MiddleY) <= abs(ent2MiddleY - ent1MiddleY)))) { pointX += dx; pointY += dy; for(int i = 0 ; i < Vector_Count(&world->non_null_walls) && !collision ; i++) { Entity* wall = (Entity*)Vector_Get(&world->non_null_walls, i); if(Entity_CheckDistance(ent1, wall, 1000) && Wall_IsWall(wall) && BoundingBox_CheckPointCollision(pointX, pointY, &wall->box)) { collision = true; } } } return !collision; }
void Level_Clear(World* w) { for(int i = 0 ; i < Vector_Count(&w->monsters_vector) ; i++) { Entity* ent = Vector_Get(&w->monsters_vector, i); Entity_Destroy(ent); } for(int i = 0 ; i < Vector_Count(&w->bonus_vector) ; i++) { Entity* ent = Vector_Get(&w->bonus_vector, i); Entity_Destroy(ent); } for(int i = 0 ; i < Vector_Count(&w->bullets_vector) ; i++) { Entity* ent = Vector_Get(&w->bullets_vector, i); Entity_Destroy(ent); } for(int i = 0 ; i < Vector_Count(&w->explosions_vector) ; i++) { Entity* ent = Vector_Get(&w->explosions_vector, i); Entity_Destroy(ent); } Vector_Clear(&w->monsters_vector); Vector_Clear(&w->bonus_vector); Vector_Clear(&w->events_vector); Vector_Clear(&w->bullets_vector); Vector_Clear(&w->explosions_vector); Vector_Clear(&w->decals_vector); Vector_Clear(&w->props_vector); for(int i = 0 ; i < w->map_size ; i++) { free(w->map[i]); } for(int i = 0 ; i < w->map_size ; i++) { free(w->ground_map[i]); } }
/* Get the element at the end of the vector, decreasing the size by one */ inline int Vector_Pop(Vector *v, void *ptr) { if (v->top > 0) { if (ptr != NULL) { Vector_Get(v, v->top - 1, ptr); } v->top--; return 1; } return 0; }
dfaNode *__dfn_getCache(Vector *cache, sparseVector *v) { size_t n = Vector_Size(cache); for (int i = 0; i < n; i++) { dfaNode *dfn; Vector_Get(cache, i, &dfn); if (__sv_equals(v, dfn->v)) { return dfn; } } return NULL; }
void Free_AST_OrderNode(AST_OrderNode *orderNode) { if(orderNode != NULL) { for(int i = 0; i < Vector_Size(orderNode->columns); i++) { AST_ColumnNode *c = NULL; Vector_Get(orderNode->columns, i , &c); Free_AST_ColumnNode(c); } Vector_Free(orderNode->columns); free(orderNode); } }
void Player_CheckBonusCollision(Entity* player, Vector* bonus_vector) { bool collision = false; for(int i = 0 ; i < Vector_Count(bonus_vector) ; i++) { collision = false; Entity* bonus = (Entity*)Vector_Get(bonus_vector, i); collision = BoundingBox_CheckSimpleCollision(&player->box, &bonus->box); if(collision) { Sound_PlayPickUp(); Player_PickUpBonus(player, bonus); } } }
void Free_AST_GraphEntity(AST_GraphEntity *graphEntity) { if(graphEntity->label != NULL) { free(graphEntity->label); } if(graphEntity->alias != NULL) { free(graphEntity->alias); } if(graphEntity->properties != NULL) { for(int i = 0; i < Vector_Size(graphEntity->properties); i++) { SIValue *val; Vector_Get(graphEntity->properties, i, &val); SIValue_Free(val); free(val); } Vector_Free(graphEntity->properties); } free(graphEntity); }
void Level_Save(char* file_name, World* w) { FILE *save_file; save_file = fopen(file_name, "wb"); if(!save_file) { printf("Can't open file\n"); } printf("SAVING\n"); //the NULL walls aren't written //we need to tell fread how much to read printf("ftell nb_walls : %d\n", ftell(save_file)); int nb_of_walls = 0; for(int i = 0 ; i < w->map_size ; i++) { if(w->map[i] != NULL) { nb_of_walls++; } } printf("%d walls", nb_of_walls); fwrite(&nb_of_walls, sizeof(int), 1, save_file); for(int i = 0 ; i < w->map_size ; i++) { fwrite(w->map[i], sizeof(Entity), 1, save_file); } for(int i = 0 ; i < w->map_size ; i++) { fwrite(w->ground_map[i], sizeof(Entity), 1, save_file); } int num_of_events = Vector_Count(&w->events_vector); fwrite(&num_of_events, sizeof(int), 1, save_file); for(int i = 0 ; i < num_of_events ; i++) { Entity* buffer = (Entity*)Vector_Get(&w->events_vector, i); LevelEditor_WriteEntity(save_file, buffer); } int num_of_props = Vector_Count(&w->props_vector); fwrite(&num_of_props, sizeof(int), 1, save_file); for(int i = 0 ; i < num_of_props ; i++) { Entity* buffer = (Entity*)Vector_Get(&w->props_vector, i); LevelEditor_WriteEntity(save_file, buffer); } int num_of_zombies = Vector_Count(&w->monsters_vector); fwrite(&num_of_zombies, sizeof(int), 1, save_file); printf("Saving %d mobs\n", num_of_zombies); for(int i = 0 ; i < num_of_zombies ; i++) { Entity* buffer = (Entity*)Vector_Get(&w->monsters_vector, i); LevelEditor_WriteEntity(save_file, buffer); } int num_of_decals = Vector_Count(&w->decals_vector); fwrite(&num_of_decals, sizeof(int), 1, save_file); for(int i = 0 ; i < num_of_decals ; i++) { Entity* buffer = (Entity*)Vector_Get(&w->decals_vector, i); LevelEditor_WriteEntity(save_file, buffer); } int num_of_bonus = Vector_Count(&w->bonus_vector); fwrite(&num_of_bonus, sizeof(int), 1, save_file); for(int i = 0 ; i < num_of_bonus ; i++) { Entity* buffer = (Entity*)Vector_Get(&w->bonus_vector, i); LevelEditor_WriteEntity(save_file, buffer); } fclose(save_file); }
void Inputs_ApplyInputsLevelEditor(Controls* controls, World* world, Window* level_editor, GameManager* gm) { rotate_timer += delta_g; if(pressedKeys[SDLK_F1] && SDL_GetTicks() - switch_timer > 200) { draw_grid_g = draw_grid_g? false : true; } if(pressedKeys[SDLK_F7] && SDL_GetTicks() - switch_timer > 2000) { switch_timer = SDL_GetTicks(); LevelEditor_QuickTry(world); } int position_in_array = controls->mouseTileY * world->map_width + controls->mouseTileX; if(controls->temp_object_to_create != NULL) { if(pressedKeys[SDLK_f] && !previousPressedKeys_g[SDLK_f]) { SDL_WarpMouseInWindow(Graphics_GetWindow(), (controls->mouseTileX * TILE_SIZE - world->player.playerC->cameraX), controls->mouseTileY * TILE_SIZE - world->player.playerC->cameraY); } if(pressedKeys[SDLK_RIGHT] && !previousPressedKeys_g[SDLK_RIGHT]) { SDL_WarpMouseInWindow(Graphics_GetWindow(), (controls->mouseX + 1 ), controls->mouseY); } if(pressedKeys[SDLK_LEFT] && !previousPressedKeys_g[SDLK_LEFT]) { SDL_WarpMouseInWindow(Graphics_GetWindow(), (controls->mouseX - 1 ), controls->mouseY); } if(pressedKeys[SDLK_UP] && !previousPressedKeys_g[SDLK_UP]) { SDL_WarpMouseInWindow(Graphics_GetWindow(), (controls->mouseX ), controls->mouseY - 1); } if(pressedKeys[SDLK_DOWN] && !previousPressedKeys_g[SDLK_DOWN]) { SDL_WarpMouseInWindow(Graphics_GetWindow(), (controls->mouseX), controls->mouseY + 1); } if(controls->mouseWheelPos < 0 && rotate_timer > 50) { if(controls->temp_object_to_create->angle == 0) { controls->temp_object_to_create->angle = HALF_PI; } else if(controls->temp_object_to_create->angle <= HALF_PI + 0.1f) { controls->temp_object_to_create->angle = HALF_PI * 2; } else if(controls->temp_object_to_create->angle <= HALF_PI * 2 + 0.1f) { controls->temp_object_to_create->angle = HALF_PI * 3; } else if(controls->temp_object_to_create->angle <= HALF_PI * 3 + 0.1f) { controls->temp_object_to_create->angle = 0; } rotate_timer = 0; } if(controls->mouseWheelPos > 0 && rotate_timer > 50) { if(controls->temp_object_to_create->angle == 0) { controls->temp_object_to_create->angle = HALF_PI * 3; } else if(controls->temp_object_to_create->angle >= HALF_PI * 3 - 0.1f) { controls->temp_object_to_create->angle = HALF_PI * 2; } else if(controls->temp_object_to_create->angle >= HALF_PI * 2 - 0.1f) { controls->temp_object_to_create->angle = HALF_PI; } else if(controls->temp_object_to_create->angle >= HALF_PI - 0.2f) { controls->temp_object_to_create->angle = 0; } rotate_timer = 0; } controls->temp_object_to_create->x = controls->mousePositionInWorldX; controls->temp_object_to_create->y = controls->mousePositionInWorldY; BoundingBox_Update(controls->temp_object_to_create); } Vector* monsters_vector = &world->monsters_vector; if(BoundingBox_CheckPointCollision(controls->mouseX, controls->mouseY, &level_editor->box)) { controls->hovering_on_window = true; //selecting button in level_editor if(controls->pressedMouseButtons[SDL_BUTTON_LEFT]) { for(int i = 0 ; i < level_editor->nb_of_buttons ; i++) { if(BoundingBox_CheckPointCollision(controls->mouseX, controls->mouseY, &level_editor->buttons[i].box)) { level_editor->active_button = &level_editor->buttons[i]; if(controls->temp_object_to_create != NULL) { Entity_Destroy(controls->temp_object_to_create); free(controls->temp_object_to_create); } controls->temp_object_to_create = Entity_Create(level_editor->active_button->main_category, level_editor->active_button->button_type, controls->mouseX, controls->mouseY, 0, world); } } } //showing window resizing cursor if((controls->mouseX > level_editor->box.right - 10 || controls->mouseX < level_editor->box.left + 10) && controls->active_window == NULL) { controls->cursor_resize_left_right = true; if(controls->pressedMouseButtons[SDL_BUTTON_LEFT] && controls->previousPressedMouseButtons[SDL_BUTTON_LEFT] && controls->mouseX > level_editor->box.right - 10) { controls->resizing_right = true; } if(controls->pressedMouseButtons[SDL_BUTTON_LEFT] && controls->previousPressedMouseButtons[SDL_BUTTON_LEFT] && controls->mouseX < level_editor->box.left + 10) { controls->resizing_left = true; } } else { controls->cursor_resize_left_right = false; } //clicking on the level editor window if(controls->pressedMouseButtons[SDL_BUTTON_LEFT] && controls->previousPressedMouseButtons[SDL_BUTTON_LEFT] && !controls->cursor_resize_left_right) { controls->active_window = level_editor; } } else //if not hovering on the window { controls->cursor_resize_left_right = false; controls->cursor_resize_up_down = false; controls->hovering_on_window = false; } //if you've clicked on the window and then stopped clicking, it's not "active" anymore if(controls->active_window != NULL && !controls->previousPressedMouseButtons[SDL_BUTTON_LEFT]) { controls->active_window = NULL; } //resizing window if(controls->resizing_right || controls->resizing_left) { controls->cursor_resize_left_right = true; if(!controls->pressedMouseButtons[SDL_BUTTON_LEFT] && !controls->previousPressedMouseButtons[SDL_BUTTON_LEFT]) { controls->resizing_right = false; controls->resizing_left = false; } if(controls->resizing_right) { Window_ResizeRight(level_editor, controls->mouseX - controls->previousMouseX); } else if(controls->resizing_left) { Window_ResizeLeft(level_editor, controls->mouseX - controls->previousMouseX); } } //moving window if(controls->active_window != NULL && controls->pressedMouseButtons[SDL_BUTTON_LEFT] && controls->previousPressedMouseButtons[SDL_BUTTON_LEFT] && !controls->resizing_left && !controls->resizing_right) { Window_Move(level_editor, controls->mouseX - controls->previousMouseX, controls->mouseY - controls->previousMouseY); } if(pressedKeys[SDLK_LCTRL]) { unlimited_creation = true; } else { unlimited_creation = false; } //if not hovering and not moving window //we can create/delete stuff on the map if(!controls->active_window && !controls->hovering_on_window) { if (level_editor->active_button != NULL) { Main_Category category = level_editor->active_button->main_category; int obj_type = level_editor->active_button->button_type; if(!rectangle_selection_create && (controls->pressedMouseButtons[SDL_BUTTON_RIGHT] || pressedKeys[SDLK_SPACE])) { if (controls->mouseTileX < world->map_width && controls->mouseTileX > 0 && controls->mouseTileY < world->map_height && controls->mouseTileY > 0) { //pressing shift activate the rectangle selection tool if((category == Cat_Wall || category == Cat_Ground) && pressedKeys[SDLK_LSHIFT]) { rectangle_selection_startX = controls->mousePositionInWorldX; rectangle_selection_startY = controls->mousePositionInWorldY; rectangle_selection_create = true; } LevelEditor_CreateObject(category, obj_type, controls->tileInPixelsX, controls->tileInPixelsY, position_in_array, controls->mousePositionInWorldX, controls->mousePositionInWorldY, world, unlimited_creation, controls->temp_object_to_create->angle); } else printf("out of bound!!!"); } //remove wall (replace it with empty entity) if(pressedKeys[SDLK_c]) { } //deleting monster if(pressedKeys[SDLK_x]) { if(pressedKeys[SDLK_LSHIFT] && !rectangle_selection_delete) { rectangle_selection_startX = controls->mousePositionInWorldX; rectangle_selection_startY = controls->mousePositionInWorldY; rectangle_selection_delete = true; } if(!rectangle_selection_delete) { LevelEditor_UpdateWallsCorners(position_in_array, world); free(world->map[position_in_array]); world->map[position_in_array] = NULL; } for(int i = 0 ; i < Vector_Count(&world->monsters_vector) ; i++) { Entity* mob = (Entity*)Vector_Get(&world->monsters_vector, i); if(BoundingBox_CheckPointCollision(controls->mousePositionInWorldX, controls->mousePositionInWorldY, &mob->box)) { Entity_Destroy(mob); Vector_Delete(&world->monsters_vector, i); } } for(int i = 0 ; i < Vector_Count(&world->bonus_vector) ; i++) { Entity* bonus = (Entity*)Vector_Get(&world->bonus_vector, i); if(BoundingBox_CheckPointCollision(controls->mousePositionInWorldX, controls->mousePositionInWorldY, &bonus->box)) { Entity_Destroy(bonus); Vector_Delete(&world->bonus_vector, i); } } for(int i = 0 ; i < Vector_Count(&world->props_vector) ; i++) { Entity* obj = (Entity*)Vector_Get(&world->props_vector, i); if(BoundingBox_CheckPointCollision(controls->mousePositionInWorldX, controls->mousePositionInWorldY, &obj->box)) { Entity_Destroy(obj); Vector_Delete(&world->props_vector, i); } } for(int i = 0 ; i < Vector_Count(&world->decals_vector) ; i++) { Entity* obj = (Entity*)Vector_Get(&world->decals_vector, i); if(BoundingBox_CheckPointCollision(controls->mousePositionInWorldX, controls->mousePositionInWorldY, &obj->box)) { Entity_Destroy(obj); Vector_Delete(&world->decals_vector, i); } } } /*stopping pressing left shift stop the selection and creates the selection tile in the selection (only if it's a tile and not a monster/object */ if((rectangle_selection_create || rectangle_selection_delete) && pressedKeys[SDLK_LSHIFT]) { rectangle_selection_endX = controls->mousePositionInWorldX; rectangle_selection_endY =controls->mousePositionInWorldY; int startX = rectangle_selection_startX; int endX = rectangle_selection_endX; int startY = rectangle_selection_startY; int endY = rectangle_selection_endY; if(rectangle_selection_endY <= rectangle_selection_startY) { startY = rectangle_selection_endY; endY = rectangle_selection_startY; } if(rectangle_selection_endX <= rectangle_selection_startX) { startX = rectangle_selection_endX; endX = rectangle_selection_startX; } int tileStartX = startX / TILE_SIZE; int tileStartY = startY / TILE_SIZE; int tileEndX = endX / TILE_SIZE; int tileEndY = endY / TILE_SIZE; for(int y = tileStartY ; y <= tileEndY ; y++) { for(int x = tileStartX ; x <= tileEndX ; x++) { position_in_array = y * world->map_width + x; if(rectangle_selection_create) { int obj_type = level_editor->active_button->button_type; LevelEditor_CreateObject(category, obj_type, x * TILE_SIZE, y * TILE_SIZE, position_in_array, controls->mousePositionInWorldX, controls->mousePositionInWorldY, world, unlimited_creation, controls->temp_object_to_create->angle); } else if(rectangle_selection_delete) { LevelEditor_UpdateWallsCorners(position_in_array, world); free(world->map[position_in_array]); world->map[position_in_array] = NULL; } } } if(rectangle_selection_delete) { for(int i = 0 ; i < Vector_Count(&world->monsters_vector) ; i++) { Entity* obj = (Entity*)Vector_Get(&world->monsters_vector, i); if(obj->box.right > startX && obj->box.left < endX && obj->box.bottom > startY && obj->box.top < endY) { Entity_Destroy(obj); Vector_Delete(&world->monsters_vector, i); } } for(int i = 0 ; i < Vector_Count(&world->bonus_vector) ; i++) { Entity* obj = (Entity*)Vector_Get(&world->bonus_vector, i); if(obj->box.right > startX && obj->box.left < endX && obj->box.bottom > startY && obj->box.top < endY) { Entity_Destroy(obj); Vector_Delete(&world->bonus_vector, i); } } for(int i = 0 ; i < Vector_Count(&world->props_vector) ; i++) { Entity* obj = (Entity*)Vector_Get(&world->props_vector, i); if(obj->box.right > startX && obj->box.left < endX && obj->box.bottom > startY && obj->box.top < endY) { Entity_Destroy(obj); Vector_Delete(&world->props_vector, i); } } for(int i = 0 ; i < Vector_Count(&world->decals_vector) ; i++) { Entity* obj = (Entity*)Vector_Get(&world->decals_vector, i); if(obj->box.right > startX && obj->box.left < endX && obj->box.bottom > startY && obj->box.top < endY) { Entity_Destroy(obj); Vector_Delete(&world->decals_vector, i); } } } } if((rectangle_selection_create || rectangle_selection_delete) && !pressedKeys[SDLK_LSHIFT]) { rectangle_selection_create = false; rectangle_selection_delete = false; } } //deleting every monster if(pressedKeys[SDLK_u]) { // Vector_Clear(&world->monsters_vector); leads to memory leak } //cancel current selected object /*if(pressedKeys[SDLK_e]) { level_editor->active_button = NULL; }*/ //switch mobs AI if(pressedKeys[SDLK_a] && SDL_GetTicks() - controls->last_ai_switch > 150) { if(gm->ai_on) { gm->ai_on = false; } else { gm->ai_on = true; } controls->last_ai_switch = SDL_GetTicks(); } } }
void GameManage_UpdateWorldEntities(GameManager* gm, World* world) { Vector* bullets_vector = &world->bullets_vector; Vector* bonus_vector = &world->bonus_vector; Vector* monsters_vector = &world->monsters_vector; Vector* explosions_vector = &world->explosions_vector; Vector* decals_vector = &world->decals_vector; Vector* props_vector = &world->props_vector; for(int i = 0 ; i < world->map_size ; i++) { if(world->map[i] != NULL) { // Entity_CalculateVisibility(world->map[i], world); if(world->map[i]->t == Cat_Door) { Door_Update(world->map[i], world); } if(!world->map[i]->alive) { free(world->map[i]);//walls don't have any compotent, no need to destroy them world->map[i] = NULL; } } } for(int i = 0 ; i < Vector_Count(bullets_vector) ; i++) { if(Vector_Get(bullets_vector, i) != NULL) { Entity* projectile = (Entity*)Vector_Get(bullets_vector, i); Entity_CalculateVisibility(projectile, world); if(!bullet_time_g) { if(projectile->t == Cat_Bullet) { Bullet_Update(projectile, world); } else if(projectile->t == Cat_Grenade) { Grenade_Update(projectile, world); if(!projectile->alive) { Vector_Push(explosions_vector, Explosion_Create(projectile->x - 64, projectile->y - 64) ); } } if (projectile->alive == false) { Entity_Destroy(projectile); Vector_Delete(bullets_vector, i); } } } else { printf("Error during update of bullets vector : bullet = NULL"); } } if(gm->ai_on) { for(int i = 0 ; i < Vector_Count(monsters_vector) ; i++) { Entity* mob = (struct Entity*)Vector_Get(monsters_vector, i); if(Entity_CheckDistance(mob, &world->player, 800)) { Entity_CalculateVisibility(mob, world); } if(mob->zombieC->aggressive || Entity_CheckDistance(mob, &world->player, 400)) { Zombie_Update(mob, world); } if (mob->alive == false) { Zombie_Die(mob, bonus_vector, decals_vector); Entity_Destroy(mob); Vector_Delete(monsters_vector, i); } } } for(int i = 0 ; i < Vector_Count(bonus_vector) ; i++) { if(Vector_Get(bonus_vector, i) != NULL) { Entity* bonus = (Entity*)Vector_Get(bonus_vector, i); if(Entity_CheckDistance(bonus, &world->player, 800)) { if(!bullet_time_g) { Bonus_Update(bonus, &world->player); } Entity_CalculateVisibility(bonus, world); } if (bonus->alive == false) { Entity_Destroy(bonus); Vector_Delete(bonus_vector, i); } } else { printf("Error during update of bonus vector : bonus = NULL"); } } for(int i = 0 ; i < Vector_Count(decals_vector) ; i++) { Entity* decal = (Entity*)Vector_Get(decals_vector, i); if(Entity_CheckDistance(decal, &world->player, 800)) { Entity_CalculateVisibility(decal, world); } if(Vector_Count(decals_vector) > 200) { if(decal->is_ennemy) { Vector_Delete(decals_vector, i); } } } for(int i = 0 ; i < Vector_Count(props_vector) ; i++) { Entity* prop = (Entity*)Vector_Get(props_vector, i); if(Entity_CheckDistance(prop, &world->player, 800)) { Entity_CalculateVisibility(prop, world); } if (!prop->alive) { Entity_Destroy(prop); Vector_Delete(props_vector, i); } } for(int i = 0 ; i < Vector_Count(explosions_vector) ; i++) { Entity* exp = (Entity*)Vector_Get(explosions_vector, i); if(!bullet_time_g) { Explosion_Update(exp, world); } Entity_CalculateVisibility(exp, world); if (!exp->alive) { Entity_Destroy(exp); Vector_Delete(explosions_vector, i); } } }
void LevelEditor_CreateObject(Main_Category category, int obj_type, int x, int y, int position_in_array, float mousePositionInWorldX, float mousePositionInWorldY, World* world, bool unlimited, float angle) { if(category == Cat_Wall || category == Cat_Door) { free(world->map[position_in_array]); world->map[position_in_array] = Entity_Create(category, obj_type, x, y, angle, world); } else if(category == Cat_Ground) { Entity_Destroy(world->ground_map[position_in_array]); free(world->ground_map[position_in_array]); world->ground_map[position_in_array] = Entity_Create(category, obj_type, x, y, angle, world); } else if(category == Cat_Zombie && (SDL_GetTicks() - building_time > 150 || unlimited)) { Vector_Push(&world->monsters_vector, Entity_Create(category, obj_type, mousePositionInWorldX, mousePositionInWorldY, 0, world)); building_time = SDL_GetTicks(); } else if(category == Cat_Prop && (SDL_GetTicks() - building_time > 150 || unlimited)) { Vector_Push(&world->props_vector, Entity_Create(category, obj_type, mousePositionInWorldX, mousePositionInWorldY, angle, world)); building_time = SDL_GetTicks(); } else if(category == Cat_Decal && (SDL_GetTicks() - building_time > 150 || unlimited)) { Vector_Push(&world->decals_vector, Entity_Create(category, obj_type, mousePositionInWorldX, mousePositionInWorldY, angle, world)); building_time = SDL_GetTicks(); } else if(category == Cat_Event && (SDL_GetTicks() - building_time > 150)) { //there can be only 1 player start and end of level for(int i = 0 ; i < Vector_Count(&world->events_vector) ; i++) { Entity* map_event = (Entity*)Vector_Get(&world->events_vector, i); if((obj_type == Event_Player_Start || obj_type == Event_End_Level )&& obj_type == map_event->sub_category) { Vector_Delete(&world->events_vector, i); } } Vector_Push(&world->events_vector, Entity_Create(category, obj_type, x, y, 0, world)); building_time = SDL_GetTicks(); } else if(category == Cat_Bonus && (SDL_GetTicks() - building_time > 150 || unlimited)) { Vector_Push(&world->bonus_vector, Entity_Create(category, obj_type, mousePositionInWorldX, mousePositionInWorldY, 0, world)); building_time = SDL_GetTicks(); } }
void Bullet_Update(Entity* bullet, World* world) { bullet->alive_timer -= delta_g; if(bullet->alive_timer <= 0) { bullet->alive = false; } if(bullet->alive) { moveEntity(bullet, bullet->movementC->dx * delta_g, bullet->movementC->dy * delta_g); if(!bullet->is_ennemy) { for(int i = 0 ; i < Vector_Count(&world->monsters_vector) ; i++) { Entity* mob = (struct Entity*)Vector_Get(&world->monsters_vector, i); if (BoundingBox_CheckSimpleCollision(&bullet->box, &mob->box) && mob != bullet->last_zombie_hit) { bullet->last_zombie_hit = mob; Zombie_GetAttacked(mob, bullet->damage, world); int random = rand() % 1000; //modulo the address by 1000 to get a kinda random number if(bullet->nb_penetrations < 4 && random < bullet->penetration_chance) { bullet->nb_penetrations++; } else { bullet->alive = false; } } } } else { //wow I really need to improve this mess //put the collision detection on the zombie's side //and pass the attack direction to the player Box* temp = BoundingBox_CreateTemp(bullet); Direction bullet_coming_from = BoundingBox_CheckCollision(&bullet->box, temp, &world->player.box); if(bullet_coming_from != None && world->player.playerC->invulnerability_timer <= 0) { Entity* collision_direction[5] = {NULL}; collision_direction[bullet_coming_from] = bullet; Player_TakeDamage(&world->player, collision_direction); bullet->alive = false; } free(temp); } for(int i = 0 ; i < Vector_Count(&world->non_null_walls) && bullet->alive; i++) { Entity* wall = (Entity*)Vector_Get(&world->non_null_walls, i); if( Entity_CheckNear(bullet, wall) && wall->solid && BoundingBox_CheckSimpleCollision(&bullet->box, &wall->box)) { Structure_GetAttacked(wall, bullet); bullet->alive = false; } } } }
bool Entity_CollisionWithWalls(Entity* ent, World* world) { Box* temp = BoundingBox_CreateTemp(ent); Entity* collision_sides[5] = {NULL}; int collisions_nb[5] = { 0 }; bool collision = false; for (int i = 0; i < Vector_Count(&world->non_null_walls); i++) { Entity* wall = (Entity*)Vector_Get(&world->non_null_walls, i); if(wall->solid && Entity_CheckVeryClose(ent, wall)) { Direction collision_direction = BoundingBox_CheckCollision(&ent->box, temp, &wall->box); if (collision_direction != None) { collision = true; collision_sides[collision_direction] = wall; collisions_nb[collision_direction]++; ent->collision_direction = collision_direction; if(ent->t == Cat_Zombie && ent->zombieC->aggressive && wall->solid && (wall->t == Cat_Door || wall->t == Cat_Wall) && ent->zombieC->attack_timer >= ent->zombieC->attack_delay) { Structure_GetAttacked(wall, ent); ent->zombieC->attack_timer = 0; } if(ent->t == Cat_Player && wall->t == Cat_Door) { Door_Open(wall); } } } } for(int i = 0 ; i < Vector_Count(&world->props_vector) ; i++) { Entity* prop = (struct Entity*)Vector_Get(&world->props_vector, i); if(Entity_CheckVeryClose(ent, prop)) { Direction collision_direction = BoundingBox_CheckCollision(&ent->box, temp, &prop->box); if (collision_direction != None) { if(ent->t == Cat_Zombie && ent->zombieC->attack_timer >= ent->zombieC->attack_delay) { Structure_GetAttacked(prop, ent); ent->zombieC->attack_timer = 0; } collision = true; collision_sides[collision_direction] = prop; collisions_nb[collision_direction]++; ent->collision_direction = collision_direction; } } } if(ent->movementC->dy == 0) { if(collisions_nb[Right] == 1 && ent->movementC->dx > 0 && ent->box.right <= collision_sides[Right]->box.left) { if(ent->box.top < collision_sides[Right]->box.top) { ent->movementC->dy = -1; } else if(ent->box.bottom > collision_sides[Right]->box.bottom) { ent->movementC->dy = 1; } } else if(collisions_nb[Left] == 1 && ent->movementC->dx < 0 && ent->box.left >= collision_sides[Left]->box.right) { if(ent->box.top < collision_sides[Left]->box.top) { ent->movementC->dy = -1; } else if(ent->box.bottom > collision_sides[Left]->box.bottom) { ent->movementC->dy = 1; } } } if(ent->movementC->dx == 0) { if(collisions_nb[Top] == 1&& ent->movementC->dy < 0 && ent->box.top >= collision_sides[Top]->box.bottom) { if(ent->box.left < collision_sides[Top]->box.left) { ent->movementC->dx = -1; } else if(ent->box.right > collision_sides[Top]->box.right) { ent->movementC->dx = 1; } } else if(collisions_nb[Bottom] == 1 && ent->movementC->dy > 0 && ent->box.bottom <= collision_sides[Bottom]->box.top) { if(ent->box.left < collision_sides[Bottom]->box.left) { ent->movementC->dx = -1; } else if(ent->box.right > collision_sides[Bottom]->box.right) { ent->movementC->dx = 1; } } } if (collisions_nb[Bottom] && ent->movementC->dy >= 0) { ent->movementC->dy = collision_sides[Bottom]->box.top - ent->box.bottom - 1; } if (collisions_nb[Top] && ent->movementC->dy <= 0) { ent->movementC->dy = collision_sides[Top]->box.bottom - ent->box.top + 1; } Box* temp2 = BoundingBox_CreateTemp(ent); if (collisions_nb[Right] && BoundingBox_CheckCollision(&ent->box, temp2, &collision_sides[Right]->box) == Right && ent->movementC->dx >= 0) { ent->movementC->dx = collision_sides[Right]->box.left - ent->box.right - 1; } if (collisions_nb[Left] && BoundingBox_CheckCollision(&ent->box, temp2, &collision_sides[Left]->box) == Left && ent->movementC->dx <= 0) { ent->movementC->dx = collision_sides[Left]->box.right - ent->box.left + 1; } free(temp2); if(collisions_nb[Left] > 1 && collisions_nb[Bottom] > 0) { ent->movementC->dx = collision_sides[Left]->box.right - ent->box.left + 1; ent->movementC->dy = collision_sides[Bottom]->box.top - ent->box.bottom - 1; } if(collisions_nb[Left] > 1 && collisions_nb[Top] > 0) { ent->movementC->dx = collision_sides[Left]->box.right - ent->box.left + 1; ent->movementC->dy = collision_sides[Top]->box.bottom - ent->box.top + 1; } if(collisions_nb[Right] > 1 && collisions_nb[Bottom] > 0) { ent->movementC->dx = collision_sides[Right]->box.left - ent->box.right - 1; ent->movementC->dy = collision_sides[Bottom]->box.top - ent->box.bottom - 1; } if(collisions_nb[Right] > 1 && collisions_nb[Top] > 0) { ent->movementC->dx = collision_sides[Right]->box.left - ent->box.right - 1; ent->movementC->dy = collision_sides[Top]->box.bottom - ent->box.top + 1; } free(temp); return collision; }