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

Add functionality to place monsters in the Editor #7852

Merged
merged 7 commits into from
Oct 5, 2023
Merged
Show file tree
Hide file tree
Changes from 5 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
24 changes: 22 additions & 2 deletions src/fheroes2/editor/editor_interface.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
#include "maps_tiles.h"
#include "maps_tiles_helper.h"
#include "math_base.h"
#include "monster.h"
#include "mp2.h"
#include "screen.h"
#include "settings.h"
Expand Down Expand Up @@ -586,6 +587,20 @@ namespace Interface
}
}
}
else if ( _editorPanel.isMonsterSettingMode() ) {
if ( tile.isWater() ) {
fheroes2::showStandardTextMessage( _( "Monster" ), _( "Monsters cannot be placed on water." ), Dialog::OK );
}
else if ( !Maps::isClearGround( tile ) ) {
fheroes2::showStandardTextMessage( _( "Monster" ), _( "Choose a tile which does not contain any objects." ), Dialog::OK );
}
else if ( Monster{ _editorPanel.getMonsterId() }.isValid() ) {
const fheroes2::ActionCreator action( _historyManager );

Maps::setMonsterOnTile( tile, _editorPanel.getMonsterId(), 0 );
_redraw |= mapUpdateFlags;
}
}
}

void EditorInterface::mouseCursorAreaPressRight( const int32_t tileIndex ) const
Expand Down Expand Up @@ -623,8 +638,13 @@ namespace Interface
}
}

void EditorInterface::updateCursor( const int32_t /*tileIndex*/ )
void EditorInterface::updateCursor( const int32_t tileIndex )
{
Cursor::Get().SetThemes( Cursor::POINTER );
if ( _cursorUpdater ) {
_cursorUpdater( tileIndex );
}
else {
Cursor::Get().SetThemes( Cursor::POINTER );
}
}
}
8 changes: 8 additions & 0 deletions src/fheroes2/editor/editor_interface.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
#pragma once

#include <cstdint>
#include <functional>

#include "editor_interface_panel.h"
#include "game_mode.h"
Expand Down Expand Up @@ -71,6 +72,11 @@ namespace Interface

void updateCursor( const int32_t tileIndex ) override;

void setCursorUpdater( const std::function<void( const int32_t )> & cursorUpdater )
{
_cursorUpdater = cursorUpdater;
}

private:
EditorInterface()
: _editorPanel( *this )
Expand All @@ -83,6 +89,8 @@ namespace Interface
int32_t _selectedTile{ -1 };
int32_t _tileUnderCursor{ -1 };

std::function<void( const int32_t )> _cursorUpdater;

fheroes2::HistoryManager _historyManager;
};
}
29 changes: 28 additions & 1 deletion src/fheroes2/editor/editor_interface_panel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,9 @@
#include <string>

