PeaShooter

 avatar
unknown
plain_text
9 months ago
2.3 kB
3
Indexable
#include <allegro5/base.h>
#include <cmath>
#include <string>
#include <random>

#include "Engine/AudioHelper.hpp"
#include "Bullet/FireBullet.hpp"
#include "Engine/Group.hpp"
#include "Scene/PlayScene.hpp"
#include "Engine/Point.hpp"
#include "PeaShooter.hpp"
#include "Engine/Sprite.hpp"
#include "Engine/LOG.hpp"
#include "Engine/Allegro5Exception.hpp"
#include <allegro5/bitmap.h>
#include <allegro5/allegro5.h>
#include "Bullet/PeaBullet.hpp"

int num_peashooter=0;
const float PeaShooter::suntime = 10.0;
const float PeaShooter::cool_down_time = 7.5;
PeaShooter::PeaShooter(float x, float y) :
    Turret("play/plant-floor.png", "play/PeaShooter/Peashooter-0.png", x, y, 200, suntime, 0.5) {
    Anchor.y += 8.0f / GetBitmapHeight() - 0.125;
}

void PeaShooter::CreateBullet() {
    Engine::Point diff = Engine::Point(cos(Rotation - ALLEGRO_PI / 2), sin(Rotation - ALLEGRO_PI / 2));
    float rotation = atan2(diff.y, diff.x);
    Engine::Point normalized = diff.Normalize();
    // Change bullet position to the front of the gun barrel.
    getPlayScene()->BulletGroup->AddNewObject(new PeaBullet(Position + normalized * 36, diff, rotation, this));
    AudioHelper::PlayAudio("gun.wav");
}

void PeaShooter::Draw() const{
    std::string fname = "Resource/images/play/PeaShooter/Peashooter-" + std::to_string(off) + ".png";
    ALLEGRO_BITMAP* bmp = al_load_bitmap(fname.c_str());
    if (!bmp) throw Engine::Allegro5Exception(("failed to load image: " + fname).c_str());
    Engine::LOG(Engine::INFO) << "Loaded Resource<image>: " << fname;
    al_draw_tinted_scaled_rotated_bitmap(bmp, Tint, Anchor.x * GetBitmapWidth(), Anchor.y * GetBitmapHeight(),
        Position.x, Position.y, Size.x / GetBitmapWidth(), Size.y / GetBitmapHeight(), Rotation, 0);
}
void PeaShooter::Update(float deltaTime){
    Turret::Update(deltaTime);
    //int off = this->offset;
    Rotation = 0;
    num_peashooter++;
    if(num_peashooter % 5 == 0){
        this->off++;
        if(this->off > 12) this->off = 0;
    }
    //head.Position = Position;
    // Choose arbitrary one.
    std::random_device dev;
    std::mt19937 rng(dev());
    std::uniform_real_distribution<> dist(0.0f, 4.0f);
    float rnd = dist(rng);
}

Editor is loading...
Leave a Comment