From febf3182ddadbe1d9373117f30d5df0a676de9ca Mon Sep 17 00:00:00 2001 From: Danilo Date: Fri, 31 May 2024 12:01:46 -0300 Subject: [PATCH] =?UTF-8?q?Corre=C3=A7=C3=B5es=20em=20Platform?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- framework/platform-dx/soundeffect.cpp | 4 +++ inc/audio/soundeffect.hpp | 1 + inc/platform-dx/implementations.hpp | 3 +++ .../02_PlatfformerStarterKit/animation.cpp | 4 +-- samples/02_PlatfformerStarterKit/gem.cpp | 7 +++--- samples/02_PlatfformerStarterKit/gem.hpp | 5 ++++ samples/02_PlatfformerStarterKit/level.cpp | 21 ++++++++++++---- samples/02_PlatfformerStarterKit/player.cpp | 25 ++++++++++--------- samples/02_PlatfformerStarterKit/player.hpp | 18 +++++++------ 9 files changed, 58 insertions(+), 30 deletions(-) diff --git a/framework/platform-dx/soundeffect.cpp b/framework/platform-dx/soundeffect.cpp index 261aa00..a0331ac 100644 --- a/framework/platform-dx/soundeffect.cpp +++ b/framework/platform-dx/soundeffect.cpp @@ -24,6 +24,10 @@ namespace xna { impl->_dxSoundEffect = unew(AudioEngine::impl->_dxAudioEngine.get(), file.c_str()); } + SoundEffect::~SoundEffect() { + impl = nullptr; + } + SoundEffect::SoundEffect( std::vector const& format, std::vector const& data, diff --git a/inc/audio/soundeffect.hpp b/inc/audio/soundeffect.hpp index 8ccc046..b68d7b4 100644 --- a/inc/audio/soundeffect.hpp +++ b/inc/audio/soundeffect.hpp @@ -34,6 +34,7 @@ namespace xna { Int loopStart, Int loopLength, TimeSpan const& duration); + ~SoundEffect(); void Play(); void Play(float volume, float pitch, float pan); uptr CreateInstance(); diff --git a/inc/platform-dx/implementations.hpp b/inc/platform-dx/implementations.hpp index 8b692a3..65b395c 100644 --- a/inc/platform-dx/implementations.hpp +++ b/inc/platform-dx/implementations.hpp @@ -476,6 +476,9 @@ namespace xna { }; struct SoundEffect::PlatformImplementation { + ~PlatformImplementation() { + } + uptr _dxSoundEffect = nullptr; }; diff --git a/samples/02_PlatfformerStarterKit/animation.cpp b/samples/02_PlatfformerStarterKit/animation.cpp index 770663e..350e1dc 100644 --- a/samples/02_PlatfformerStarterKit/animation.cpp +++ b/samples/02_PlatfformerStarterKit/animation.cpp @@ -13,10 +13,10 @@ namespace PlatformerStarterKit { void AnimationPlayer::Draw(xna::GameTime const& gameTime, xna::SpriteBatch& spriteBatch, xna::Vector2& position, xna::SpriteEffects spriteEffects) { if (animation == nullptr) - std::exception(); + std::exception("No animation is currently playing."); // Process passing time. - time += (float)gameTime.ElapsedGameTime.TotalSeconds(); + time += static_cast(gameTime.ElapsedGameTime.TotalSeconds()); while (time > animation->FrameTime()) { time -= animation->FrameTime(); diff --git a/samples/02_PlatfformerStarterKit/gem.cpp b/samples/02_PlatfformerStarterKit/gem.cpp index 49ff501..8c78095 100644 --- a/samples/02_PlatfformerStarterKit/gem.cpp +++ b/samples/02_PlatfformerStarterKit/gem.cpp @@ -15,7 +15,7 @@ namespace PlatformerStarterKit { } void Gem::Update(xna::GameTime const& gameTime) - { + { constexpr float BounceHeight = 0.18f; constexpr float BounceRate = 3.0f; constexpr float BounceSync = -0.75f; @@ -28,11 +28,12 @@ namespace PlatformerStarterKit { void Gem::OnCollected(xna::sptr& collectedBy) { - collectedSound->Play(); + collectedSound->Play(); + isCollected = true; } void Gem::Draw(xna::GameTime const& gameTime, xna::SpriteBatch& spriteBatch) - { + { spriteBatch.Draw(texture, Position(), nullptr, Color, 0.0f, origin, 1.0f, xna::SpriteEffects::None, 0.0f); } } \ No newline at end of file diff --git a/samples/02_PlatfformerStarterKit/gem.hpp b/samples/02_PlatfformerStarterKit/gem.hpp index 15caa9c..23ee1e0 100644 --- a/samples/02_PlatfformerStarterKit/gem.hpp +++ b/samples/02_PlatfformerStarterKit/gem.hpp @@ -31,6 +31,10 @@ namespace PlatformerStarterKit { void Update(xna::GameTime const& gameTime); void OnCollected(xna::sptr& collectedBy); void Draw(xna::GameTime const& gameTime, xna::SpriteBatch& spriteBatch); + + constexpr bool IsCollected() const { + return isCollected; + } private: xna::PTexture2D texture = nullptr; @@ -39,6 +43,7 @@ namespace PlatformerStarterKit { xna::Vector2 basePosition{}; float bounce{ 0.0F }; xna::sptr level = nullptr; + bool isCollected{ false }; }; } diff --git a/samples/02_PlatfformerStarterKit/level.cpp b/samples/02_PlatfformerStarterKit/level.cpp index 384232e..2a02a15 100644 --- a/samples/02_PlatfformerStarterKit/level.cpp +++ b/samples/02_PlatfformerStarterKit/level.cpp @@ -10,7 +10,7 @@ namespace PlatformerStarterKit { Level::Level(xna::sptr const& serviceProvider, xna::String const& path) : path(path) { - srand(354668); + //srand(354668); content = xna::snew("Content", serviceProvider); timeRemaining = xna::TimeSpan::FromMinutes(2.0); @@ -112,7 +112,7 @@ namespace PlatformerStarterKit { if (player != nullptr) std::exception("A level may only have one starting point."); - start = RectangleExtensions::GetBottomCenter(GetBounds(x, y)); + start = RectangleExtensions::GetBottomCenter(GetBounds(x, --y)); const auto _this = shared_from_this(); player = xna::snew(_this, start); @@ -149,10 +149,12 @@ namespace PlatformerStarterKit { for (size_t i = 0; i < gems.size(); ++i) { auto& gem = gems[i]; - gem->Update(gameTime); + if (gem->IsCollected()) + continue; + + gem->Update(gameTime); if (gem->BoundingCircle().Intersects(player->BoundingRectangle())) { - gems.erase(gems.begin() + i--); OnGemCollected(gem, player); } } @@ -168,6 +170,8 @@ namespace PlatformerStarterKit { for (size_t i = 0; i < enemies.size(); ++i) { auto& enemy = enemies[i]; + enemy->Update(gameTime); + if (enemy->BoundingRectangle().Intersects(player->BoundingRectangle())) { OnPlayerKilled(enemy); } @@ -223,7 +227,10 @@ namespace PlatformerStarterKit { UpdateGems(gameTime); - if (player->BoundingRectangle().Top() >= Height() * Tile::Height) + auto playerBounds = player->BoundingRectangle(); + auto heigth = Height(); + + if (playerBounds.Top() >= heigth * Tile::Height) OnPlayerKilled(nullptr); UpdateEnemies(gameTime); @@ -250,6 +257,10 @@ namespace PlatformerStarterKit { for (size_t i = 0; i < gems.size(); ++i) { auto& gem = gems[i]; + + if (gem->IsCollected()) + continue; + gem->Draw(gameTime, spriteBatch); } diff --git a/samples/02_PlatfformerStarterKit/player.cpp b/samples/02_PlatfformerStarterKit/player.cpp index 9d9f7e6..9d98c5b 100644 --- a/samples/02_PlatfformerStarterKit/player.cpp +++ b/samples/02_PlatfformerStarterKit/player.cpp @@ -112,7 +112,7 @@ namespace PlatformerStarterKit { gamePadState.IsButtonDown(JumpButton) || keyboardState.IsKeyDown(xna::Keys::Space) || keyboardState.IsKeyDown(xna::Keys::Up) || - keyboardState.IsKeyDown(xna::Keys::W); + keyboardState.IsKeyDown(xna::Keys::W); } void Player::OnKilled(xna::sptr& killedBy) @@ -171,15 +171,15 @@ namespace PlatformerStarterKit { void Player::HandleCollisions() { auto bounds = BoundingRectangle(); - auto leftTile = std::floor(static_cast(bounds.Left()) / Tile::Width); - auto rightTile = std::ceil((static_cast(bounds.Right()) / Tile::Width)) - 1; - auto topTile = std::floor(static_cast(bounds.Top()) / Tile::Height); - auto bottomTile = std::ceil((static_cast(bounds.Bottom()) / Tile::Height)) - 1; + auto leftTile = static_cast(std::floor(static_cast(bounds.Left()) / Tile::Width)); + auto rightTile = static_cast(std::ceil((static_cast(bounds.Right()) / Tile::Width))) - 1; + auto topTile = static_cast(std::floor(static_cast(bounds.Top()) / Tile::Height)); + auto bottomTile = static_cast(std::ceil((static_cast(bounds.Bottom()) / Tile::Height))) - 1; - isOnGround = false; + isOnGround = false; - for (size_t y = topTile; y <= bottomTile; ++y) { - for (size_t x = leftTile; x <= rightTile; ++x) { + for (int y = topTile; y <= bottomTile; ++y) { + for (int x = leftTile; x <= rightTile; ++x) { auto collision = level->GetCollision(x, y); if (collision != TileCollision::Passable) { auto tileBounds = level->GetBounds(x, y); @@ -189,11 +189,12 @@ namespace PlatformerStarterKit { auto absDepthY = std::abs(depth.Y); if (absDepthY < absDepthX || collision == TileCollision::Platform) { - if (previousBottom <= tileBounds.Top()) - isOnGround = true; + const auto tileBoundsTop = tileBounds.Top(); + if (previousBottom <= tileBoundsTop) + isOnGround = true; if (collision == TileCollision::Impassable || IsOnGround()) { - Position = xna::Vector2(Position.X, Position.Y + depth.Y); + Position = xna::Vector2(Position.X, Position.Y + depth.Y); bounds = BoundingRectangle(); } } @@ -206,7 +207,7 @@ namespace PlatformerStarterKit { } } } - + previousBottom = bounds.Bottom(); } diff --git a/samples/02_PlatfformerStarterKit/player.hpp b/samples/02_PlatfformerStarterKit/player.hpp index cb8a3da..b8db4df 100644 --- a/samples/02_PlatfformerStarterKit/player.hpp +++ b/samples/02_PlatfformerStarterKit/player.hpp @@ -60,15 +60,17 @@ namespace PlatformerStarterKit { float jumpTime = false; xna::Rectangle localBounds{}; - static constexpr float MoveAcceleration = 14000.0f; - static constexpr float MaxMoveSpeed = 2000.0f; - static constexpr float GroundDragFactor = 0.58f; - static constexpr float AirDragFactor = 0.65f; + static constexpr float MoveAcceleration = 13000.0f; + static constexpr float MaxMoveSpeed = 1750.0f; + static constexpr float GroundDragFactor = 0.48; + static constexpr float AirDragFactor = 0.58f; + static constexpr float MaxJumpTime = 0.35f; - static constexpr float JumpLaunchVelocity = -4000.0f; - static constexpr float GravityAcceleration = 3500.0f; - static constexpr float MaxFallSpeed = 600.0f; - static constexpr float JumpControlPower = 0.14f; + static constexpr float JumpLaunchVelocity = -3500.0f; + static constexpr float GravityAcceleration = 3400.0f; + static constexpr float MaxFallSpeed = 550.0f; + static constexpr float JumpControlPower = 0.14f; + static constexpr float MoveStickScale = 1.0f; static constexpr xna::Buttons JumpButton = xna::Buttons::A;