#include "agg_image.h"
#include "cursor.h"
#include "dialog.h"
#include "dialog_selectitems.h"
#include "dialog_system_options.h"
#include "editor_interface.h"
#include "ground.h"
Expand Down Expand Up @@ -74,7 +76,7 @@ namespace Interface
int32_t EditorPanel::getBrushSize() const
{
// Roads and streams are placed using only 1x1 brush.
if ( _selectedInstrument == Instrument::STREAM || _selectedInstrument == Instrument::ROAD ) {
if ( _selectedInstrument == Instrument::STREAM || _selectedInstrument == Instrument::ROAD || isMonsterSettingMode() ) {
return 1;
}

Expand Down Expand Up @@ -301,6 +303,10 @@ namespace Interface
if ( le.MousePressLeft( _instrumentButtonsRect[i] ) ) {
if ( _instrumentButtons[i].drawOnPress() ) {
_selectedInstrument = static_cast<uint8_t>( i );

// Reset cursor updater since this UI element was clicked.
_interface.setCursorUpdater( {} );

setRedraw();
}
}
Expand Down Expand Up @@ -399,6 +405,10 @@ namespace Interface
for ( size_t i = 0; i < _objectButtonsRect.size(); ++i ) {
if ( ( _selectedObject != i ) && le.MousePressLeft( _objectButtonsRect[i] ) ) {
_selectedObject = static_cast<uint8_t>( i );

// Reset cursor updater since this UI element was clicked.
_interface.setCursorUpdater( {} );

setRedraw();

// There is no need to continue the loop as only one button can be pressed at one moment.
Expand Down Expand Up @@ -432,6 +442,23 @@ namespace Interface
else if ( le.MousePressRight( _objectButtonsRect[Brush::TREASURES] ) ) {
fheroes2::showStandardTextMessage( _getObjectTypeName( Brush::TREASURES ), _( "Used to place\na resource or treasure." ), Dialog::ZERO );
}
else if ( le.MouseClickLeft( _objectButtonsRect[Brush::MONSTERS] ) ) {
_monsterId = Monster::UNKNOWN;

const Monster monster = Dialog::selectMonster();
if ( monster.GetID() != Monster::UNKNOWN ) {
_monsterId = monster.GetID();

_interface.setCursorUpdater( [monster = monster]( const int32_t /*tileIndex*/ ) {
const fheroes2::Sprite & image = fheroes2::AGG::GetICN( ICN::MONS32, monster.GetSpriteIndex() );

Cursor::Get().setCustomImage( image, { -image.width() / 2, -image.height() / 2 } );
} );

_interface.updateCursor( 0 );
return res;
}
}
}

le.MousePressLeft( _rectMagnify ) ? _buttonMagnify.drawOnPress() : _buttonMagnify.drawOnRelease();
Expand Down
15 changes: 14 additions & 1 deletion src/fheroes2/editor/editor_interface_panel.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
#include "game_mode.h"
#include "ground.h"
#include "math_base.h"
#include "monster.h"
#include "ui_button.h"

namespace Interface
Expand Down Expand Up @@ -66,10 +67,20 @@ namespace Interface
return _selectedInstrument == Instrument::ERASE;
}

bool isMonsterSettingMode() const
{
return ( _selectedInstrument == OBJECT ) && ( _selectedObject == MONSTERS );
}

int32_t getMonsterId() const
{
return _monsterId;
}

bool showAreaSelectRect() const
{
return _selectedInstrument == Instrument::TERRAIN || _selectedInstrument == Instrument::STREAM || _selectedInstrument == Instrument::ROAD
|| _selectedInstrument == Instrument::ERASE;
|| _selectedInstrument == Instrument::ERASE || isMonsterSettingMode();
}

bool useMouseDragMovement() const
Expand Down Expand Up @@ -184,5 +195,7 @@ namespace Interface
uint8_t _selectedTerrain{ Brush::WATER };
uint8_t _selectedObject{ Brush::WATER };
uint8_t _selectedBrushSize{ BrushSize::MEDIUM };

int32_t _monsterId{ Monster::UNKNOWN };
};
}
11 changes: 7 additions & 4 deletions src/fheroes2/maps/maps_tiles_helper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2423,10 +2423,13 @@ namespace Maps
{
tile.SetObject( MP2::OBJ_MONSTER );

// If there was another object sprite here (shadow for example) push it down to Addons,
// except when there is already MONS32.ICN here.
if ( tile.getObjectIcnType() != MP2::OBJ_ICN_TYPE_UNKNOWN && tile.getObjectIcnType() != MP2::OBJ_ICN_TYPE_MONS32 && tile.GetObjectSpriteIndex() != 255 ) {
// Push object sprite to Level 1 Addons preserving the Layer Type.
if ( tile.getObjectIcnType() == MP2::OBJ_ICN_TYPE_UNKNOWN ) {
// No object exists on this tile. Add one.
tile.setObjectUID( getNewObjectUID() );
tile.setObjectIcnType( MP2::OBJ_ICN_TYPE_MONS32 );
}
else if ( tile.getObjectIcnType() != MP2::OBJ_ICN_TYPE_MONS32 ) {
// If there is another object sprite here (shadow for example) push it down to add-ons.
tile.pushBottomLayerAddon( TilesAddon( tile.getLayerType(), tile.GetObjectUID(), tile.getObjectIcnType(), tile.GetObjectSpriteIndex() ) );

// Set unique UID for placed monster.
Expand Down