ROBOT_STATE Level::robot_on_level(SDL_Rect &box, int dir, int speed, ROBOT_STATE state){ //GlassDoor * door = dynamic_cast<GlassDoor *>(sprite_list[1]); if (dir == SDLK_RIGHT) { int bot_right= get_tile_pos(box.x + box.w + speed, box.y+ box.h); int top_right = get_tile_pos(box.x + box.w + speed, box.y); if ((tiles[bot_right + column].type >= WALL_C0 && tiles[bot_right + column].type <= WALL_V4) || tiles[bot_right + column].type == LADDER) { //2nd, no obstacle for body SDL_Rect box_copy = box; box_copy.x += speed; for (int i = bot_right; i > top_right; i -=column) { if (tiles[i].type >= WALL_C0 && tiles[i].type <= WALL_V4) { state = TURN; break; } } //add a stop interacting here? int block_x = box.x + speed; if (state == TURN )box.x = (top_right % column - 1) * TILE_WIDTH; else box.x = block_x; } }else if(dir == SDLK_LEFT){ int top_left = get_tile_pos(box.x - speed, box.y); int bot_left = get_tile_pos(box.x - speed, box.y + box.h); if ((tiles[bot_left + column].type >= WALL_C0 && tiles[bot_left + column].type <= WALL_V4) || tiles[bot_left + column].type == LADDER) { for (int i = bot_left; i > top_left; i -= column) { if (tiles[i].type >= WALL_C0 && tiles[i].type <= WALL_V4) { state = TURN; break; } } // int block_x = box.x - speed; // if (door->is_block() && box.x >= door->get_rect().x) block_x = std::max(block_x, door->get_rect().x + box.w); // if (state == TURN) box.x = (top_left % column + 1) * TILE_WIDTH; else box.x = block_x; } } return state; }
bool Level::is_on_ground(SDL_Rect &box){ int bot_center = get_tile_pos(box.x + box.w/2, box.y + box.h); if (tiles[bot_center ].type == LADDER || (tiles[bot_center ].type >= WALL_H0 && tiles[bot_center ].type <= WALL_H4)) { box.y = (int) (bot_center / column) * TILE_HEIGHT - box.h; return true; }else{ return false; } }
bool EditorToolboxWidget::on_mouse_motion(const SDL_MouseMotionEvent& motion) { Vector mouse_pos = VideoSystem::current()->get_viewport().to_logical(motion.x, motion.y); float x = mouse_pos.x - static_cast<float>(m_Xpos); float y = mouse_pos.y - static_cast<float>(m_Ypos); if (x < 0) { m_hovered_item = HoveredItem::NONE; m_tile_scrolling = TileScrolling::NONE; return false; } if (y < 0) { if (y < -38) { m_hovered_item = HoveredItem::TILEGROUP; } else if (y < -16) { m_hovered_item = HoveredItem::OBJECTS; } else { m_hovered_item = HoveredItem::TOOL; m_hovered_tile = get_tool_pos(mouse_pos); } m_tile_scrolling = TileScrolling::NONE; return false; } else { m_hovered_item = HoveredItem::TILE; m_hovered_tile = get_tile_pos(mouse_pos); if (m_dragging && m_input_type == InputType::TILE) { update_selection(); } } if (y < 16) { m_tile_scrolling = TileScrolling::UP; m_using_scroll_wheel = false; } else if (y > static_cast<float>(SCREEN_HEIGHT - 16 - m_Ypos)) { m_tile_scrolling = TileScrolling::DOWN; m_using_scroll_wheel = false; } else { m_tile_scrolling = TileScrolling::NONE; } return false; }
HUMAN_STATE Level::stick_on_level(SDL_Rect &box, int dir, int speed, HUMAN_STATE state){ //do a dirty test first //GlassDoor * door = dynamic_cast<GlassDoor *>(sprite_list[1]); if (state == WALK) { if (dir == SDLK_RIGHT) { int bot_right= get_tile_pos(box.x + box.w + speed, box.y+ box.h); int top_right = get_tile_pos(box.x + box.w + speed, box.y); if ((box.y + box.h)%TILE_HEIGHT == 0 && ((tiles[bot_right + column].type >= WALL_C0 && tiles[bot_right + column].type <= WALL_V4) || tiles[bot_right + column].type == LADDER || tiles[bot_right + column].type == BACK_WALL)) { //2nd, no obstacle for body for (int i = bot_right; i > top_right; i -=column) { if (tiles[i].type >= WALL_C0 && tiles[i].type <= WALL_V4) { //dirty fix state = STUCK; break; } } if (state == STUCK )box.x = (top_right % column - 1) * TILE_WIDTH; else { if (tiles[bot_right + column].type == BACK_WALL) { state = FALL; }else{ box.x += speed; } } } }else if(dir == SDLK_LEFT){ int top_left = get_tile_pos(box.x - speed, box.y); int bot_left = get_tile_pos(box.x - speed, box.y + box.h); if ((box.y + box.h)%TILE_HEIGHT == 0 && ((tiles[bot_left + column].type >= WALL_C0 && tiles[bot_left + column].type <= WALL_V4) ||tiles[bot_left + column].type == LADDER ||tiles[bot_left + column].type == BACK_WALL)) { for (int i = bot_left; i > top_left; i -= column) { if (tiles[i].type >= WALL_C0 && tiles[i].type <= WALL_V4) { state = STUCK; break; } } if (state == STUCK) box.x = (top_left % column + 1) * TILE_WIDTH; else { if (tiles[bot_left + column].type == BACK_WALL) { state = FALL; }else{ box.x -= speed; } } } }else if(dir == SDLK_SPACE){ int top_center = get_tile_pos(box.x + box.w/2, box.y - speed); if (tiles[top_center].type != BACK_WALL && tiles[top_center].type != LADDER) { //- column? state = STUCK; }else { box.y -= speed; state = JUMP; } }else if(dir == SDLK_UP){ int bot_center = get_tile_pos(box.x + box.w/2, box.y + box.h); int top_center = get_tile_pos(box.x + box.w/2, box.y-20); //why 20 here? if (tiles[bot_center].type == LADDER ) { if (tiles[top_center].type >= WALL_C0 && tiles[top_center].type <= WALL_H4) { state = STUCK; }else if ( tiles[bot_center - column].type == LADDER) { box.x = (bot_center % column) * TILE_WIDTH; box.y -= speed; state = CLIMB; } } }else if(dir == SDLK_DOWN){ int bot_center = get_tile_pos(box.x + box.w/2, box.y + box.h); if (tiles[bot_center + column].type == LADDER) { box.x = (int)(box.x / TILE_WIDTH) * TILE_WIDTH; box.y += speed; state = CLIMB; } } }else if(state == CLIMB){ if (dir == SDLK_UP) { int bot_center = get_tile_pos(box.x + box.w/2, box.y + box.h); int top_center = get_tile_pos(box.x + box.w/2, box.y-20); if (tiles[top_center].type >= WALL_C0 && tiles[top_center].type <= WALL_H4) { state = STUCK; box.y = (int) (top_center /column + 1) * TILE_HEIGHT; }else if ( tiles[bot_center - column].type == LADDER && state != STUCK) {//&& !state == STUCK box.y -= speed; }else if(tiles[bot_center - column].type == BACK_WALL){ box.y = (int) (bot_center / column) * TILE_HEIGHT - box.h; state = WALK; } }else if(dir == SDLK_DOWN){ int bot_center = get_tile_pos(box.x + box.w/2, box.y + box.h); if (tiles[bot_center].type == LADDER || tiles[bot_center + column].type == LADDER) { box.x = (int)(box.x / TILE_WIDTH) * TILE_WIDTH; if ( tiles[bot_center + column].type == LADDER) { box.y += speed; }else if(tiles[bot_center + column].type >= WALL_H0 && tiles[bot_center + column].type <= WALL_H4){ box.y = (int) ((bot_center / column) +1) * TILE_HEIGHT - box.h; state = WALK; } } }else if(dir == SDLK_RIGHT){ int top_right = get_tile_pos(box.x + box.w , box.y) ; if (tiles[top_right+ column+1].type == TUNNEL) state = CRAWL; else state = STUCK; }else if(dir == SDLK_LEFT){ int top_left = get_tile_pos(box.x, box.y); if (tiles[top_left+ column-1].type == TUNNEL) state = CRAWL; else state = STUCK; } }else if(state == CRAWL){ if (dir == SDLK_RIGHT) { int top_right = get_tile_pos(box.x + box.w+ speed, box.y)+column + 1; //std::cout<<"r->"<<top_right<<std::endl; if (tiles[top_right].type == TUNNEL) box.x += speed; else if (tiles[top_right].type == BACK_WALL ) state = FALL; else if(tiles[top_right].type == LADDER) state = CLIMB; else { state = STUCK; box.x = (top_right % column) * TILE_WIDTH; } }else if(dir == SDLK_LEFT){ int top_left = get_tile_pos(box.x - speed, box.y) + column; //std::cout<<"l->"<<top_left<<std::endl; if (tiles[top_left].type == TUNNEL) box.x -= speed; else if (tiles[top_left].type == BACK_WALL){ state = FALL; }else { state = STUCK; box.x = (top_left % column ) * TILE_WIDTH; } } }else if(state == FALL){ } interact_with_level(&box); //a passive interact and an active interact? //dirty prototype for now if (dir == SDLK_RETURN) { Exit * exit = dynamic_cast<Exit *>(sprite_list[0]); if (exit->is_open()) { state = EXIT; } } return state; }