#include "mapview.h" // Initialize jump physics double jump_height = TILE_HEIGHT*3; double jump_length = 0.25 * FRAMES_PER_SECOND; double jump_velocity = -2 * jump_height / jump_length; double jump_gravity = -1 * jump_velocity / jump_length; MapView::MapView(Map* first, int x, int y) { // Initialize player data player = new mob_t(); player->x = x; player->y = y; player->x_vel = 0; player->y_vel = 0; player->x_accel = 0; player->y_accel = jump_gravity; player->w = 10; player->h = 14; loadMap(first); } MapView::~MapView() { destroyTexture(bg); delete player; } void MapView::loadMap(Map* m) { curMap = m; left_collisions.clear(); right_collisions.clear(); up_collisions.clear(); down_collisions.clear(); add_collision(-6, 0, GAME_WIDTH, left, (m->getLeftMap() == NULL) ? 1 : 2); add_collision(GAME_WIDTH+6, 0, GAME_WIDTH, right, (m->getRightMap() == NULL) ? 1 : 2); if (bg == NULL) { bg = createTexture(GAME_WIDTH, GAME_HEIGHT); } fillTexture(bg, NULL, 0, 0, 0); const char* mapbuf = m->mapdata(); for (int i=0; ititle(); int start_x = (40/2) - (strlen(map_name)/2); for (size_t i=0; iy_vel = jump_velocity; break; } } else if (action == GLFW_RELEASE) { switch (key) { case GLFW_KEY_LEFT: holding_left = false; break; case GLFW_KEY_RIGHT: holding_right = false; break; } } } void MapView::tick() { if (holding_left && player->x_vel >= 0) { player->x_vel = -2; } else if (holding_right && player->x_vel <= 0) { player->x_vel = 2; } else if (!holding_left && !holding_right) { player->x_vel = 0; } player->x_vel += player->x_accel; if (player->x_vel < -16) player->x_vel = -16; if (player->x_vel > 16) player->x_vel = 16; int playerx_next = player->x + player->x_vel; player->y_vel += player->y_accel; if (player->y_vel > 16) player->y_vel = 16; // Terminal velocity if (player->y_vel < -16) player->y_vel = -16; int playery_next = player->y + player->y_vel; check_collisions(player, playerx_next, playery_next); } void MapView::render(Texture* tex) { // Draw the background blitTexture(bg, tex, NULL, NULL); // Draw the player Rectangle dst_rect(player->x, player->y, player->w, player->h); fillTexture(tex, &dst_rect, 255, 255, 255); } void MapView::add_collision(int axis, int lower, int upper, direction_t dir, int type) { //printf("added collision\n"); list::iterator it; switch (dir) { case up: it = up_collisions.begin(); for (; it!=up_collisions.end(); it++) { if (it->axis < axis) break; } up_collisions.insert(it, {axis, lower, upper, type}); break; case down: it = down_collisions.begin(); for (; it!=down_collisions.end(); it++) { if (it->axis > axis) break; } down_collisions.insert(it, {axis, lower, upper, type}); break; case left: it = left_collisions.begin(); for (; it!=left_collisions.end(); it++) { if (it->axis < axis) break; } left_collisions.insert(it, {axis, lower, upper, type}); break; case right: it = right_collisions.begin(); for (; it!=right_collisions.end(); it++) { if (it->axis > axis) break; } right_collisions.insert(it, {axis, lower, upper, type}); break; } } void MapView::check_collisions(mob_t* mob, int x_next, int y_next) { if (x_next < mob->x) { for (list::iterator it=left_collisions.begin(); it!=left_collisions.end(); it++) { if (it->axis > mob->x) continue; if (it->axis < x_next) break; if ((mob->y+mob->h > it->lower) && (mob->y < it->upper)) { // We have a collision! if (it->type == 0) { x_next = it->axis; mob->x_vel = 0; } else if (it->type == 1) { x_next = GAME_WIDTH-mob->w/2; } else if (it->type == 2) { x_next = GAME_WIDTH-mob->w/2; loadMap(curMap->getLeftMap()); } break; } } } else if (x_next > mob->x) { for (list::iterator it=right_collisions.begin(); it!=right_collisions.end(); it++) { if (it->axis < mob->x+mob->w) continue; if (it->axis > x_next+mob->w) break; if ((mob->y+mob->h > it->lower) && (mob->y < it->upper)) { // We have a collision! if (it->type == 0) { x_next = it->axis - mob->w; mob->x_vel = 0; } else if (it->type == 1) { x_next = -mob->w/2; } else if (it->type == 2) { x_next = -mob->w/2; loadMap(curMap->getRightMap()); } break; } } } mob->x = x_next; if (y_next < mob->y) { for (list::iterator it=up_collisions.begin(); it!=up_collisions.end(); it++) { if (it->axis > mob->y) continue; if (it->axis < y_next) break; if ((mob->x+mob->w > it->lower) && (mob->x < it->upper)) { // We have a collision! if (it->type == 0) { y_next = it->axis; mob->y_vel = 0; } else if (it->type == 1) { y_next = GAME_HEIGHT-mob->h/2-1; } break; } } } else if (y_next > mob->y) { for (list::iterator it=down_collisions.begin(); it!=down_collisions.end(); it++) { if (it->axis < mob->y+mob->h) continue; if (it->axis > y_next+mob->h) break; if ((mob->x+mob->w > it->lower) && (mob->x < it->upper)) { // We have a collision! if (it->type == 0) { y_next = it->axis - mob->h; mob->y_vel = 0; } else if (it->type == 1) { y_next = 1 - mob->h/2; } break; } } } mob->y = y_next; }