int Ghost::eventHandler(Event *p_e) { if (p_e->getType() == HERO_MOVE_EVENT) { EventHeroMove *p_hm_e = static_cast<EventHeroMove *>(p_e); hero_pos = p_hm_e->getPos(); return 1; } if (p_e->getType() == STEP_EVENT) { speed_cooldown--; if (speed_cooldown == 0) { speed_cooldown = max_speed_cooldown; move_to_hero(); } return 1; } if (p_e->getType() == COLLISION_EVENT) { EventCollision *p_c_e = static_cast<EventCollision *>(p_e); handleCollision(p_c_e); } if (p_e->getType() == LEVEL_UP_EVENT) { EventLevelUp *le = static_cast<EventLevelUp *>(p_e); max_speed_cooldown -= le->getLevel(); ghost_velocity += GHOST_VELOCITY * le->getLevel(); if (max_speed_cooldown < 1){ max_speed_cooldown = 1; } } return 0; }
bool Computer::movement(Maze level_map) { const auto Q_POSSIBILITIES = 4U; SegmentDistribution distributed_move[Q_POSSIBILITIES]; Point shift; Point temp; bool result, approval1 = false, approval2 = false, approval3 = false; unsigned left_boundary = 0; unsigned root_decision, decision; if(move_to_hero(level_map) == true) return true; //относительно героя компьютера if (prev_position == position) { shift.x = 0; shift.y = -1; } else { shift.x = position.x - prev_position.x; shift.y = position.y - prev_position.y; } distributed_move[0].probability = 70; // Смещение вперёд distributed_move[0].shift = shift; distributed_move[1].probability = 2; // Смещение назад distributed_move[1].shift.x = -shift.x; distributed_move[1].shift.y = -shift.y; distributed_move[2].probability = 14; // Смещение влево distributed_move[2].shift.x = shift.y; distributed_move[2].shift.y = -shift.x; distributed_move[3].probability = 14; // Смещение вправо distributed_move[3].shift.x = -shift.y; distributed_move[3].shift.y = shift.x; for (auto i = 0U; i < Q_POSSIBILITIES; ++i) { temp.x = position.x + distributed_move[i].shift.x; temp.y = position.y + distributed_move[i].shift.y; if (level_map.isExistance(temp.y, temp.x)) { approval1 = level_map.getCell(temp.y, temp.x) != '#'; approval2 = level_map.getCell(temp.y, temp.x) != 'E'; approval3 = level_map.getCell(temp.y, temp.x) != 'M'; } result = approval1 && approval2 && approval3; distributed_move[i].use = result; if (result == true) left_boundary += distributed_move[i].probability; } if (left_boundary == 0) return true; root_decision = rand() % left_boundary; for (auto i = 0U; i < Q_POSSIBILITIES; ++i) { if (!distributed_move[i].use) continue; if (root_decision < distributed_move[i].probability) { decision = i; break; } else root_decision -= distributed_move[i].probability; } prev_position = position; position.x += distributed_move[decision].shift.x; position.y += distributed_move[decision].shift.y; return true; }