Monad Engine da02fba2
> Undercity Codex_
Loading...
Searching...
No Matches
Player.cpp
Go to the documentation of this file.
1#include "Player.h"
2#include "Bullet.h"
3
4#include <GLFW/glfw3.h>
5
7{
8 if (auto bullet = FindChildByName("bullet_33"))
9 {
10 bullet->SetActive(false);
11 }
12
13 if (auto fire = FindChildByName("BOOM_35"))
14 {
15 fire->SetActive(false);
16 }
17
18 if (auto gun = FindChildByName("Gun"))
19 {
20 m_animationComponent = gun->GetComponent<eng::AnimationComponent>();
21 }
22
23 m_audioComponent = GetComponent<eng::AudioComponent>();
24 m_playerControllerComponent = GetComponent<eng::PlayerControllerComponent>();
25}
26
27void Player::Update(float deltaTime)
28{
29 eng::GameObject::Update(deltaTime);
30
32 if (input.IsMouseButtonPressed(GLFW_MOUSE_BUTTON_LEFT))
33 {
34 if (m_animationComponent && !m_animationComponent->IsPlaying())
35 {
36 m_animationComponent->Play("shoot", false);
37
38 if (m_audioComponent)
39 {
40 if (m_audioComponent->IsPlaying("shoot"))
41 {
42 m_audioComponent->Stop("shoot");
43 }
44 m_audioComponent->Play("shoot");
45 }
46
47 auto bullet = m_scene->CreateObject<Bullet>("Bullet");
48 auto material = eng::Material::Load("materials/suzanne.mat");
49 auto mesh = eng::Mesh::CreateSphere(0.2f, 32, 32);
50 bullet->AddComponent(new eng::MeshComponent(material, mesh));
51
52 glm::vec3 pos = glm::vec3(0.0f);
53 if (auto child = FindChildByName("BOOM_35"))
54 {
55 pos = child->GetWorldPosition();
56 }
57 bullet->SetPosition(pos + m_rotation * glm::vec3(-0.2f, 0.2f, -1.75f));
58
59 auto collider = std::make_shared<eng::SphereCollider>(0.2f);
60 auto rigidBody = std::make_shared<eng::RigidBody>(
61 eng::BodyType::Dynamic, collider, 10.0f, 0.1f);
62 bullet->AddComponent(new eng::PhysicsComponent(rigidBody));
63
64 glm::vec3 front = m_rotation * glm::vec3(0.0f, 0.0f, -1.0f);
65 rigidBody->ApplyImpulse(front * 500.0f);
66 }
67 }
68
69 if (input.IsKeyPressed(GLFW_KEY_SPACE))
70 {
71 if (m_audioComponent && !m_audioComponent->IsPlaying("jump"))
72 {
73 m_audioComponent->Play("jump");
74 }
75 }
76
77 bool walking =
78 input.IsKeyPressed(GLFW_KEY_W) ||
79 input.IsKeyPressed(GLFW_KEY_A) ||
80 input.IsKeyPressed(GLFW_KEY_S) ||
81 input.IsKeyPressed(GLFW_KEY_D);
82
83 if (walking && m_playerControllerComponent && m_playerControllerComponent->OnGround())
84 {
85 if (m_audioComponent && !m_audioComponent->IsPlaying("step"))
86 {
87 m_audioComponent->Play("step", true);
88 }
89 }
90 else
91 {
92 if (m_audioComponent && m_audioComponent->IsPlaying("step"))
93 {
94 m_audioComponent->Stop("step");
95 }
96 }
97}
std::shared_ptr< Material > material
std::shared_ptr< Mesh > mesh
Definition Bullet.h:6
void Update(float deltaTime) override
Definition Player.cpp:27
void Init() override
Definition Player.cpp:6
void Play(const std::string &name, bool loop=true)
void Play(const std::string &name, bool loop=false)
void Stop(const std::string &name)
bool IsPlaying(const std::string &name)
InputManager & GetInputManager()
Definition Engine.cpp:190
static Engine & GetInstance()
Definition Engine.cpp:50
virtual void Update(float deltaTime)
Scene * m_scene
Definition GameObject.h:84
GameObject * FindChildByName(const std::string &name)
glm::quat m_rotation
Definition GameObject.h:89
static std::shared_ptr< Material > Load(const std::string &path)
Definition Material.cpp:70
static std::shared_ptr< Mesh > CreateSphere(float radius, int sectors, int stacks)
Definition Mesh.cpp:187
GameObject * CreateObject(const std::string &name, GameObject *parent=nullptr)
Definition Scene.cpp:65