From 7aa315550e9cbe08a602117f8f5e72a321e4d8db Mon Sep 17 00:00:00 2001 From: Legida Date: Thu, 16 Dec 2021 23:58:31 +0300 Subject: [PATCH] first iteration added trigger for change bullet, fixed fireBullet, fixed hook, added bullet switch to GUI --- Classes/MainScene.cpp | 15 ++++++++++ Classes/Trigger.cpp | 3 ++ Classes/Trigger.h | 3 +- Classes/bullets/FireBullet.cpp | 37 ++++++++++++++++++++---- Classes/bullets/FireBullet.h | 4 +++ Classes/bullets/IShootingPattern.cpp | 2 +- Classes/bullets/PlayerHookBullet.cpp | 3 ++ Classes/characters/ShootingCharacter.cpp | 2 +- Classes/characters/ShootingCharacter.h | 11 +++---- Classes/player/Player.cpp | 23 +++++++++++---- Classes/player/Player.h | 8 +++++ Resources/maximum.tmx | 7 ++++- Resources/nodeProperties/bullets.json | 2 +- proj.win32/imgui.ini | 4 +-- 14 files changed, 100 insertions(+), 24 deletions(-) diff --git a/Classes/MainScene.cpp b/Classes/MainScene.cpp index 21dd4da..60351d7 100644 --- a/Classes/MainScene.cpp +++ b/Classes/MainScene.cpp @@ -215,6 +215,21 @@ void MainScene::showImGui() { _player->changeHp(-1); } } + if (ImGui::Button("FireBullet")) { + if (_player) { + _player->changeBulletCreator(new FireBulletCreator(ShootingCharacter::playerPhysMask())); + } + } + if (ImGui::Button("IdleBullet")) { + if (_player) { + _player->changeBulletCreator(new IdleBulletCreator(ShootingCharacter::playerPhysMask())); + } + } + if (ImGui::Button("IceBullet")) { + if (_player) { + _player->changeBulletCreator(new IceBulletCreator(ShootingCharacter::playerPhysMask())); + } + } //Position ImGui::Text("Position X: %f Y: %f", _player->getPosition().x, _player->getPosition().y); //HP diff --git a/Classes/Trigger.cpp b/Classes/Trigger.cpp index 663fe57..aff5891 100644 --- a/Classes/Trigger.cpp +++ b/Classes/Trigger.cpp @@ -33,6 +33,9 @@ void Trigger::onCollision() { isActivated = true; _TMM->getTiledMap()->getLayer(FG + triggerFunc.at(2))->setVisible(false); break; + case 5: + isActivated = true; + _player->changeBulletCreator(new FireBulletCreator(ShootingCharacter::playerPhysMask())); } } diff --git a/Classes/Trigger.h b/Classes/Trigger.h index fe1908c..cd4686c 100644 --- a/Classes/Trigger.h +++ b/Classes/Trigger.h @@ -8,7 +8,8 @@ class Trigger : public b2Sprite {"SA", 0}, {"SM", 1}, {"LL", 2}, - {"LD", 3} + {"LD", 3}, + {"BF", 5} }; bool isActivated = false; std::string triggerFunc; diff --git a/Classes/bullets/FireBullet.cpp b/Classes/bullets/FireBullet.cpp index e2d3ecd..b7be62a 100644 --- a/Classes/bullets/FireBullet.cpp +++ b/Classes/bullets/FireBullet.cpp @@ -37,25 +37,34 @@ FireBullet* FireBullet::create(cocos2d::Node* world, Vec2 pos, Vec2 dest, b2Filt bullet->setCoords(pos, dest); bullet->getFixtureDef()->filter = filter; bullet->ordinaryOptions(world, pos); + bullet->setCooldown(); return bullet; } CC_SAFE_DELETE(bullet); return nullptr; } +void FireBullet::setCooldown() { + Vec2 nDest = getDest(); + nDest.normalize(); + nDest *= 10; + double k = getDest().x / nDest.x; + _attackConstCooldown = 0.25*(1/k);//k; +} + void FireBullet::shoot(Vec2 targetPos, IBulletTypeCreator* bulletCreator) { if (_attackCooldown <= 0) { - _attackCooldown = 0.5; + _attackCooldown = _attackConstCooldown; Vec2 pos = getPosition(); Vec2 dest = targetPos; //+ pos; Vec2 newDest = dest; - newDest.rotate(Vec2(), 0.9f); + //newDest.rotate(Vec2(), 0.1f); newDest.normalize(); newDest *= getContentSize().height; Vec2 pos1 = pos + newDest; //dest.y *= -1; - dest.normalize(); - dest *= PLAYER_BULLET_SPEED; + //dest.normalize(); + //dest *= PLAYER_BULLET_SPEED; _shootingPattern->shoot(pos1, dest, bulletCreator); _fireCount++; } @@ -89,6 +98,8 @@ FireBlast* FireBlast::create(cocos2d::Node* world, Vec2 pos, Vec2 dest, b2Filter //bullet->init(); bullet->setCoords(pos, dest); bullet->_moveDest = bullet->getDest(); + bullet->_moveDest.rotate(Vec2(), M_PI/2); + bullet->setAngleMoveParam(); bullet->getFixtureDef()->filter = filter; bullet->ordinaryOptions(world, pos); return bullet; @@ -97,12 +108,26 @@ FireBlast* FireBlast::create(cocos2d::Node* world, Vec2 pos, Vec2 dest, b2Filter return nullptr; } +void FireBlast::setAngleMoveParam() { + Vec2 nDest = getDest(); + nDest.normalize(); + nDest *= 10; + double k = getDest().x / nDest.x; + _moveDest *= (0.66f); + _moveAngle = M_PI * 2 / (30*(1/k)); +} + int FireBlast::getDamage() { return 0; } void FireBlast::move(float dt) { - _moveDest.rotate(Vec2(), 360/60 ); - getBody()->SetLinearVelocity(b2Vec2(getDest().x + _moveDest.x, getDest().y + _moveDest.y)); + _moveDest.rotate(Vec2(), _moveAngle); + setPosition(getPosition() + _moveDest); + //Vec2 sdest = getDest() + _moveDest; + //dest *= dt / 0.016; + //getBody()->SetLinearVelocity(b2Vec2(_moveDest.x, _moveDest.y)); + getBody()->SetLinearVelocity(b2Vec2(getDest().x, getDest().y)); + //getBody()->SetLinearVelocity(b2Vec2(getDest().x + _moveDest.x, getDest().y + _moveDest.y)); } \ No newline at end of file diff --git a/Classes/bullets/FireBullet.h b/Classes/bullets/FireBullet.h index 65ba12b..f053461 100644 --- a/Classes/bullets/FireBullet.h +++ b/Classes/bullets/FireBullet.h @@ -13,7 +13,9 @@ class FireBullet : public Bullet, ShootingCharacter { void shoot(cocos2d::Vec2 targetPos, IBulletTypeCreator* bulletCreator) override; private: + void setCooldown(); int _fireCount; //created fires + float _attackConstCooldown; }; class FireBlast : public Bullet { @@ -27,5 +29,7 @@ class FireBlast : public Bullet { int getDamage() override; private: + void setAngleMoveParam(); Vec2 _moveDest; + double _moveAngle; }; \ No newline at end of file diff --git a/Classes/bullets/IShootingPattern.cpp b/Classes/bullets/IShootingPattern.cpp index f967edc..b681720 100644 --- a/Classes/bullets/IShootingPattern.cpp +++ b/Classes/bullets/IShootingPattern.cpp @@ -14,7 +14,7 @@ void ShotGunShootingPattern::shoot(Vec2 pos, Vec2 dest, IBulletTypeCreator* bull void CircleShootingPattern::shoot(Vec2 pos, Vec2 dest, IBulletTypeCreator* bulletCreator) { float a = -0.13; - for (int i = 0; i < 12; i++) { + for (int i = -6; i < 6; i++) { Vec2 newDest = dest; newDest.rotate(Vec2(), a * i); _parent->createBulletOnParent(bulletCreator, pos, newDest); diff --git a/Classes/bullets/PlayerHookBullet.cpp b/Classes/bullets/PlayerHookBullet.cpp index d75c9b1..f5851a6 100644 --- a/Classes/bullets/PlayerHookBullet.cpp +++ b/Classes/bullets/PlayerHookBullet.cpp @@ -17,6 +17,9 @@ void PlayerHookBullet::update(float dt) { if (_hooked) { getBody()->SetLinearVelocity(b2Vec2(0, 0)); } + if (!_hooked && _moveTime <= 0) { + setOnRemove(); + } Bullet::update(dt); } diff --git a/Classes/characters/ShootingCharacter.cpp b/Classes/characters/ShootingCharacter.cpp index ae6fd7a..1918af0 100644 --- a/Classes/characters/ShootingCharacter.cpp +++ b/Classes/characters/ShootingCharacter.cpp @@ -5,7 +5,7 @@ const float ShootingCharacter::PLAYER_ATTACK_COOLDOWN = 0.2f; const float ShootingCharacter::PLAYER_BIG_ATTACK_COOLDOWN = 1; const float ShootingCharacter::ENEMY_ATTACK_COOLDOWN = 0.5f; const int ShootingCharacter::PLAYER_BULLET_SPEED = 10; - +const int ShootingCharacter::PLAYER_HOOK_SPEED = 15; b2Filter ShootingCharacter::playerPhysMask() { b2Filter filter; diff --git a/Classes/characters/ShootingCharacter.h b/Classes/characters/ShootingCharacter.h index 621b678..35fdac3 100644 --- a/Classes/characters/ShootingCharacter.h +++ b/Classes/characters/ShootingCharacter.h @@ -18,13 +18,14 @@ class ShootingCharacter { static const float PLAYER_BIG_ATTACK_COOLDOWN; static const float ENEMY_ATTACK_COOLDOWN; static const int PLAYER_BULLET_SPEED; -protected: - float _attackCooldown; + static const int PLAYER_HOOK_SPEED; - b2Filter playerPhysMask(); - b2Filter enemyPhysMask(); - b2Filter hookPhysMask(); + static b2Filter playerPhysMask(); + static b2Filter enemyPhysMask(); + static b2Filter hookPhysMask(); +protected: + float _attackCooldown; IShootingPattern* _shootingPattern; }; diff --git a/Classes/player/Player.cpp b/Classes/player/Player.cpp index 1e50150..4128b85 100644 --- a/Classes/player/Player.cpp +++ b/Classes/player/Player.cpp @@ -1,6 +1,6 @@ #include "Player.h" #include "box2d/b2dRootWorldNode.h" -#include "IShootingPattern.h" +//#include "IShootingPattern.h" #include "external/json/document.h" USING_NS_CC; @@ -113,7 +113,10 @@ bool Player::init() { //Hook _hook = nullptr; _hookBody = DrawNode::create(); + _hookPattern = new IdleShootingPattern(this); addChild(_hookBody); + //shoot + _bulletCreator = new IdleBulletCreator(playerPhysMask()); //Animation //Idle animation @@ -303,7 +306,8 @@ void Player::mousePressed(cocos2d::Event* event) { EventMouse* mouse = dynamic_cast(event); if (mouse->getMouseButton() == EventMouse::MouseButton::BUTTON_LEFT) { - shoot(clickPosCalculate(mouse), new FireBulletCreator(playerPhysMask())); + //shoot(clickPosCalculate(mouse), _bulletCreator); + shoot(clickPosCalculate(mouse), _bulletCreator); //hit(); setAnimState(eAnimState::Attack); } @@ -313,6 +317,10 @@ void Player::mousePressed(cocos2d::Event* event) { } } +void Player::changeBulletCreator(IBulletTypeCreator* bulletCreator) { + _bulletCreator = bulletCreator; +} + void Player::move(float dt) { //UNDONE moving if (getBody()->GetLinearVelocity().x < _maxSpeed && _curSpeed > 0) { @@ -340,12 +348,15 @@ void Player::shoot(Vec2 targetPos, IBulletTypeCreator* bulletCreator) { Vec2 dest = targetPos - pos; dest.normalize(); dest.y *= -1; - dest *= PLAYER_BULLET_SPEED; - if (auto isHook = dynamic_cast(bulletCreator)) { + if (auto isHook = dynamic_cast(bulletCreator)) { + dest *= PLAYER_HOOK_SPEED; + //dest += {getBody()->GetLinearVelocity().x, getBody()->GetLinearVelocity().y}; + _hookPattern->shoot(pos, dest, bulletCreator); + } + else { dest *= PLAYER_BULLET_SPEED; + _shootingPattern->shoot(pos, dest, bulletCreator); } - - _shootingPattern->shoot(pos, dest, bulletCreator); } } diff --git a/Classes/player/Player.h b/Classes/player/Player.h index 2965a29..11c3158 100644 --- a/Classes/player/Player.h +++ b/Classes/player/Player.h @@ -2,6 +2,7 @@ #include "ShootingCharacter.h" #include "PlayerHookBullet.h" #include "MeleeCharacter.h" +#include "IShootingPattern.h" enum class eJumpState { None, @@ -56,7 +57,14 @@ class Player : public ShootingCharacter, public MeleeCharacter, public b2Sprite //functions for testing int getJumpCount() const; + + //for change bullets + void changeBulletCreator(IBulletTypeCreator* bulletCreator); private: + //shoot + IBulletTypeCreator* _bulletCreator; + IdleShootingPattern* _hookPattern; + //functions void move(float dt); void jump(float dt); diff --git a/Resources/maximum.tmx b/Resources/maximum.tmx index 73e0773..b7e5e2f 100644 --- a/Resources/maximum.tmx +++ b/Resources/maximum.tmx @@ -1,5 +1,5 @@ - + @@ -13,6 +13,11 @@ + + + + + diff --git a/Resources/nodeProperties/bullets.json b/Resources/nodeProperties/bullets.json index 0b6a7a4..28fe955 100644 --- a/Resources/nodeProperties/bullets.json +++ b/Resources/nodeProperties/bullets.json @@ -25,7 +25,7 @@ "name": "PlayerHookBullet", "specifications": { "damage": 100, - "moveTime": 2.0, + "moveTime": 0.8, "lifeTime": 7.0 }, "components": { diff --git a/proj.win32/imgui.ini b/proj.win32/imgui.ini index bf0744b..c39721d 100644 --- a/proj.win32/imgui.ini +++ b/proj.win32/imgui.ini @@ -19,7 +19,7 @@ Size=400,213 Collapsed=1 [Window][Debug] -Pos=1080,566 -Size=357,214 +Pos=1046,589 +Size=357,185 Collapsed=0