From dcec81c7d5426a7f48762067f9f964befa7b4388 Mon Sep 17 00:00:00 2001 From: Alayan Date: Sat, 29 Sep 2018 15:42:50 +0200 Subject: [PATCH] Add an alert icon when another kart is nearby --- src/states_screens/race_gui.cpp | 54 +++++ src/states_screens/race_gui.hpp | 1 + src/states_screens/race_gui_base.cpp | 311 ++++++++++++++------------- src/states_screens/race_gui_base.hpp | 1 + 4 files changed, 216 insertions(+), 151 deletions(-) diff --git a/src/states_screens/race_gui.cpp b/src/states_screens/race_gui.cpp index dfd0105ed..878b8f285 100644 --- a/src/states_screens/race_gui.cpp +++ b/src/states_screens/race_gui.cpp @@ -39,6 +39,7 @@ using namespace irr; #include "guiengine/scalable_font.hpp" #include "io/file_manager.hpp" #include "items/powerup_manager.hpp" +#include "items/projectile_manager.hpp" #include "karts/abstract_kart.hpp" #include "karts/controller/controller.hpp" #include "karts/controller/spare_tire_ai.hpp" @@ -193,6 +194,9 @@ RaceGUI::RaceGUI() m_blue_flag = irr_driver->getTexture(FileManager::GUI_ICON, "blue_flag.png"); m_soccer_ball = irr_driver->getTexture(FileManager::GUI_ICON, "soccer_ball_normal.png"); m_heart_icon = irr_driver->getTexture(FileManager::GUI_ICON, "heart.png"); + m_danger[0] = irr_driver->getTexture(FileManager::GUI_ICON, "alert_nodanger.png"); + m_danger[1] = irr_driver->getTexture(FileManager::GUI_ICON, "alert_danger.png"); + m_danger[2] = irr_driver->getTexture(FileManager::GUI_ICON, "alert_bigdanger.png"); } // RaceGUI //----------------------------------------------------------------------------- @@ -812,6 +816,56 @@ void RaceGUI::drawMiscInfo(const AbstractKart *kart, font->setScale(1.0f); return; } + else if (race_manager->isLinearRaceMode()) + { + float closest_kart_dist_squared = 99999.9f; + int closest_kart_id = -1; + + World *world = World::getWorld(); + + for(unsigned int i=0;igetNumKarts();i++) + { + float dist2 = world->getKart(i)->getXYZ().distance2(kart->getXYZ()); + if (dist2 > 0 && dist2 < closest_kart_dist_squared) + { + closest_kart_id = i; + closest_kart_dist_squared = dist2; + } + } + + int projectile_types[4]; //[3] basket, [2] cakes, [1] plunger, [0] bowling + projectile_types[0] = projectile_manager->getNearbyProjectileCount(kart, 15.0f /*alert radius*/, + PowerupManager::POWERUP_BOWLING); + projectile_types[1] = projectile_manager->getNearbyProjectileCount(kart, 20.0f /*alert radius*/, + PowerupManager::POWERUP_PLUNGER); + projectile_types[2] = projectile_manager->getNearbyProjectileCount(kart, 25.0f /*alert radius*/, + PowerupManager::POWERUP_CAKE); + projectile_types[3] = projectile_manager->getNearbyProjectileCount(kart, 50.0f /*alert radius*/, + PowerupManager::POWERUP_RUBBERBALL); + + int icon_to_use = 0; + + if (closest_kart_id >= 0 && closest_kart_dist_squared < 900.0f) + icon_to_use = 1; + + + int icon_width = meter_width/2; + int x = int(offset.X + 0.375f*meter_width); + int y = int(offset.Y - 0.235f*meter_height) - icon_width; + core::rect indicator_pos(x, y, x + icon_width, y + icon_width); + core::rect source_rect(core::position2d(0,0), + m_danger[icon_to_use]->getSize()); + draw2DImage(m_danger[icon_to_use],indicator_pos,source_rect, + NULL,NULL,true); + + // Draw kart's icon + if (icon_to_use == 1) + { + drawPlayerIcon(world->getKart(closest_kart_id), + x+0.1875f*icon_width,y+0.1875f*icon_width, + 0.625f*icon_width); + } + } } // drawMiscInfo //----------------------------------------------------------------------------- diff --git a/src/states_screens/race_gui.hpp b/src/states_screens/race_gui.hpp index 7acedff41..fc5fc2562 100644 --- a/src/states_screens/race_gui.hpp +++ b/src/states_screens/race_gui.hpp @@ -100,6 +100,7 @@ private: irr::video::ITexture *m_blue_flag; irr::video::ITexture *m_soccer_ball; irr::video::ITexture *m_heart_icon; + irr::video::ITexture *m_danger[2]; /** Animation state: none, getting smaller (old value), * getting bigger (new number). */ diff --git a/src/states_screens/race_gui_base.cpp b/src/states_screens/race_gui_base.cpp index cebbc44f2..9817e7930 100644 --- a/src/states_screens/race_gui_base.cpp +++ b/src/states_screens/race_gui_base.cpp @@ -717,7 +717,6 @@ void RaceGUIBase::drawGlobalPlayerIcons(int bottom_margin) int ICON_WIDTH = ICON_PLAYER_WIDTH * 5 / 6; WorldWithRank* world = dynamic_cast(World::getWorld()); - CaptureTheFlag* ctf = dynamic_cast(World::getWorld()); //initialize m_previous_icons_position if(m_previous_icons_position.size()==0) @@ -865,163 +864,173 @@ void RaceGUIBase::drawGlobalPlayerIcons(int bottom_margin) true /* ignore RTL */); } - // draw icon - video::ITexture *icon = - kart->getKartProperties()->getIconMaterial()->getTexture(); int w = kart->getController() ->isLocalPlayerController() ? ICON_PLAYER_WIDTH : ICON_WIDTH; - - // CTF - if (ctf) - { - if (ctf->getRedHolder() == (int)kart_id) - { - video::ITexture* red = - irr_driver->getTexture(FileManager::GUI_ICON, "red_flag.png"); - const core::rect rect(core::position2d(0, 0), - red->getSize()); - const core::rect pos1 - (x - 20, y - 10, x + w - 20, y + w - 30); - draw2DImage(red, pos1, rect, NULL, NULL, true); - } - else if (ctf->getBlueHolder() == (int)kart_id) - { - video::ITexture* blue = - irr_driver->getTexture(FileManager::GUI_ICON, "blue_flag.png"); - const core::rect rect(core::position2d(0, 0), - blue->getSize()); - const core::rect pos1 - (x - 20, y - 10, x + w - 20, y + w - 30); - draw2DImage(blue, pos1, rect, NULL, NULL, true); - } - } - - const core::rect pos(x, y, x+w, y+w); - - //to bring to light the player's icon: add a background - if (kart->getController()->isLocalPlayerController() && - m_icons_frame != NULL) - { - video::SColor colors[4]; - for (unsigned int i=0;i<4;i++) - { - colors[i]=kart->getKartProperties()->getColor(); - colors[i].setAlpha( - 100+(int)(100*cos(M_PI/2*i+World::getWorld()->getTime()*2))); - } - const core::rect rect(core::position2d(0,0), - m_icons_frame->getSize()); - draw2DImage(m_icons_frame, pos, rect,NULL, colors, true); - } - - // Fixes crash bug, why are certain icons not showing up? - if (icon && !kart->getKartAnimation() && !kart->isSquashed()) - { - const core::rect rect(core::position2d(0,0), - icon->getSize()); - draw2DImage(icon, pos, rect, NULL, NULL, true, kart->isGhostKart()); - } - - //draw status info - icon fade out in case of rescue/explode - - if (icon && dynamic_cast(kart->getKartAnimation())) - { - //icon fades to the left - float t = kart->getKartAnimation()->getAnimationTimer(); - float t_anim=100*sin(0.5f*M_PI*t); - const core::rect rect1(core::position2d(0,0), - icon->getSize()); - const core::rect pos1((int)(x-t_anim), y, - (int)(x+w-t_anim), y+w); - draw2DImage(icon, pos1, rect1, - NULL, NULL, true); - } - - if (icon && !kart->getKartAnimation() && kart->isSquashed() ) - { - //syncs icon squash with kart squash - const core::rect destRect(core::position2d(x,y+w/4), - core::position2d(x+w,y+w*3/4)); - const core::rect sourceRect(core::position2d(0,0), - icon->getSize()); - draw2DImage(icon, destRect, - sourceRect, NULL, NULL, - true); - } - - if (icon && - dynamic_cast(kart->getKartAnimation()) ) - { - //exploses into 4 parts - float t = kart->getKartAnimation()->getAnimationTimer(); - float t_anim=50.0f*sin(0.5f*M_PI*t); - u16 icon_size_x=icon->getSize().Width; - u16 icon_size_y=icon->getSize().Height; - - const core::rect rect1(0, 0, icon_size_x/2,icon_size_y/2); - const core::rect pos1((int)(x-t_anim), (int)(y-t_anim), - (int)(x+w/2-t_anim), - (int)(y+w/2-t_anim)); - draw2DImage(icon, pos1, rect1, - NULL, NULL, true); - - const core::rect rect2(icon_size_x/2,0, - icon_size_x,icon_size_y/2); - const core::rect pos2((int)(x+w/2+t_anim), - (int)(y-t_anim), - (int)(x+w+t_anim), - (int)(y+w/2-t_anim)); - draw2DImage(icon, pos2, rect2, - NULL, NULL, true); - - const core::rect rect3(0, icon_size_y/2, icon_size_x/2,icon_size_y); - const core::rect pos3((int)(x-t_anim), (int)(y+w/2+t_anim), - (int)(x+w/2-t_anim), (int)(y+w+t_anim)); - draw2DImage(icon, pos3, rect3, NULL, NULL, true); - - const core::rect rect4(icon_size_x/2,icon_size_y/2,icon_size_x,icon_size_y); - const core::rect pos4((int)(x+w/2+t_anim), (int)(y+w/2+t_anim), - (int)(x+w+t_anim), (int)(y+w+t_anim)); - draw2DImage(icon, pos4, rect4, NULL, NULL, true); - } - - //Plunger - if (kart->getBlockedByPlungerTicks()>0) - { - video::ITexture *icon_plunger = - powerup_manager->getIcon(PowerupManager::POWERUP_PLUNGER)->getTexture(); - if (icon_plunger != NULL) - { - const core::rect rect(core::position2d(0,0), - icon_plunger->getSize()); - const core::rect pos1(x+10, y-10, x+w+10, y+w-10); - draw2DImage(icon_plunger, pos1, - rect, NULL, NULL, - true); - } - } - //attachment - if (kart->getAttachment()->getType() != Attachment::ATTACH_NOTHING) - { - video::ITexture *icon_attachment = - attachment_manager->getIcon(kart->getAttachment()->getType()) - ->getTexture(); - if (icon_attachment != NULL) - { - const core::rect rect(core::position2d(0,0), - icon_attachment->getSize()); - const core::rect pos1(x-20, y-10, x+w-20, y+w-10); - draw2DImage(icon_attachment, - pos1, rect, NULL, - NULL, true); - } - } - + drawPlayerIcon(kart, x, y, w); } //next position #endif } // drawGlobalPlayerIcons +//----------------------------------------------------------------------------- +/** Draw one player icon + * Takes care of icon looking different due to plumber, squashing, ... + */ +void RaceGUIBase::drawPlayerIcon(AbstractKart *kart, int x, int y, int w) +{ + video::ITexture *icon = + kart->getKartProperties()->getIconMaterial()->getTexture(); + + CaptureTheFlag* ctf = dynamic_cast(World::getWorld()); + unsigned int kart_id = kart->getWorldKartId(); + + // CTF + if (ctf) + { + if (ctf->getRedHolder() == (int)kart_id) + { + video::ITexture* red = + irr_driver->getTexture(FileManager::GUI_ICON, "red_flag.png"); + const core::rect rect(core::position2d(0, 0), + red->getSize()); + const core::rect pos1 + (x - 20, y - 10, x + w - 20, y + w - 30); + draw2DImage(red, pos1, rect, NULL, NULL, true); + } + else if (ctf->getBlueHolder() == (int)kart_id) + { + video::ITexture* blue = + irr_driver->getTexture(FileManager::GUI_ICON, "blue_flag.png"); + const core::rect rect(core::position2d(0, 0), + blue->getSize()); + const core::rect pos1 + (x - 20, y - 10, x + w - 20, y + w - 30); + draw2DImage(blue, pos1, rect, NULL, NULL, true); + } + } + + const core::rect pos(x, y, x+w, y+w); + + //to bring to light the player's icon: add a background + if (kart->getController()->isLocalPlayerController() && + m_icons_frame != NULL) + { + video::SColor colors[4]; + for (unsigned int i=0;i<4;i++) + { + colors[i]=kart->getKartProperties()->getColor(); + colors[i].setAlpha( + 100+(int)(100*cos(M_PI/2*i+World::getWorld()->getTime()*2))); + } + const core::rect rect(core::position2d(0,0), + m_icons_frame->getSize()); + draw2DImage(m_icons_frame, pos, rect,NULL, colors, true); + } + + // Fixes crash bug, why are certain icons not showing up? + if (icon && !kart->getKartAnimation() && !kart->isSquashed()) + { + const core::rect rect(core::position2d(0,0), + icon->getSize()); + draw2DImage(icon, pos, rect, NULL, NULL, true, kart->isGhostKart()); + } + + //draw status info - icon fade out in case of rescue/explode + + if (icon && dynamic_cast(kart->getKartAnimation())) + { + //icon fades to the left + float t = kart->getKartAnimation()->getAnimationTimer(); + float t_anim=100*sin(0.5f*M_PI*t); + const core::rect rect1(core::position2d(0,0), + icon->getSize()); + const core::rect pos1((int)(x-t_anim), y, + (int)(x+w-t_anim), y+w); + draw2DImage(icon, pos1, rect1, + NULL, NULL, true); + } + + if (icon && !kart->getKartAnimation() && kart->isSquashed() ) + { + //syncs icon squash with kart squash + const core::rect destRect(core::position2d(x,y+w/4), + core::position2d(x+w,y+w*3/4)); + const core::rect sourceRect(core::position2d(0,0), + icon->getSize()); + draw2DImage(icon, destRect, + sourceRect, NULL, NULL, + true); + } + + if (icon && + dynamic_cast(kart->getKartAnimation()) ) + { + //exploses into 4 parts + float t = kart->getKartAnimation()->getAnimationTimer(); + float t_anim=50.0f*sin(0.5f*M_PI*t); + u16 icon_size_x=icon->getSize().Width; + u16 icon_size_y=icon->getSize().Height; + + const core::rect rect1(0, 0, icon_size_x/2,icon_size_y/2); + const core::rect pos1((int)(x-t_anim), (int)(y-t_anim), + (int)(x+w/2-t_anim), + (int)(y+w/2-t_anim)); + draw2DImage(icon, pos1, rect1, + NULL, NULL, true); + + const core::rect rect2(icon_size_x/2,0, + icon_size_x,icon_size_y/2); + const core::rect pos2((int)(x+w/2+t_anim), + (int)(y-t_anim), + (int)(x+w+t_anim), + (int)(y+w/2-t_anim)); + draw2DImage(icon, pos2, rect2, + NULL, NULL, true); + + const core::rect rect3(0, icon_size_y/2, icon_size_x/2,icon_size_y); + const core::rect pos3((int)(x-t_anim), (int)(y+w/2+t_anim), + (int)(x+w/2-t_anim), (int)(y+w+t_anim)); + draw2DImage(icon, pos3, rect3, NULL, NULL, true); + + const core::rect rect4(icon_size_x/2,icon_size_y/2,icon_size_x,icon_size_y); + const core::rect pos4((int)(x+w/2+t_anim), (int)(y+w/2+t_anim), + (int)(x+w+t_anim), (int)(y+w+t_anim)); + draw2DImage(icon, pos4, rect4, NULL, NULL, true); + } + + //Plunger + if (kart->getBlockedByPlungerTicks()>0) + { + video::ITexture *icon_plunger = + powerup_manager->getIcon(PowerupManager::POWERUP_PLUNGER)->getTexture(); + if (icon_plunger != NULL) + { + const core::rect rect(core::position2d(0,0), + icon_plunger->getSize()); + const core::rect pos1(x+10, y-10, x+w+10, y+w-10); + draw2DImage(icon_plunger, pos1, + rect, NULL, NULL, + true); + } + } + //attachment + if (kart->getAttachment()->getType() != Attachment::ATTACH_NOTHING) + { + video::ITexture *icon_attachment = + attachment_manager->getIcon(kart->getAttachment()->getType()) + ->getTexture(); + if (icon_attachment != NULL) + { + const core::rect rect(core::position2d(0,0), + icon_attachment->getSize()); + const core::rect pos1(x-20, y-10, x+w-20, y+w-10); + draw2DImage(icon_attachment, + pos1, rect, NULL, + NULL, true); + } + } +} // drawPlayerIcon + // ---------------------------------------------------------------------------- /** Draws the plunger-in-face if necessary. Does nothing if there is no diff --git a/src/states_screens/race_gui_base.hpp b/src/states_screens/race_gui_base.hpp index 1a09ebdeb..18cd35c87 100644 --- a/src/states_screens/race_gui_base.hpp +++ b/src/states_screens/race_gui_base.hpp @@ -246,6 +246,7 @@ public: virtual void clearAllMessages() { m_messages.clear(); } void drawGlobalPlayerIcons(int bottom_margin); + void drawPlayerIcon(AbstractKart *kart, int x, int y, int w); virtual void drawEnergyMeter(int x, int y, const AbstractKart *kart, const core::recti &viewport,