void Enemy::chase(sf::Vector2f pos) { action_tree.deleteThread(1); Action chase; chase.thread_id = 1; chase.action = get_action([this, pos](sf::Time dt) { sf::Vector2f normal = normalize(pos.x - getPosition().x, pos.y - getPosition().y); float x = normal.x; float y = normal.y; if ((isCollisionDirection(Direction::Left) && x < 0.f) || (isCollisionDirection(Direction::Right) && x > 0.f)) { x = 0.f; if (y > 0.f) y = 1.f; else if (y < 0.f) y = -1.f; } if ((isCollisionDirection(Direction::Up) && y < 0.f) || (isCollisionDirection(Direction::Down) && y > 0.f)) { y = 0.f; if (x > 0.f)x = 1.f; else if (x < 0.f) x = -1.f; } x = x * 100.f * dt.asSeconds(); y = y * 100.f * dt.asSeconds(); playAnimation(); if (x > 0 && abs(x) > abs(y)) { if (getCurrentDirection() != Direction::Right) { setCurrentDirection(Direction::Right); changeAnimation(static_cast<int>(Animations::GoRight)); } } if (x < 0 && abs(x) > abs(y)) { if (getCurrentDirection() != Direction::Left) { setCurrentDirection(Direction::Left); changeAnimation(static_cast<int>(Animations::GoLeft)); } } if (y > 0 && abs(x) < abs(y)) { if (getCurrentDirection() != Direction::Down) { setCurrentDirection(Direction::Down); changeAnimation(static_cast<int>(Animations::GoDown)); } } if (y < 0 && abs(x) < abs(y)) { if (getCurrentDirection() != Direction::Up) { setCurrentDirection(Direction::Up); changeAnimation(static_cast<int>(Animations::GoUp)); } } //std::cout <<x << " " << y << std::endl; //std::cout << this->isCollisionDirection(Direction::Left) << " " << this->isCollisionDirection(Direction::Right) << " " << isCollisionDirection(Direction::Down) << " " << isCollisionDirection(Direction::Up) << std::endl; move(sf::Vector2f(x, y)); return false; }); pushAction(chase); }
//The ghosts cannot go to the opposite site void Enemy::discardOppositeDirection() { if (getCurrentDirection() == Direction::Up) removeDirection(Direction::Down); else if (getCurrentDirection() == Direction::Down) removeDirection(Direction::Up); else if (getCurrentDirection() == Direction::Left) removeDirection(Direction::Right); else if (getCurrentDirection() == Direction::Right) removeDirection(Direction::Left); }
void Npc::updateCurrent(CommandQueue &command_queue, const sf::Time dt) { if (isActive()) { updateAnimation(dt); action_queue.update(dt); if (getCurrentDirection() == Direction::Left) goLeft(dt); else if (getCurrentDirection() == Direction::Right) goRight(dt); else if (getCurrentDirection() == Direction::Up) goUp(dt); else if (getCurrentDirection() == Direction::Down) goDown(dt); } }
void Enemy::patrol() { std::discrete_distribution<int> dir_distribution({ 60, 10, 10, 10, 10 }); std::uniform_int_distribution<int> distance_distribution(50, 100); std::uniform_real_distribution<float> wait_time_distribution(1.f, 3.f); Direction dir = static_cast<Direction>(dir_distribution(mt)); int distance = distance_distribution(mt); stop_time = sf::seconds(wait_time_distribution(mt)); //Action selection Action patrol; patrol.thread_id = 1; patrol.action = get_action([this, dir, distance](sf::Time dt) { if (dir == Direction::Left) { moveLeft(distance); } else if (dir == Direction::Right) { moveRight(distance); } else if (dir == Direction::Up) { moveUp(distance); } else if (dir == Direction::Down) { moveDown(distance); } //else if (dir == Direction::None) { std::cout << "Stop time" << stop_time.asSeconds() << std::endl; wait(stop_time); } return false; }); //When we have 2 actions we stop action addition unsigned int max_actions = 2; if (action_tree.threadSize(patrol.thread_id) < max_actions) { pushAction(patrol); } //Action updating, each frame Action patrol_update; patrol_update.thread_id = 0; patrol_update.action = get_action([this](sf::Time dt) { if (getCurrentDirection() == Direction::Left) { goLeft(dt); } else if (getCurrentDirection() == Direction::Right) { goRight(dt); } else if (getCurrentDirection() == Direction::Up) { goUp(dt); } else if (getCurrentDirection() == Direction::Down) { goDown(dt); } return false; }); pushAction(patrol_update); }
void Pacman::draw() const { glPushMatrix(); glTranslatef(0.5 + getX(), 0.5, -0.5 + getY()); auto direction = getCurrentDirection(); if (direction == Direction::North) { glRotatef(-90, 0, 1, 0); } else if (direction == Direction::East) { glRotatef(-180, 0, 1, 0); } else if (direction == Direction::South) { glRotatef(-270, 0, 1, 0); } Drawable::draw(); glScalef(size, size, size); glPushMatrix(); glRotatef(-frame_no, 0, 0, 1); sphere->draw(); glPopMatrix(); glPushMatrix(); glRotatef(180 + frame_no, 0, 0, 1); sphere->draw(); glPopMatrix(); glPopMatrix(); }
int getInput() { SceCtrlData pad; // if(PSPGOAvail) // inputMethod = GO_METHOD; // else inputMethod = GENERAL_A; sceCtrlReadBufferPositive(&pad, 1); if(inputMethod==GENERAL_A) { if(pad.Buttons & PSP_CTRL_UP) return DIR_UP; else if(pad.Buttons & PSP_CTRL_RIGHT) return DIR_RIGHT; else if(pad.Buttons & PSP_CTRL_DOWN) return DIR_DOWN; else if(pad.Buttons & PSP_CTRL_LEFT) return DIR_LEFT; /* } else if(inputMethod==GO_METHOD) {*/ if(pad.Buttons & PSP_CTRL_LTRIGGER && pad.Buttons & PSP_CTRL_RTRIGGER) return 0; if(pad.Buttons & PSP_CTRL_LTRIGGER && !ltriggerPressed) { ltriggerPressed = 1; if(getCurrentDirection() == DIR_UP) { return DIR_LEFT; } else if(getCurrentDirection() == DIR_RIGHT) { return DIR_UP; } else if(getCurrentDirection() == DIR_DOWN) { return DIR_RIGHT; } else if(getCurrentDirection() == DIR_LEFT) { return DIR_DOWN; } } else if(!(pad.Buttons & PSP_CTRL_LTRIGGER)) { ltriggerPressed = 0; } if(pad.Buttons & PSP_CTRL_RTRIGGER && !rtriggerPressed) { rtriggerPressed = 1; if(getCurrentDirection() == DIR_UP) { return DIR_RIGHT; } else if(getCurrentDirection() == DIR_RIGHT) { return DIR_DOWN; } else if(getCurrentDirection() == DIR_DOWN) { return DIR_LEFT; } else if(getCurrentDirection() == DIR_LEFT) { return DIR_UP; } } else if(!(pad.Buttons & PSP_CTRL_RTRIGGER)) { rtriggerPressed=0; } } else return 0; return 0; }