能帮到你的话,就给个赞吧 😘
文章目录
- bullet.h
- bullet.cpp
bullet.h
#pragma once
#include "vector2.h"
#include <functional>
#include "playerID.h"
#include "camera.h"
#include "graphics.h"class Bullet {public:
//伤害void setDamage(int val);int getDamage() const;
//位置void setPosition(float x, float y);const Vector2& getPosition() const;const Vector2& getSize() const;void setVelocity(float x, float y);//碰撞void setCollideTarget(const PlayerID& id);const PlayerID& getCollideTarget() const;void setValid(bool flag);bool getValid() const;bool checkCanRemove() const;virtual bool checkIsCollision(const Vector2& position, const Vector2& size); //检测碰撞virtual void onCollide(); //处理碰撞//回调void setCallback(const std::function<void()>& cb);protected:
//屏幕bool checkIsExceedScreen();public:virtual void update(int time);virtual void render();protected://子弹移动Vector2 position; //位置Vector2 size; //尺寸Vector2 velocity; //速度//伤害int damage = 10;//子弹状态bool isValid = true;bool isCanRemove = false;//回调——增加玩家能量std::function<void()> cb;//目标玩家PlayerID targetID = P1;};
bullet.cpp
#include "bullet.h"void Bullet::setDamage(int val){damage = val;
}int Bullet::getDamage() const{return damage;
}void Bullet::setPosition(float x, float y){position.x = x, position.y = y;
}const Vector2& Bullet::getPosition() const{return position;
}const Vector2& Bullet::getSize() const{return size;
}void Bullet::setVelocity(float x, float y){velocity.x = x, velocity.y = y;
}void Bullet::setCollideTarget(const PlayerID& id){targetID = id;
}const PlayerID& Bullet::getCollideTarget() const{return targetID;
}void Bullet::setValid(bool flag){isValid = flag;
}bool Bullet::getValid() const{return isValid;
}bool Bullet::checkCanRemove() const{return isCanRemove;
}bool Bullet::checkIsCollision(const Vector2& position, const Vector2& size){auto x = this->position.x + size.x / 2;auto y = this->position.y + size.y / 2;//判断子弹的中心位置坐标return x >= position.x && x <= position.x + size.x && y >= position.y && y <= position.y + size.y;}void Bullet::onCollide(){if (cb)cb();
}void Bullet::setCallback(const std::function<void()>& cb){this->cb = cb;
}bool Bullet::checkIsExceedScreen(){int w = getwidth(), h = getheight();auto x = this->position.x + size.x;auto y = this->position.y + size.y;return x <= 0 || x >= w || y <= 0 || y >= h;}void Bullet::update(int time){
}void Bullet::render()
{
}