• GameScene.cpp
  • #include "../Game.h"
    #include "../BackgroundRenderer.hpp"
    #include <Gorgon/Graphics/Bitmap.h>
    #include <Gorgon/String.h>
    
    void Game::PreparePlayerResources() {
        player.image.Import("resource/spaceship.png"); 
        player.image = player.image.ShrinkMultiple({2, 2});
        player.image.Prepare(); 
    }
    
    void Game::PrepareAlienResources() {
        Bitmap alien; 
        alien.Import("resource/alien.png");
        alien = alien.FlipY();
        alien = alien.ShrinkMultiple({4, 4});
        
        Gorgon::Graphics::BitmapAnimationProvider anim; 
        anim.Add(std::move(alien));
        anim.Prepare();
        this->alien = anim.MoveOutProvider();
    }
    
    void Game::PrepareLasers() {
        auto PrepareLaser = [&](int R, int G, int B) {
            Bitmap l;
            l.Resize(5, 10); 
            l.ForAllPixels([&l, R, G, B](int x, int y) {
                l(x, y, 0) = R;
                l(x, y, 1) = G;
                l(x, y, 2) = B;
                l(x, y, 3) = 255;
            });
            Gorgon::Graphics::BitmapAnimationProvider anim; 
            anim.Add(std::move(l));
            anim.Prepare();
            return anim;
        };
        
        player_laser = PrepareLaser(0, 255, 0).MoveOutProvider();
        alien_laser = PrepareLaser(255, 0, 0).MoveOutProvider();
    }
    
    void Game::ShootingScene() {
        using namespace Gorgon::UI::literals; 
        NewScene(1, std::move(ImageBackgroundRenderer{}), false, true, false); 
        ExecuteForScene<ImageBackgroundRenderer>(1, [&](Gorgon::Game::Template::Scene<ImageBackgroundRenderer>& scene) {
            srand(time(NULL));
            
            score.Move(400_px, 5_px);
            score.SetWidth(200_px);
            score.Text = Gorgon::String::Concat("**Points:** 0 **Health:** ", health);
            scene.GetUI().Add(score);
            
            timer_text.Move(10_px, 5_px); 
            timer_text.SetWidth(100_px);
            timer_text.Text = Gorgon::String::Concat(time_left, " sec"); 
            scene.GetUI().Add(timer_text); 
            
            timer.SetInterval(1000);
            timer.SetDoUpdate([&]() {
                if(not time_left) { timer.Stop(); }
                time_left -= 1; 
            });
            
            PreparePlayerResources();
            PrepareAlienResources();
            PrepareLasers();
    
    
            // Left
            keypress[0].do_if_pressed = [&]() {
                player.location.X -= 5;
            }; 
            // Right
            keypress[1].do_if_pressed = [&]() {
                player.location.X += 5;
            }; 
            // Up
            keypress[2].do_if_pressed = [&]() {
                player.location.Y -= 5;
            }; 
            // Down
            keypress[3].do_if_pressed = [&]() {
                player.location.Y += 5;
            }; 
    
            // Space
            keypress[4].do_if_pressed = [&]() {
                if (next_fire <= Delta) {
                    player_lasers.AddNew(Point {player.location.X + player.image.GetWidth() / 2, player.location.Y} ,player_laser.CreateAnimation());
                    next_fire += 300 - Delta;
                } else {
                    next_fire -= Delta; 
                }
            };
            
            scene.OnRender.Register([&](Gorgon::Graphics::Layer& graphics) {
                player.image.Draw(graphics, player.location); 
                for(auto& ll : player_lasers) {
                    ll.image.Draw(graphics, ll.location);
                }
                
                for(auto& al : alien_lasers) {
                    al.image.Draw(graphics, al.location);
                }
    
                for(auto& a : aliens) {
                    a.image.Draw(graphics, a.location);
                }
            }); 
            
            scene.OnKeyEvent.Register([&](Gorgon::Input::Key k, float active) {
                for(auto& key : keypress) {
                    key.PressIf(k, active); 
                    key.ReleaseIf(k, active); 
                }
            }); 
            
            
            scene.OnUpdate.Register([&](int delta) {    
                Delta = delta; 
                PlayerMech();
                LaserMovement();
                AddAlien(delta); 
                AlienMovement(delta);
                LaserCollusion();
                PlayerCollusion();
                if(health == 0) {
                    SwitchScene(2);
                }
                AlienSpeedUp(delta);
                AlienCountUp(delta);
                AlienFireLaser(delta);
                timer_text.Text = Gorgon::String::Concat(time_left, " sec"); 
            }); 
        }); 
    }