Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: attached effects on tile and possible to change draw order #628

Merged
merged 9 commits into from
Nov 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions modules/game_attachedeffects/attachedeffects.lua
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ controller = Controller:new()
g_game.getLocalPlayer():attachEffect(g_attachedEffects.getById(1))
g_game.getLocalPlayer():attachEffect(g_attachedEffects.getById(2))
g_game.getLocalPlayer():attachEffect(g_attachedEffects.getById(3))
g_game.getLocalPlayer():getTile():attachEffect(g_attachedEffects.getById(1))
end

function controller:onGameEnd()
Expand Down
1 change: 1 addition & 0 deletions modules/game_attachedeffects/effects.lua
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
speed, disableWalkAnimation, shader, drawOnUI, opacity
duration, loop, transform, hideOwner, size{width, height}
offset{x, y, onTop}, dirOffset[dir]{x, y, onTop},
light { color, intensity}, drawOrder(only for tiles),
onAttach, onDetach
}
]] --
Expand Down
11 changes: 11 additions & 0 deletions modules/game_attachedeffects/lib.lua
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,17 @@ local executeConfig = function(attachedEffect, config)
attachedEffect:setOpacity(config.opacity)
end

if config.light then
attachedEffect:setLight({
color = config.light.color or 0,
intensity = config.light.intensity or 0
})
end

if config.drawOrder then
attachedEffect:setDrawOrder(config.drawOrder)
end

if config.duration ~= nil and config.duration > 0 then
attachedEffect:setDuration(config.duration)
end
Expand Down
1 change: 1 addition & 0 deletions modules/gamelib/gamelib.otmod
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ Module
dofile 'textmessages'
dofile 'thing'
dofile 'spells'
dofile 'tile'

dofile 'eventcontroller'
dofile 'controller'
Expand Down
4 changes: 4 additions & 0 deletions modules/gamelib/thing.lua
Original file line number Diff line number Diff line change
Expand Up @@ -48,3 +48,7 @@ SpriteMaskRed = 1
SpriteMaskGreen = 2
SpriteMaskBlue = 3
SpriteMaskYellow = 4

function Thing:isTile()
return false
end
39 changes: 39 additions & 0 deletions modules/gamelib/tile.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
function Tile:isTile()
return true
end

function Tile:isCreature()
return false
end

function Tile:isLocalPlayer()
return false
end

function Tile:isNpc()
return false
end

function Tile:isMonster()
return false
end

function Tile:isPlayer()
return false
end

function Tile:isEffect()
return false
end

function Tile:isMissile()
return false
end

function Tile:isItem()
return false
end

