add welcome stage and a little gui

This commit is contained in:
VisualGMQ 2022-02-10 21:22:06 +08:00
parent 5dcf222a0a
commit fd0bcce5c7
18 changed files with 153 additions and 75 deletions

BIN
assets/exit_btn.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 542 B

BIN
assets/start_btn.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 837 B

View File

@ -70,6 +70,8 @@ public:
FreeRotation = 0x02, FreeRotation = 0x02,
}; };
static constexpr int InfBullet = -1;
void Init(const std::string& name, void Init(const std::string& name,
Type type, Type type,
BulletCmpt::Type bulletType, BulletCmpt::Type bulletType,
@ -77,7 +79,8 @@ public:
int damage, int damage,
float shootSpeed, float shootSpeed,
float maxSpeed, float maxSpeed,
float duration) { float duration,
int bulletAmount = InfBullet) {
this->name = name; this->name = name;
this->type = type; this->type = type;
this->bulletType = bulletType; this->bulletType = bulletType;
@ -87,6 +90,7 @@ public:
this->shootDuration = duration; this->shootDuration = duration;
this->coolDown = 0; this->coolDown = 0;
this->maxSpeed = maxSpeed; this->maxSpeed = maxSpeed;
this->bulletAmount = bulletAmount;
} }
void Release() {} void Release() {}
@ -103,6 +107,7 @@ public:
float coolDown; float coolDown;
float maxSpeed; float maxSpeed;
Entity* owner; Entity* owner;
int bulletAmount;
}; };
class SpaceshipArmorCmpt: public Component { class SpaceshipArmorCmpt: public Component {

5
include/game/gui.hpp Normal file
View File

@ -0,0 +1,5 @@
#pragma once
#include "tinyengine/tinyengine.hpp"
bool Button(Texture* texture, const Point& pos, const Size& size);

View File

@ -1,26 +0,0 @@
#pragma once
#include "tinyengine/pch.hpp"
#include "tinyengine/libmath.hpp"
class Sprite {
public:
virtual ~Sprite() = default;
void Move(const Point& offset) { position_ += offset; dirty_ = true; }
void MoveTo(const Point& p) { position_ = p; dirty_ = true; }
void Rotate(const float degreeDelta) { rotation_ += degreeDelta; dirty_ = true; }
void RotateTo(const float degree) { rotation_ = degree; dirty_ = true; }
bool TryCalcView();
const Mat44& GetModel() const { return modelMat_; }
virtual void Update() { oldPosition_ = position_; }
private:
Point position_ = {0, 0};
Point oldPosition_;
float rotation_ = 0;
bool dirty_ = false;
Mat44 modelMat_;
};

View File

@ -0,0 +1,14 @@
#pragma once
#include "tinyengine/tinyengine.hpp"
class WelcomeScence: public Scence {
public:
void OnInit() override;
void OnRender() override;
private:
Unique<Texture> startImage_;
Unique<Texture> exitImage_;
Camera camera_;
};

View File

@ -14,7 +14,7 @@ public:
void Init(const std::string& title, const Size& size, Scence* scence); void Init(const std::string& title, const Size& size, Scence* scence);
void Shutdown(); void Shutdown();
void Exit() { shouldExit_ = true; } void Exit() { glfwSetWindowShouldClose(window_, 1); }
bool ShouldExit() const { return glfwWindowShouldClose(window_); } bool ShouldExit() const { return glfwWindowShouldClose(window_); }
void Update(float deltaTime); void Update(float deltaTime);

View File

@ -9,11 +9,11 @@ void Shoot(SpaceshipWeaponCmpt& weapon, const Point& dir) {
Entity* bullet; Entity* bullet;
bullet = weapon.ShootBullet(dir); bullet = weapon.ShootBullet(dir);
Bullets.Add(bullet); if (bullet) {
Bullets.Add(bullet);
weapon.coolDown = weapon.shootDuration; weapon.coolDown = weapon.shootDuration;
Sounds["shoot"]->Play();
Sounds["shoot"]->Play(); }
} }
void Shoot(SpaceshipWeaponCmpt& weapon, const Point& dir, Entity* target) { void Shoot(SpaceshipWeaponCmpt& weapon, const Point& dir, Entity* target) {
@ -25,14 +25,12 @@ void Shoot(SpaceshipWeaponCmpt& weapon, const Point& dir, Entity* target) {
Entity* bullet; Entity* bullet;
bullet = weapon.ShootMissile(dir, target); bullet = weapon.ShootMissile(dir, target);
if (bullet) {
// FIXME the rotation have some bugs // FIXME the rotation have some bugs
bullet->Use<BulletCmpt>()->rotation = Degrees(std::acos(-Normalize(dir).y)); bullet->Use<BulletCmpt>()->rotation = Degrees(std::acos(-Normalize(dir).y));
Bullets.Add(bullet);
Bullets.Add(bullet); weapon.coolDown = weapon.shootDuration;
Sounds["shoot"]->Play();
weapon.coolDown = weapon.shootDuration; }
Sounds["shoot"]->Play();
} }

View File

@ -2,6 +2,10 @@
#include "game/entity.hpp" #include "game/entity.hpp"
Entity* SpaceshipWeaponCmpt::ShootBullet(const Point& dir) { Entity* SpaceshipWeaponCmpt::ShootBullet(const Point& dir) {
if (bulletAmount == 0) return nullptr;
if (bulletAmount != InfBullet) {
bulletAmount --;
}
Entity* bullet = CreateBullet(damage, owner, maxSpeed); Entity* bullet = CreateBullet(damage, owner, maxSpeed);
bullet->Use<MotionCmpt>()->speed = Normalize(dir) * shootSpeed; bullet->Use<MotionCmpt>()->speed = Normalize(dir) * shootSpeed;
bullet->Use<MoveCmpt>()->position = owner->Get<MoveCmpt>()->position; bullet->Use<MoveCmpt>()->position = owner->Get<MoveCmpt>()->position;
@ -9,9 +13,13 @@ Entity* SpaceshipWeaponCmpt::ShootBullet(const Point& dir) {
} }
Entity* SpaceshipWeaponCmpt::ShootMissile(const Point& dir, Entity* target) { Entity* SpaceshipWeaponCmpt::ShootMissile(const Point& dir, Entity* target) {
if (bulletAmount == 0) return nullptr;
if (bulletAmount != InfBullet) {
bulletAmount --;
}
Entity* bullet = CreateMissile(damage, owner, maxSpeed, target); Entity* bullet = CreateMissile(damage, owner, maxSpeed, target);
if (target) { if (target) {
bullet->Use<BulletCmpt>()->rotation = Degrees(std::acos(Dot(dir, Normalize(target->Get<MoveCmpt>()->position - owner->Get<MoveCmpt>()->position)))); bullet->Use<BulletCmpt>()->rotation = Degrees(std::acos(Dot(dir, Normalize(target->Get<MoveCmpt>()->position - owner->Get<MoveCmpt>()->position))));
} }
bullet->Use<MotionCmpt>()->speed = Normalize(dir) * shootSpeed; bullet->Use<MotionCmpt>()->speed = Normalize(dir) * shootSpeed;
bullet->Use<MoveCmpt>()->position = owner->Get<MoveCmpt>()->position; bullet->Use<MoveCmpt>()->position = owner->Get<MoveCmpt>()->position;

View File

@ -41,7 +41,8 @@ Entity* CreateFightShip() {
LazerDamage, LazerDamage,
LazerShooterSpeed, LazerShooterSpeed,
LazerShooterMaxSpeed, LazerShooterMaxSpeed,
LazerShooterCooldown); LazerShooterCooldown,
10);
entity->Add<CollisionCmpt>(Size{16, 16}); entity->Add<CollisionCmpt>(Size{16, 16});
entity->Add<LifeCmpt>(FreightLife); entity->Add<LifeCmpt>(FreightLife);

12
src/game/gui.cpp Normal file
View File

@ -0,0 +1,12 @@
#include "game/gui.hpp"
#include "tinyengine/event.hpp"
bool Button(Texture* texture, const Point& pos, const Size& size) {
Renderer::DrawTexture(texture, nullptr, pos, size);
if (IsLeftPressing() &&
IsPointInRect(GetMousePosition(), Rect{pos.x - size.w / 2, pos.y - size.h / 2, size.w, size.h})) {
return true;
} else {
return false;
}
}

View File

@ -1,6 +1,8 @@
#include "game/stages/gamelogo.hpp" #include "game/stages/gamelogo.hpp"
#include "game/stages/space.hpp" #include "game/stages/space.hpp"
#include "game/stages/welcome.hpp"
#include "game/constants.hpp" #include "game/constants.hpp"
RUN_WINDOW("Space Sector", GameWindowSize.w, GameWindowSize.h, SpaceScence) RUN_WINDOW("Space Sector", GameWindowSize.w, GameWindowSize.h, WelcomeScence)
// RUN_WINDOW("Space Sector", GameWindowSize.w, GameWindowSize.h, SpaceScence)
// RUN_WINDOW("Space Sector", GameWindowSize.w, GameWindowSize.h, GameLogoScence) // RUN_WINDOW("Space Sector", GameWindowSize.w, GameWindowSize.h, GameLogoScence)

View File

@ -1,19 +0,0 @@
#include "game/sprite.hpp"
bool Sprite::TryCalcView() {
if (dirty_) {
float theta = Radians(rotation_);
float sinTheta = std::sin(theta);
float cosTheta = std::cos(theta);
modelMat_ = Mat44({
cosTheta, -sinTheta, 0, position_.x,
sinTheta, cosTheta, 0, position_.y,
0, 0, 1, 0,
0, 0, 0, 1,
});
dirty_ = false;
return true;
} else {
return false;
}
}

View File

@ -98,14 +98,14 @@ void SpaceScence::renderMiniMap() {
} }
void SpaceScence::renderWeapons(SpaceshipWeaponCmpt* weapon1, SpaceshipWeaponCmpt* weapon2) { void SpaceScence::renderWeapons(SpaceshipWeaponCmpt* weapon1, SpaceshipWeaponCmpt* weapon2) {
Rect mapRect = {0, 0, 200, 100}; Rect weaponInfoRect = {0, 0, 300, 100};
mapRect.y = GameWindowSize.h - mapRect.h; weaponInfoRect.y = GameWindowSize.h - weaponInfoRect.h;
mapRect.x = GameWindowSize.w - mapRect.w - 1; weaponInfoRect.x = GameWindowSize.w - weaponInfoRect.w - 1;
Renderer::SetDrawColor(Color{0, 0, 0, 255}); Renderer::SetDrawColor(Color{0, 0, 0, 1});
Renderer::FillRect(mapRect); Renderer::FillRect(weaponInfoRect);
Renderer::SetDrawColor(Color{255, 255, 255, 255}); Renderer::SetDrawColor(Color{1, 1, 1, 1});
Renderer::DrawRect(mapRect); Renderer::DrawRect(weaponInfoRect);
Point offset = {10, 10}; Point offset = {10, 10};
auto& font = engine.GetInnerBmpFont(); auto& font = engine.GetInnerBmpFont();
@ -113,15 +113,28 @@ void SpaceScence::renderWeapons(SpaceshipWeaponCmpt* weapon1, SpaceshipWeaponCmp
if (weapon1) { if (weapon1) {
font.Render(weapon1->name, font.Render(weapon1->name,
20, 20,
Point{mapRect.x, mapRect.y} + offset, Point{weaponInfoRect.x, weaponInfoRect.y} + offset,
Color{0, 200, 0, 255}); Color{0, 0.8, 0, 1});
if (weapon1->bulletAmount != SpaceshipWeaponCmpt::InfBullet) {
font.Render("[" + std::to_string(weapon1->bulletAmount) + "]",
20,
Point{weaponInfoRect.x + weaponInfoRect.w - 50, weaponInfoRect.y} + offset,
Color{0, 0.8, 0, 1});
}
offset.y += 20; offset.y += 20;
} }
if (weapon2) { if (weapon2) {
font.Render(weapon2->name, font.Render(weapon2->name,
20, 20,
Point{mapRect.x, mapRect.y} + offset, Point{weaponInfoRect.x, weaponInfoRect.y} + offset,
Color{0, 50, 0, 255}); Color{0, 0.8, 0, 1});
if (weapon2->bulletAmount != SpaceshipWeaponCmpt::InfBullet) {
font.Render("[" + std::to_string(weapon2->bulletAmount) + "]",
20,
Point{weaponInfoRect.x + weaponInfoRect.w - 100, weaponInfoRect.y} + offset,
Color{0, 0.8, 0, 1});
}
} }
} }

View File

@ -0,0 +1,32 @@
#include "game/stages/welcome.hpp"
#include "game/gui.hpp"
#include "game/stages/space.hpp"
void WelcomeScence::OnInit() {
startImage_.reset(new Texture("assets/start_btn.png"));
exitImage_.reset(new Texture("assets/exit_btn.png"));
}
void WelcomeScence::OnRender() {
Renderer::SetCamera(camera_);
auto& font = engine.GetInnerBmpFont();
std::string title = "SPACE SECTOR";
int pt = 70;
font.Render(title, pt,
Point{(GameWindowSize.w - title.size() * pt) / 2, 100},
Color{0.39, 0.6, 1, 1});
title = "-------MADE FOR 1M GAMES-------";
pt = 20;
font.Render(title, pt,
Point{(GameWindowSize.w - title.size() * pt) / 2, 200},
Color{0.8, 0.8, 1, 1});
if (Button(exitImage_.get(), Point{(GameWindowSize.w) / 2, 550}, Size{100, 100})) {
engine.Exit();
}
if (Button(startImage_.get(), Point{(GameWindowSize.w) / 2, 400}, Size{100, 100})) {
engine.ChangeScence(new SpaceScence);
}
}

View File

@ -87,6 +87,7 @@ void Engine::Shutdown() {
} }
void Engine::Update(float deltaTime) { void Engine::Update(float deltaTime) {
deltaTime = std::min(deltaTime, 0.3f);
if (scence_) if (scence_)
scence_->OnUpdate(deltaTime); scence_->OnUpdate(deltaTime);
} }

View File

@ -1,6 +1,11 @@
#include "tinyengine/event.hpp" #include "tinyengine/event.hpp"
#include "tinyengine/engine.hpp" #include "tinyengine/engine.hpp"
struct {
bool left = false;
bool right = false;
} MouseState;
void OnWindowResize(GLFWwindow* window, int width, int height) { void OnWindowResize(GLFWwindow* window, int width, int height) {
Renderer::SetViewport(0, 0, width, height); Renderer::SetViewport(0, 0, width, height);
} }

View File

@ -336,6 +336,33 @@ InnerBmpFont::InnerBmpFont() {
0,0,1,1,0,0,0,0, 0,0,1,1,0,0,0,0,
0,0,1,1,0,0,0,0, 0,0,1,1,0,0,0,0,
0,0,0,0,0,0,0,0}); 0,0,0,0,0,0,0,0});
saveChar('[',
{0,0,0,0,0,0,0,0,
0,0,1,1,1,1,0,0,
0,0,1,0,0,0,0,0,
0,0,1,0,0,0,0,0,
0,0,1,0,0,0,0,0,
0,0,1,0,0,0,0,0,
0,0,1,1,1,1,0,0,
0,0,0,0,0,0,0,0});
saveChar(']',
{0,0,0,0,0,0,0,0,
0,0,1,1,1,1,0,0,
0,0,0,0,0,1,0,0,
0,0,0,0,0,1,0,0,
0,0,0,0,0,1,0,0,
0,0,0,0,0,1,0,0,
0,0,1,1,1,1,0,0,
0,0,0,0,0,0,0,0});
saveChar('-',
{0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,
1,1,1,1,1,1,1,1,
1,1,1,1,1,1,1,1,
0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0});
errorChar_ = fontMat({1,1,1,1,1,1,1,1, errorChar_ = fontMat({1,1,1,1,1,1,1,1,
1,0,0,0,0,0,0,1, 1,0,0,0,0,0,0,1,