Skip to content
This repository has been archived by the owner on Mar 3, 2022. It is now read-only.

Commit

Permalink
Unified the textures of doors/characters events
Browse files Browse the repository at this point in the history
A step for issue #113 (The same should be done for the animated elements on the maps).
  • Loading branch information
milkyroute committed May 25, 2020
1 parent 775d1a8 commit 1d942f7
Show file tree
Hide file tree
Showing 21 changed files with 107 additions and 98 deletions.
2 changes: 1 addition & 1 deletion OpMon-Data
Submodule OpMon-Data updated 212 files
4 changes: 2 additions & 2 deletions src/opmon/screens/overworld/Overworld.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -284,7 +284,7 @@ namespace OpMon {

//Drawing events under the player
for(Elements::AbstractEvent *event : current->getEvents()) {
event->updateTexture();
event->updateFrame();
}

if(!is_in_dialog && data.getPlayer().getPosition().isAnim()) {
Expand Down Expand Up @@ -334,7 +334,7 @@ namespace OpMon {

//Drawing the events above the player
for(Elements::AbstractEvent *event : current->getEvents()) {
event->updateTexture();
event->updateFrame();
}

updateElements();
Expand Down
10 changes: 4 additions & 6 deletions src/opmon/screens/overworld/OverworldData.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ namespace OpMon {
listFile >> listJson;
if(listJson.contains("events")){
for(nlohmann::json element : listJson.at("events")){
Utils::ResourceLoader::loadTextureArray(eventsTextures[element.at("id")], element.at("path"), element.at("texturesnb"), element.value("offset", 0));
Utils::ResourceLoader::load(eventsTextures[element.at("id")], element.at("path"));
}
}
if(listJson.contains("elements")) {
Expand All @@ -82,8 +82,6 @@ namespace OpMon {
}
}

eventsTextures.emplace("alpha", alphaTab);

//Items initialisation
for(std::filesystem::directory_entry const& file : std::filesystem::directory_iterator(Path::getResourcePath() + "data/items")) {
if(file.is_regular_file()){
Expand Down Expand Up @@ -176,12 +174,12 @@ namespace OpMon {
return getMap(player->getMapId());
}

std::vector<sf::Texture> &OverworldData::getEventsTexture(std::string const &key) { //Uncomment commented lines when C++20 is commonly used
sf::Texture &OverworldData::getEventsTexture(std::string const &key) { //Uncomment commented lines when C++20 is commonly used
//#if __cplusplus > 201703L
//if(!eventsTextures.contains(key)){
//#else
bool contains = false;
for(std::pair<std::string, std::vector<sf::Texture> > pair : eventsTextures){
for(std::pair<std::string, sf::Texture> pair : eventsTextures){
if(pair.first == key) {
contains = true;
break;
Expand All @@ -190,7 +188,7 @@ namespace OpMon {
if(!contains){
//#endif
Utils::Log::warn("Event texture key " + key + " not found. Returning alpha.");
return eventsTextures["alpha"];
return alpha;
}else return eventsTextures[key];
}

Expand Down
14 changes: 2 additions & 12 deletions src/opmon/screens/overworld/OverworldData.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ class Map;
std::map<std::string, unsigned int> elementsCounter;
std::map<std::string, std::vector<sf::Texture>> elementsTextures;

std::map<std::string, std::vector<sf::Texture>> eventsTextures;
std::map<std::string, sf::Texture> eventsTextures;

std::map<std::string, std::unique_ptr<Item>> itemsList;

Expand Down Expand Up @@ -102,20 +102,10 @@ class Map;
*/
sf::Texture &getCurrentElementTexture(std::string const &id) { return elementsTextures[id][elementsCounter[id]]; }

/*!
* \brief Gets the textures of a character.
* \deprecated Use getEventsTexture, charaTextures have been merged with eventsTextures.
*/
OP_DEPRECATED std::vector<sf::Texture> &getCharaTexture(std::string const &key) { return eventsTextures[key]; }
/*!
* \brief Gets the textures of a door.
* \deprecated Use getEventsTexture, doorsTextures have been merged with eventsTextures.
*/
OP_DEPRECATED std::vector<sf::Texture> &getDoorsTexture(std::string const &key) { return eventsTextures[key]; }
/*!
* \brief Gets the textures of an event.
*/
std::vector<sf::Texture> &getEventsTexture(std::string const &key);
sf::Texture &getEventsTexture(std::string const &key);

/*!
* \brief Gets a completion.
Expand Down
20 changes: 11 additions & 9 deletions src/opmon/view/elements/events/AbstractEvent.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,32 +5,34 @@

namespace OpMon {
namespace Elements {
AbstractEvent::AbstractEvent(std::vector<sf::Texture> &otherTextures, EventTrigger eventTrigger, sf::Vector2f const &position, int sides, bool passable)
: otherTextures(otherTextures)
AbstractEvent::AbstractEvent(sf::Texture &texture, std::vector<sf::IntRect> rectangles , EventTrigger eventTrigger, sf::Vector2f const &position, int sides, bool passable)
: texture(texture)
, rectangles(rectangles)
, eventTrigger(eventTrigger)
, position(32.0f * position)
, mapPos(position, true)
, passable(passable)
, sides(sides)
, sprite(new sf::Sprite())
, currentTexture(otherTextures.begin()) {
, sprite(new sf::Sprite(texture))
, currentFrame(rectangles.begin()) {

}

AbstractEvent::AbstractEvent(OverworldData &data, nlohmann::json jsonData)
: otherTextures(data.getEventsTexture(jsonData.at("textures")))
: texture(data.getEventsTexture(jsonData.at("textures")))
, rectangles({sf::IntRect()})
, eventTrigger(jsonData.value("trigger", EventTrigger::PRESS))
, position(32.0f * sf::Vector2f(jsonData.value("position", std::vector<int>{0,0})[0], jsonData.value("position", std::vector<int>{0,0})[1]))
, mapPos((1.0f / 32.0f) * position, true)
, passable(jsonData.value("passable", true))
, sides(jsonData.value("side", SIDE_ALL))
, sprite(new sf::Sprite())
, currentTexture(otherTextures.begin()) {
, sprite(new sf::Sprite(texture))
, currentFrame(rectangles.begin()) {
}

void AbstractEvent::updateTexture() {
void AbstractEvent::updateFrame() {
this->sprite->setPosition(position);
this->sprite->setTexture(*currentTexture);
this->sprite->setTextureRect(*currentFrame);
}

void AbstractEvent::setPosition(sf::Vector2i pos) {
Expand Down
29 changes: 19 additions & 10 deletions src/opmon/view/elements/events/AbstractEvent.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

#include <memory>
#include <SFML/Graphics/Sprite.hpp>
#include <SFML/Graphics/Rect.hpp>
#include "src/opmon/core/Player.hpp"
#include "src/nlohmann/json.hpp"

Expand Down Expand Up @@ -79,20 +80,28 @@ namespace OpMon{
* \brief The position of the even of the map.
*/
Position mapPos;

/*!
* \brief Other textures used by the event.
* \brief The texture used by the event.
*/
sf::Texture texture;

/*!
* \brief The list of rectangles used to animate the event if need.
*
* The texture can be a table of different frames for an animation, and this vector lists the bounds of each frame on the texture.
*/
std::vector<sf::Texture> &otherTextures;
std::vector<sf::IntRect> rectangles;
/*!
* \brief An iterator to the current texture in \ref otherTextures.
* \brief An iterator to the current frame in \ref rectangles.
*/
std::vector<sf::Texture>::iterator currentTexture;
std::vector<sf::IntRect>::iterator currentFrame;

public:
/*!
* \warning The parameter position represents the position in squares, unlike the field position which stores the position in pixels.
*/
AbstractEvent(std::vector<sf::Texture> &otherTextures, EventTrigger eventTrigger, sf::Vector2f const &position, int sides, bool passable);
AbstractEvent(sf::Texture &texture, std::vector<sf::IntRect> rectangles, EventTrigger eventTrigger, sf::Vector2f const &position, int sides, bool passable);
AbstractEvent(OverworldData &data, nlohmann::json jsonData);
virtual ~AbstractEvent() = default;
/*!
Expand All @@ -106,13 +115,13 @@ namespace OpMon{
/*!
* \brief Updates \ref sprite with \ref currentTexture and \ref position.
*/
virtual void updateTexture();
virtual void updateFrame();

int getSide() const {
return sides;
}
virtual const sf::Texture &getTexture() {
return *currentTexture;
virtual const sf::IntRect &getFrameRect() {
return *currentFrame;
}
EventTrigger getEventTrigger() const {
return eventTrigger;
Expand All @@ -137,14 +146,14 @@ namespace OpMon{
/*!
* \brief Sets the current texture to the first texture of \ref otherTextures.
*/
void resetTexture() {currentTexture = otherTextures.begin();}
void resetFrame() {currentFrame = rectangles.begin();}

/*!
* \brief If the activation of the event is over, returns true. If the event is still doing something, returns false.
*/
virtual bool isOver() const = 0;

std::vector<sf::Texture>& getTextures() {return otherTextures;}
std::vector<sf::IntRect>& getRectangles() {return rectangles;}

/*!
* \brief Changes the position of the event.
Expand Down
4 changes: 2 additions & 2 deletions src/opmon/view/elements/events/AbstractMetaEvent.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -64,11 +64,11 @@ namespace OpMon{
/*!
* \brief Returns the texture of \ref mainEvent.
*/
virtual const sf::Texture &getTexture() {return mainEvent->getTexture();}
virtual const sf::IntRect &getFrameRect() {return mainEvent->getFrameRect();}
/*!
* \brief Updates the texture of \ref mainEvent.
*/
virtual void updateTexture() {mainEvent->updateTexture();}
virtual void updateFrame() {mainEvent->updateFrame();}

/*!
* \brief Changes the position of the event.
Expand Down
26 changes: 18 additions & 8 deletions src/opmon/view/elements/events/AnimationEvent.cpp
Original file line number Diff line number Diff line change
@@ -1,37 +1,47 @@
#include "AnimationEvent.hpp"
#include "src/utils/exceptions.hpp"
#include "src/utils/log.hpp"

namespace OpMon {
namespace Elements {

AnimationEvent::AnimationEvent(std::vector<sf::Texture> &otherTextures, EventTrigger eventTrigger, sf::Vector2f const &position, unsigned int framerate, bool loop, bool passable, bool lastTexture, int sides)
: AbstractEvent(otherTextures, eventTrigger, position, sides, passable)
AnimationEvent::AnimationEvent(sf::Texture &texture, std::vector<sf::IntRect> rectangles, EventTrigger eventTrigger, sf::Vector2f const &position, unsigned int framerate, bool loop, bool passable, bool lastTexture, int sides)
: AbstractEvent(texture, rectangles, eventTrigger, position, sides, passable)
, framerate(framerate)
, loop(loop)
, lastTexture(lastTexture) {
}

AnimationEvent::AnimationEvent(OverworldData &data, nlohmann::json jsonData)
AnimationEvent::AnimationEvent(OverworldData &data, nlohmann::json jsonData, std::vector<sf::IntRect> rectangles)
: AbstractEvent(data, jsonData)
, framerate(jsonData.value("framerate", 0))
, loop(jsonData.value("loop", false))
, lastTexture(jsonData.value("lastTexture", true)){
if(rectangles.size() == 0){
for(nlohmann::json rect : jsonData.value("rectangles", nlohmann::json())){
this->rectangles.push_back(sf::IntRect(rect[0], rect[1], rect[2], rect[3]));
}
if(this->rectangles.size() == 0){
Utils::Log::warn("Empty rectangle in AnimationEvent.");
rectangles.push_back(sf::IntRect());
}
} else this->rectangles = rectangles;

}

void AnimationEvent::action(Player &player, Overworld &overworld){
playing = !(playing && loop); //Stops playing only if it's a loop. Else, stay playing or start playing.
resetTexture();
resetFrame();
}

void AnimationEvent::update(Player &player, Overworld &overworld){
if(playing && (framecount >= framerate)){
++currentTexture;
++currentFrame;
framecount = 0;
if(currentTexture == otherTextures.end()){
currentTexture = otherTextures.begin();
if(currentFrame == rectangles.end()){
currentFrame = rectangles.begin();
playing = loop; //If loop, continue playing, else, stop.
if(!playing && lastTexture) currentTexture = otherTextures.end() - 1;
if(!playing && lastTexture) currentFrame = rectangles.end() - 1;
}
}else if(playing && (framecount < framerate)){
framecount++;
Expand Down
4 changes: 2 additions & 2 deletions src/opmon/view/elements/events/AnimationEvent.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,8 @@ namespace OpMon::Elements {
*/
bool lastTexture;
public:
AnimationEvent(std::vector<sf::Texture> &otherTextures, EventTrigger eventTrigger, sf::Vector2f const &position, unsigned int framerate, bool loop, bool passable, bool lastTexture = true, int sides = SIDE_ALL);
AnimationEvent(OverworldData &data, nlohmann::json jsonData);
AnimationEvent(sf::Texture &texture, std::vector<sf::IntRect> rectangles, EventTrigger eventTrigger, sf::Vector2f const &position, unsigned int framerate, bool loop, bool passable, bool lastTexture = true, int sides = SIDE_ALL);
AnimationEvent(OverworldData &data, nlohmann::json jsonData, std::vector<sf::IntRect> rectangles = {});
void action(Player &player, Overworld &overworld);
void update(Player &player, Overworld &overworld);
bool isOver() const {return !playing;}
Expand Down
9 changes: 5 additions & 4 deletions src/opmon/view/elements/events/BattleEvent.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,16 @@
namespace OpMon {
namespace Elements {

BattleEvent::BattleEvent(std::vector<sf::Texture> &textures, sf::Vector2f const &position, OpTeam *team, EventTrigger eventTrigger, bool passable, int side)
: AbstractEvent(textures, eventTrigger, position, side, passable)
BattleEvent::BattleEvent(sf::Texture &texture, std::vector<sf::IntRect> rectangles, sf::Vector2f const &position, OpTeam *team, EventTrigger eventTrigger, bool passable, int side)
: AbstractEvent(texture, rectangles, eventTrigger, position, side, passable)
, team(team){
}

BattleEvent::BattleEvent(OverworldData &data, nlohmann::json jsonData)
: AbstractEvent(data, jsonData)
, team(data.getTrainer(jsonData.at("trainer"))){

, team(data.getTrainer(jsonData.at("trainer"))) {
this->rectangles = std::vector<sf::IntRect>{sf::IntRect(0, 0, texture.getSize().x, texture.getSize().y)};
this->currentFrame = rectangles.begin();
}

void BattleEvent::action(Player &player, Overworld &overworld) {
Expand Down
2 changes: 1 addition & 1 deletion src/opmon/view/elements/events/BattleEvent.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ namespace OpMon::Elements {
*/
bool over = true;
public:
BattleEvent(std::vector<sf::Texture> &textures, sf::Vector2f const &position, OpTeam *team, EventTrigger eventTrigger = EventTrigger::PRESS, bool passable = false, int side = SIDE_ALL);
BattleEvent(sf::Texture &texture, std::vector<sf::IntRect> rectangles, sf::Vector2f const &position, OpTeam *team, EventTrigger eventTrigger = EventTrigger::PRESS, bool passable = false, int side = SIDE_ALL);
BattleEvent(OverworldData &data, nlohmann::json jsonData);

virtual void update(Player &player, Overworld &overworld);
Expand Down
Loading

0 comments on commit 1d942f7

Please sign in to comment.