1
0
mirror of https://github.com/borgesdan/xn65 synced 2024-12-29 21:54:47 +01:00

Correções em Platform

This commit is contained in:
Danilo 2024-05-31 12:01:46 -03:00
parent 3e93d0ffbe
commit febf3182dd
9 changed files with 58 additions and 30 deletions

View File

@ -24,6 +24,10 @@ namespace xna {
impl->_dxSoundEffect = unew<DxSoundEffect>(AudioEngine::impl->_dxAudioEngine.get(), file.c_str()); impl->_dxSoundEffect = unew<DxSoundEffect>(AudioEngine::impl->_dxAudioEngine.get(), file.c_str());
} }
SoundEffect::~SoundEffect() {
impl = nullptr;
}
SoundEffect::SoundEffect( SoundEffect::SoundEffect(
std::vector<Byte> const& format, std::vector<Byte> const& format,
std::vector<Byte> const& data, std::vector<Byte> const& data,

View File

@ -34,6 +34,7 @@ namespace xna {
Int loopStart, Int loopStart,
Int loopLength, Int loopLength,
TimeSpan const& duration); TimeSpan const& duration);
~SoundEffect();
void Play(); void Play();
void Play(float volume, float pitch, float pan); void Play(float volume, float pitch, float pan);
uptr<SoundEffectInstance> CreateInstance(); uptr<SoundEffectInstance> CreateInstance();

View File

@ -476,6 +476,9 @@ namespace xna {
}; };
struct SoundEffect::PlatformImplementation { struct SoundEffect::PlatformImplementation {
~PlatformImplementation() {
}
uptr<DirectX::SoundEffect> _dxSoundEffect = nullptr; uptr<DirectX::SoundEffect> _dxSoundEffect = nullptr;
}; };

View File

@ -13,10 +13,10 @@ namespace PlatformerStarterKit {
void AnimationPlayer::Draw(xna::GameTime const& gameTime, xna::SpriteBatch& spriteBatch, xna::Vector2& position, xna::SpriteEffects spriteEffects) { void AnimationPlayer::Draw(xna::GameTime const& gameTime, xna::SpriteBatch& spriteBatch, xna::Vector2& position, xna::SpriteEffects spriteEffects) {
if (animation == nullptr) if (animation == nullptr)
std::exception(); std::exception("No animation is currently playing.");
// Process passing time. // Process passing time.
time += (float)gameTime.ElapsedGameTime.TotalSeconds(); time += static_cast<float>(gameTime.ElapsedGameTime.TotalSeconds());
while (time > animation->FrameTime()) { while (time > animation->FrameTime()) {
time -= animation->FrameTime(); time -= animation->FrameTime();

View File

@ -15,7 +15,7 @@ namespace PlatformerStarterKit {
} }
void Gem::Update(xna::GameTime const& gameTime) void Gem::Update(xna::GameTime const& gameTime)
{ {
constexpr float BounceHeight = 0.18f; constexpr float BounceHeight = 0.18f;
constexpr float BounceRate = 3.0f; constexpr float BounceRate = 3.0f;
constexpr float BounceSync = -0.75f; constexpr float BounceSync = -0.75f;
@ -28,11 +28,12 @@ namespace PlatformerStarterKit {
void Gem::OnCollected(xna::sptr<Player>& collectedBy) void Gem::OnCollected(xna::sptr<Player>& collectedBy)
{ {
collectedSound->Play(); collectedSound->Play();
isCollected = true;
} }
void Gem::Draw(xna::GameTime const& gameTime, xna::SpriteBatch& spriteBatch) 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); spriteBatch.Draw(texture, Position(), nullptr, Color, 0.0f, origin, 1.0f, xna::SpriteEffects::None, 0.0f);
} }
} }

View File

@ -31,6 +31,10 @@ namespace PlatformerStarterKit {
void Update(xna::GameTime const& gameTime); void Update(xna::GameTime const& gameTime);
void OnCollected(xna::sptr<Player>& collectedBy); void OnCollected(xna::sptr<Player>& collectedBy);
void Draw(xna::GameTime const& gameTime, xna::SpriteBatch& spriteBatch); void Draw(xna::GameTime const& gameTime, xna::SpriteBatch& spriteBatch);
constexpr bool IsCollected() const {
return isCollected;
}
private: private:
xna::PTexture2D texture = nullptr; xna::PTexture2D texture = nullptr;
@ -39,6 +43,7 @@ namespace PlatformerStarterKit {
xna::Vector2 basePosition{}; xna::Vector2 basePosition{};
float bounce{ 0.0F }; float bounce{ 0.0F };
xna::sptr<PlatformerStarterKit::Level> level = nullptr; xna::sptr<PlatformerStarterKit::Level> level = nullptr;
bool isCollected{ false };
}; };
} }