function Tile:isContainer()
return false
end
94 changes: 48 additions & 46 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -232,52 +232,6 @@ endif()
# OTClient source files configuration
# *****************************************************************************
set(SOURCE_FILES
client/animatedtext.cpp
client/animator.cpp
client/attachedeffect.cpp
client/attachedeffectmanager.cpp
client/client.cpp
client/container.cpp
client/creature.cpp
client/creatures.cpp
client/effect.cpp
client/game.cpp
client/gameconfig.cpp
client/houses.cpp
client/item.cpp
client/itemtype.cpp
client/lightview.cpp
client/localplayer.cpp
client/luafunctions.cpp
client/luavaluecasts.cpp
client/map.cpp
client/mapio.cpp
client/mapview.cpp
client/minimap.cpp
client/missile.cpp
client/outfit.cpp
client/player.cpp
client/position.cpp
client/protocolcodes.cpp
client/protocolgame.cpp
client/protocolgameparse.cpp
client/protocolgamesend.cpp
client/shadermanager.cpp
client/spriteappearances.cpp
client/spritemanager.cpp
client/statictext.cpp
client/thing.cpp
client/thingtype.cpp
client/thingtypemanager.cpp
client/tile.cpp
client/towns.cpp
client/uicreature.cpp
client/uiitem.cpp
client/uimap.cpp
client/uimapanchorlayout.cpp
client/uiminimap.cpp
client/uiprogressrect.cpp
client/uisprite.cpp
framework/core/adaptativeframecounter.cpp
framework/core/application.cpp
framework/core/asyncdispatcher.cpp
Expand Down Expand Up @@ -378,6 +332,54 @@ set(SOURCE_FILES
framework/util/color.cpp
framework/util/crypt.cpp

client/animatedtext.cpp
client/animator.cpp
client/attachableobject.cpp
client/attachedeffect.cpp
client/attachedeffectmanager.cpp
client/client.cpp
client/container.cpp
client/creature.cpp
client/creatures.cpp
client/effect.cpp
client/game.cpp
client/gameconfig.cpp
client/houses.cpp
client/item.cpp
client/itemtype.cpp
client/lightview.cpp
client/localplayer.cpp
client/luafunctions.cpp
client/luavaluecasts.cpp
client/map.cpp
client/mapio.cpp
client/mapview.cpp
client/minimap.cpp
client/missile.cpp
client/outfit.cpp
client/player.cpp
client/position.cpp
client/protocolcodes.cpp
client/protocolgame.cpp
client/protocolgameparse.cpp
client/protocolgamesend.cpp
client/shadermanager.cpp
client/spriteappearances.cpp
client/spritemanager.cpp
client/statictext.cpp
client/thing.cpp
client/thingtype.cpp
client/thingtypemanager.cpp
client/tile.cpp
client/towns.cpp
client/uicreature.cpp
client/uiitem.cpp
client/uimap.cpp
client/uimapanchorlayout.cpp
client/uiminimap.cpp
client/uiprogressrect.cpp
client/uisprite.cpp

protobuf/appearances.pb.cc
main.cpp
androidmain.cpp
Expand Down
97 changes: 97 additions & 0 deletions src/client/attachableobject.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
/*
* Copyright (c) 2010-2022 OTClient <https://github.com/edubart/otclient>
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/

#include "attachableobject.h"

#include <framework/core/eventdispatcher.h>

void AttachableObject::attachEffect(const AttachedEffectPtr& obj) {
if (!obj)
return;

onStartAttachEffect(obj);

if (obj->isHidedOwner())
++m_ownerHidden;

if (obj->getDuration() > 0) {
g_dispatcher.scheduleEvent([self = std::static_pointer_cast<AttachableObject>(shared_from_this()), effectId = obj->getId()]() {
self->detachEffectById(effectId);
}, obj->getDuration());
}

m_attachedEffects.emplace_back(obj);
g_dispatcher.addEvent([effect = obj, self = std::static_pointer_cast<AttachableObject>(shared_from_this())] {
self->onDispatcherAttachEffect(effect);
effect->callLuaField("onAttach", self->attachedObjectToLuaObject());
});
}

bool AttachableObject::detachEffectById(uint16_t id) {
const auto it = std::find_if(m_attachedEffects.begin(), m_attachedEffects.end(),
[id](const AttachedEffectPtr& obj) { return obj->getId() == id; });

if (it == m_attachedEffects.end())
return false;

onDetachEffect(*it);
m_attachedEffects.erase(it);

return true;
}

void AttachableObject::onDetachEffect(const AttachedEffectPtr& effect) {
if (effect->isHidedOwner())
--m_ownerHidden;

onStartDetachEffect(effect);

effect->callLuaField("onDetach", attachedObjectToLuaObject());
}

void AttachableObject::clearAttachedEffects() {
for (const auto& e : m_attachedEffects)
onDetachEffect(e);
m_attachedEffects.clear();
}

AttachedEffectPtr AttachableObject::getAttachedEffectById(uint16_t id) {
const auto it = std::find_if(m_attachedEffects.begin(), m_attachedEffects.end(),
[id](const AttachedEffectPtr& obj) { return obj->getId() == id; });

if (it == m_attachedEffects.end())
return nullptr;

return *it;
}

void AttachableObject::drawAttachedEffect(const Point& dest, LightView* lightView, bool isOnTop)
{
for (const auto& effect : m_attachedEffects) {
effect->draw(dest, isOnTop, lightView);
if (effect->getLoop() == 0) {
g_dispatcher.addEvent([self = std::static_pointer_cast<AttachableObject>(shared_from_this()), effectId = effect->getId()]() {
self->detachEffectById(effectId);
});
}
}
}
53 changes: 53 additions & 0 deletions src/client/attachableobject.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/*
* Copyright (c) 2010-2022 OTClient <https://github.com/edubart/otclient>
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/

#pragma once
#include "attachedeffect.h"
#include <framework/luaengine/luaobject.h>

class AttachableObject : public LuaObject
{
public:
AttachableObject() = default;
virtual ~AttachableObject() = default;

void attachEffect(const AttachedEffectPtr& obj);
void clearAttachedEffects();
bool detachEffectById(uint16_t id);
AttachedEffectPtr getAttachedEffectById(uint16_t id);

virtual LuaObjectPtr attachedObjectToLuaObject() = 0;
virtual void onStartAttachEffect(const AttachedEffectPtr& effect) { };
virtual void onDispatcherAttachEffect(const AttachedEffectPtr& effect) { };
virtual void onStartDetachEffect(const AttachedEffectPtr& effect) { };

bool isOwnerHidden() { return m_ownerHidden > 0; }

const std::vector<AttachedEffectPtr>& getAttachedEffects() { return m_attachedEffects; };

protected:
void drawAttachedEffect(const Point& dest, LightView* lightView, bool isOnTop);
void onDetachEffect(const AttachedEffectPtr& effect);

std::vector<AttachedEffectPtr> m_attachedEffects;
uint8_t m_ownerHidden{ 0 };
};
10 changes: 8 additions & 2 deletions src/client/attachedeffect.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@
#include "attachedeffect.h"
#include "shadermanager.h"
#include "gameconfig.h"
#include "lightview.h"

#include <framework/core/clock.h>
#include <framework/graphics/animatedtexture.h>

Expand Down Expand Up @@ -60,12 +62,16 @@ void AttachedEffect::draw(const Point& dest, bool isOnTop, LightView* lightView)
if (m_opacity < 100) g_drawPool.setOpacity(getOpacity(), true);

const auto& point = dest - (dirControl.offset * g_drawPool.getScaleFactor());
if (lightView && m_light.intensity > 0)
lightView->addLightSource(dest, m_light);

if (m_texture) {
const auto& size = (m_size.isUnset() ? m_texture->getSize() : m_size) * g_drawPool.getScaleFactor();
g_drawPool.addTexturedRect(Rect(point, size), m_texture->get(m_frame, m_animationTimer));
const auto& texture = m_texture->get(m_frame, m_animationTimer);
const auto& rect = Rect(Point(), texture->getSize());
g_drawPool.addTexturedRect(Rect(point, size), texture, rect, Color::white, { .order = getDrawOrder() });
} else {
m_thingType->draw(point, 0, m_direction, 0, 0, animation, Color::white, true, lightView);
m_thingType->draw(point, 0, m_direction, 0, 0, animation, Color::white, true, lightView, {.order = getDrawOrder()});
}
}

Expand Down
Loading
Loading