View File

@ -10,7 +10,7 @@
namespace PlatformerStarterKit { namespace PlatformerStarterKit {
Level::Level(xna::sptr<xna::IServiceProvider> const& serviceProvider, xna::String const& path) : path(path) Level::Level(xna::sptr<xna::IServiceProvider> const& serviceProvider, xna::String const& path) : path(path)
{ {
srand(354668); //srand(354668);
content = xna::snew<xna::ContentManager>("Content", serviceProvider); content = xna::snew<xna::ContentManager>("Content", serviceProvider);
timeRemaining = xna::TimeSpan::FromMinutes(2.0); timeRemaining = xna::TimeSpan::FromMinutes(2.0);
@ -112,7 +112,7 @@ namespace PlatformerStarterKit {
if (player != nullptr) if (player != nullptr)
std::exception("A level may only have one starting point."); 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(); const auto _this = shared_from_this();
player = xna::snew<PlatformerStarterKit::Player>(_this, start); player = xna::snew<PlatformerStarterKit::Player>(_this, start);
@ -149,10 +149,12 @@ namespace PlatformerStarterKit {
for (size_t i = 0; i < gems.size(); ++i) { for (size_t i = 0; i < gems.size(); ++i) {
auto& gem = gems[i]; auto& gem = gems[i];
gem->Update(gameTime); if (gem->IsCollected())
continue;
gem->Update(gameTime);
if (gem->BoundingCircle().Intersects(player->BoundingRectangle())) { if (gem->BoundingCircle().Intersects(player->BoundingRectangle())) {
gems.erase(gems.begin() + i--);
OnGemCollected(gem, player); OnGemCollected(gem, player);
} }
} }
@ -168,6 +170,8 @@ namespace PlatformerStarterKit {
for (size_t i = 0; i < enemies.size(); ++i) { for (size_t i = 0; i < enemies.size(); ++i) {
auto& enemy = enemies[i]; auto& enemy = enemies[i];
enemy->Update(gameTime);
if (enemy->BoundingRectangle().Intersects(player->BoundingRectangle())) { if (enemy->BoundingRectangle().Intersects(player->BoundingRectangle())) {
OnPlayerKilled(enemy); OnPlayerKilled(enemy);
} }
@ -223,7 +227,10 @@ namespace PlatformerStarterKit {
UpdateGems(gameTime); UpdateGems(gameTime);
if (player->BoundingRectangle().Top() >= Height() * Tile::Height) auto playerBounds = player->BoundingRectangle();
auto heigth = Height();
if (playerBounds.Top() >= heigth * Tile::Height)
OnPlayerKilled(nullptr); OnPlayerKilled(nullptr);
UpdateEnemies(gameTime); UpdateEnemies(gameTime);
@ -250,6 +257,10 @@ namespace PlatformerStarterKit {
for (size_t i = 0; i < gems.size(); ++i) { for (size_t i = 0; i < gems.size(); ++i) {
auto& gem = gems[i]; auto& gem = gems[i];
if (gem->IsCollected())
continue;
gem->Draw(gameTime, spriteBatch); gem->Draw(gameTime, spriteBatch);
} }

View File

@ -112,7 +112,7 @@ namespace PlatformerStarterKit {
gamePadState.IsButtonDown(JumpButton) || gamePadState.IsButtonDown(JumpButton) ||
keyboardState.IsKeyDown(xna::Keys::Space) || keyboardState.IsKeyDown(xna::Keys::Space) ||
keyboardState.IsKeyDown(xna::Keys::Up) || keyboardState.IsKeyDown(xna::Keys::Up) ||
keyboardState.IsKeyDown(xna::Keys::W); keyboardState.IsKeyDown(xna::Keys::W);
} }
void Player::OnKilled(xna::sptr<Enemy>& killedBy) void Player::OnKilled(xna::sptr<Enemy>& killedBy)
@ -171,15 +171,15 @@ namespace PlatformerStarterKit {
void Player::HandleCollisions() void Player::HandleCollisions()
{ {
auto bounds = BoundingRectangle(); auto bounds = BoundingRectangle();
auto leftTile = std::floor(static_cast<float>(bounds.Left()) / Tile::Width); auto leftTile = static_cast<int>(std::floor(static_cast<float>(bounds.Left()) / Tile::Width));
auto rightTile = std::ceil((static_cast<float>(bounds.Right()) / Tile::Width)) - 1; auto rightTile = static_cast<int>(std::ceil((static_cast<float>(bounds.Right()) / Tile::Width))) - 1;
auto topTile = std::floor(static_cast<float>(bounds.Top()) / Tile::Height); auto topTile = static_cast<int>(std::floor(static_cast<float>(bounds.Top()) / Tile::Height));
auto bottomTile = std::ceil((static_cast<float>(bounds.Bottom()) / Tile::Height)) - 1; auto bottomTile = static_cast<int>(std::ceil((static_cast<float>(bounds.Bottom()) / Tile::Height))) - 1;
isOnGround = false; isOnGround = false;
for (size_t y = topTile; y <= bottomTile; ++y) { for (int y = topTile; y <= bottomTile; ++y) {
for (size_t x = leftTile; x <= rightTile; ++x) { for (int x = leftTile; x <= rightTile; ++x) {
auto collision = level->GetCollision(x, y); auto collision = level->GetCollision(x, y);
if (collision != TileCollision::Passable) { if (collision != TileCollision::Passable) {
auto tileBounds = level->GetBounds(x, y); auto tileBounds = level->GetBounds(x, y);
@ -189,11 +189,12 @@ namespace PlatformerStarterKit {
auto absDepthY = std::abs(depth.Y); auto absDepthY = std::abs(depth.Y);
if (absDepthY < absDepthX || collision == TileCollision::Platform) { if (absDepthY < absDepthX || collision == TileCollision::Platform) {
if (previousBottom <= tileBounds.Top()) const auto tileBoundsTop = tileBounds.Top();
isOnGround = true; if (previousBottom <= tileBoundsTop)
isOnGround = true;
if (collision == TileCollision::Impassable || IsOnGround()) { 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(); bounds = BoundingRectangle();
} }
} }
@ -206,7 +207,7 @@ namespace PlatformerStarterKit {
} }
} }
} }
previousBottom = bounds.Bottom(); previousBottom = bounds.Bottom();
} }

View File

@ -60,15 +60,17 @@ namespace PlatformerStarterKit {
float jumpTime = false; float jumpTime = false;
xna::Rectangle localBounds{}; xna::Rectangle localBounds{};
static constexpr float MoveAcceleration = 14000.0f; static constexpr float MoveAcceleration = 13000.0f;
static constexpr float MaxMoveSpeed = 2000.0f; static constexpr float MaxMoveSpeed = 1750.0f;
static constexpr float GroundDragFactor = 0.58f; static constexpr float GroundDragFactor = 0.48;
static constexpr float AirDragFactor = 0.65f; static constexpr float AirDragFactor = 0.58f;
static constexpr float MaxJumpTime = 0.35f; static constexpr float MaxJumpTime = 0.35f;
static constexpr float JumpLaunchVelocity = -4000.0f; static constexpr float JumpLaunchVelocity = -3500.0f;
static constexpr float GravityAcceleration = 3500.0f; static constexpr float GravityAcceleration = 3400.0f;
static constexpr float MaxFallSpeed = 600.0f; static constexpr float MaxFallSpeed = 550.0f;
static constexpr float JumpControlPower = 0.14f; static constexpr float JumpControlPower = 0.14f;
static constexpr float MoveStickScale = 1.0f; static constexpr float MoveStickScale = 1.0f;
static constexpr xna::Buttons JumpButton = xna::Buttons::A; static constexpr xna::Buttons JumpButton = xna::Buttons::A;