aboutsummaryrefslogtreecommitdiff
path: root/game
diff options
context:
space:
mode:
Diffstat (limited to 'game')
-rw-r--r--game/Config.h1
-rw-r--r--game/GameScene.cpp12
-rw-r--r--game/enemy/EnemyBulletScript.cpp8
-rw-r--r--game/enemy/EnemyBulletSubScene.cpp16
-rw-r--r--game/enemy/EnemyScript.cpp45
-rw-r--r--game/enemy/EnemyScript.h7
-rw-r--r--game/enemy/EnemySubScene.cpp3
-rw-r--r--game/main.cpp3
-rw-r--r--game/player/PlayerBulletScript.cpp10
-rw-r--r--game/player/PlayerBulletSubScene.cpp18
-rw-r--r--game/player/PlayerEndScript.cpp2
-rw-r--r--game/player/PlayerScript.cpp12
-rw-r--r--game/player/PlayerSubScene.cpp2
13 files changed, 84 insertions, 55 deletions
diff --git a/game/Config.h b/game/Config.h
index f787772..5b483cc 100644
--- a/game/Config.h
+++ b/game/Config.h
@@ -21,6 +21,7 @@ static constexpr int COLL_LAY_LASER = 7; // Only for GameScene
static constexpr int COLL_LAY_MISSILE = 8; // Only for GameScene
static constexpr int COLL_LAY_BULLET = 9; // Only for GameScene
static constexpr int COLL_LAY_ENEMY = 10; // Only for GameScene
+static constexpr int COLL_LAY_PLAYER_BULLET = 11; // Only for GameScene
static constexpr int GAME_HEIGHT = 800; // In game units
diff --git a/game/GameScene.cpp b/game/GameScene.cpp
index 383d96c..7c2166d 100644
--- a/game/GameScene.cpp
+++ b/game/GameScene.cpp
@@ -16,7 +16,8 @@
#include "enemy/EnemyPool.h"
#include "enemy/EnemySubScene.h"
#include "enemy/EnemyBulletPool.h"
-#include "enemy/BattleScript.h"#include "workers/WorkersSubScene.h"
+#include "enemy/BattleScript.h"
+#include "workers/WorkersSubScene.h"
#include <cmath>
#include <crepe/api/Animator.h>
@@ -53,7 +54,7 @@ void GameScene::load_scene() {
camera.add_component<BehaviorScript>().set_script<CoinSystemScript>();
camera.add_component<BehaviorScript>().set_script<HudScript>();
camera.add_component<BehaviorScript>().set_script<SpeedScript>();
-
+ camera.add_component<BehaviorScript>().set_script<BattleScript>();
camera.add_component<Rigidbody>(Rigidbody::Data {});
AI& enemy_path_1 = camera.add_component<AI>(400);
enemy_path_1.make_oval_path(100, 100, camera.transform.position, 1.5708, true);
@@ -96,7 +97,12 @@ void GameScene::load_scene() {
//create coin pool
CoinPoolSubScene coin_system;
coin_system.create_coins(*this);
-
+ EnemyBulletPool enemy_bullet_pool;
+ enemy_bullet_pool.create_bullets(*this);
+ PlayerBulletPool player_bullet_pool;
+ player_bullet_pool.create_bullets(*this);
+ EnemyPool enemy_pool;
+ enemy_pool.create_enemies(*this);
HudSubScene hud;
hud.create(*this);
GameObject background_music = new_object("background_music", "audio", vec2(0, 0));
diff --git a/game/enemy/EnemyBulletScript.cpp b/game/enemy/EnemyBulletScript.cpp
index 561d086..ba27b9d 100644
--- a/game/enemy/EnemyBulletScript.cpp
+++ b/game/enemy/EnemyBulletScript.cpp
@@ -14,7 +14,9 @@ void EnemyBulletScript::fixed_update(crepe::duration_t dt){
Transform& transform = this->get_component<Transform>();
Camera& camera = this->get_components_by_name<Camera>("camera").front();
Transform& cam_transform = this->get_components_by_name<Transform>("camera").front();
-
+ Rigidbody& bullet_body = this->get_component<Rigidbody>();
+ //move
+ transform.position.x += bullet_body.data.linear_velocity.x * dt.count();
vec2 half_screen = camera.viewport_size / 2;
float despawn_location = cam_transform.position.x - half_screen.x - 50;
if(transform.position.x < despawn_location){
@@ -30,7 +32,7 @@ void EnemyBulletScript::despawn_bullet(){
}
bool EnemyBulletScript::on_collide(const CollisionEvent& e){
- cout << "collision happened with " << e.info.other.metadata.tag << endl;
- //this->despawn_bullet();
+ //cout << "collision happened with " << e.info.other.metadata.tag << endl;
+ this->despawn_bullet();
return false;
}
diff --git a/game/enemy/EnemyBulletSubScene.cpp b/game/enemy/EnemyBulletSubScene.cpp
index 1660607..488dc03 100644
--- a/game/enemy/EnemyBulletSubScene.cpp
+++ b/game/enemy/EnemyBulletSubScene.cpp
@@ -21,21 +21,21 @@ int EnemyBulletSubScene::create(Scene & scn){
vec2 size = {20, 20};
static int counter = 0;
- string unique_name = "enemyBullet_" + to_string(counter++);
- GameObject bullet = scn.new_object(unique_name.c_str(),"EnemyBullet",vec2{0,-750},0,1);
+ string unique_name = "enemy_bullet_" + to_string(counter++);
+ GameObject bullet = scn.new_object(unique_name.c_str(),"enemy_bullet",vec2{0,-750},0,1);
Rigidbody& bullet_body = bullet.add_component<Rigidbody>(Rigidbody::Data {
.gravity_scale = 0,
- .body_type = Rigidbody::BodyType::DYNAMIC,
- .linear_velocity = vec2{-300,0},
- .collision_layers = {COLL_LAY_PLAYER},
- .collision_layer = COLL_LAY_BULLET,
-
+ .body_type = Rigidbody::BodyType::KINEMATIC,
+ .linear_velocity = vec2{-300,0},
+ .kinematic_collision = false,
+ .collision_layers = {COLL_LAY_MISSILE},
+ .collision_layer = COLL_LAY_BULLET
});
bullet_body.active = false;
BoxCollider& bullet_collider = bullet.add_component<BoxCollider>(vec2(60, 40));
- bullet_collider.active = false;
+ //bullet_collider.active = false;
Asset bullet_asset {"asset/other_effects/effect_smgbullet_x2.png"};
Sprite & bullet_sprite = bullet.add_component<Sprite>(
bullet_asset,
diff --git a/game/enemy/EnemyScript.cpp b/game/enemy/EnemyScript.cpp
index 1fbefaa..2bdab2c 100644
--- a/game/enemy/EnemyScript.cpp
+++ b/game/enemy/EnemyScript.cpp
@@ -1,4 +1,3 @@
-#include <iostream>
#include "../Config.h"
#include "EnemyScript.h"
#include <crepe/api/Animator.h>
@@ -8,44 +7,51 @@
#include <crepe/api/Rigidbody.h>
#include <crepe/api/Transform.h>
#include <crepe/types.h>
+#include <random>
+#include "../Random.h"
using namespace crepe;
using namespace std;
EnemyScript::EnemyScript(){
- //cout << column << std::endl;
engine.seed(rd());
this->last_fired = std::chrono::steady_clock::now();
+ this->shot_delay = std::chrono::duration<float>(2.5 + Random::f(0,1));
}
void EnemyScript::init(){
Metadata& meta = this->get_component<Metadata>();
this->subscribe<SpawnEnemyEvent>([this](const SpawnEnemyEvent& e) -> bool{
return this->spawn_enemy(e);
},meta.game_object_id);
+ this->subscribe<CollisionEvent>([this](const CollisionEvent& e) -> bool {
+ return this->on_collide(e);
+ });
};
void EnemyScript::fixed_update(duration_t dt) {
-
+ if(this->alive){
+ return;
+ }
Transform& transform = this->get_component<Transform>();
Transform& player_transform = this->get_components_by_name<Transform>("player").front();
+ Rigidbody& enemy_body = this->get_component<Rigidbody>();
AI& ai_component = this->get_component<AI>();
-
+
+ //transform.position += enemy_body.data.linear_velocity * dt.count();
float direction_to_player_y = player_transform.position.y - transform.position.y;
float distance_to_player_y = std::abs(direction_to_player_y);
float adjustment_speed = speed * (distance_to_player_y / MAX_DISTANCE);
adjustment_speed = std::clamp(adjustment_speed, MIN_SPEED, MAX_SPEED);
- //cout << "speed: "<< adjustment_speed << endl;
- //cout << "direction: " << direction_to_player_y << endl;
// Move the path nodes on the Y-axis
for (vec2& path_node : ai_component.path) {
path_node.y += (direction_to_player_y > 0 ? 1 : -1) * adjustment_speed * dt.count();
}
//bullet fire logic:
-
auto now = std::chrono::steady_clock::now();
std::chrono::duration<float> elapsed = now - last_fired;
- if (elapsed > std::chrono::duration<float>(5)) {
+ if (elapsed > shot_delay) {
this->shoot(transform.position,0);
last_fired = now;
+ this->shot_delay = std::chrono::duration<float>(Random::f(0.8,4));
}
}
@@ -55,8 +61,8 @@ bool EnemyScript::spawn_enemy(const SpawnEnemyEvent& e){
Transform& transform = this->get_component<Transform>();
Camera& camera = this->get_components_by_name<Camera>("camera").front();
Transform& cam_transform = this->get_components_by_name<Transform>("camera").front();
+
vec2 half_screen = camera.viewport_size / 2;
- //cout << "column: " << e.column << endl;
float x_value = cam_transform.position.x + half_screen.x - 50 * (1 + e.column);
uniform_real_distribution<float> dist(
cam_transform.position.y - half_screen.y + 100,
@@ -68,20 +74,27 @@ bool EnemyScript::spawn_enemy(const SpawnEnemyEvent& e){
// transform.position = vec2{cam_transform}
ai_component.path.clear();
ai_component.make_oval_path(10, 10, vec2{x_value,random_height}, 1.5708, true);
-
+ ai_component.active = true;
return true;
}
-void EnemyScript::onCollide(const CollisionEvent & collisionData){
-
+bool EnemyScript::on_collide(const CollisionEvent & e){
+ if(e.info.other.metadata.tag == "player_bullet"){
+ this->despawn_enemy();
+ }
+ return false;
+}
+void EnemyScript::despawn_enemy(){
+ Transform& transform = this->get_component<Transform>();
+ transform.position = vec2{0,-650};
+ AI& ai_component = this->get_component<AI>();
+ // Rigidbody& enemy_body
+ ai_component.active = false;
}
-
void EnemyScript::shoot(const vec2& location,float angle){
- //cout << "enemy shot" << endl;
- RefVector<Transform> bullet_transforms = this->get_components_by_tag<Transform>("EnemyBullet");
+ RefVector<Transform> bullet_transforms = this->get_components_by_tag<Transform>("enemy_bullet");
for(Transform& bullet_pos : bullet_transforms){
- //cout << "bullet pos x: " << bullet_pos.position.x << " y: " << bullet_pos.position.y << endl;
if(bullet_pos.position.x == 0 && bullet_pos.position.y == -750){
bullet_pos.position = location;
diff --git a/game/enemy/EnemyScript.h b/game/enemy/EnemyScript.h
index 35d2626..7babe4d 100644
--- a/game/enemy/EnemyScript.h
+++ b/game/enemy/EnemyScript.h
@@ -15,16 +15,17 @@ class EnemyScript : public crepe::Script {
void init() override;
void fixed_update(crepe::duration_t dt) override;
void shoot(const crepe::vec2& position,float angle);
- void onCollide(const crepe::CollisionEvent & collisionData);
+ bool on_collide(const crepe::CollisionEvent & collisionData);
+ void despawn_enemy();
bool spawn_enemy(const SpawnEnemyEvent& e);
private:
std::random_device rd;
std::default_random_engine engine;
-
+ bool alive = false;
float speed = 50;
const float MIN_SPEED = 10;
const float MAX_SPEED = 130;
const float MAX_DISTANCE = 100;
std::chrono::time_point<std::chrono::steady_clock> last_fired;
- std::chrono::duration<float> shot_delay;
+ std::chrono::duration<float> shot_delay = std::chrono::duration<float>(0);
};
diff --git a/game/enemy/EnemySubScene.cpp b/game/enemy/EnemySubScene.cpp
index 5618829..43f9b33 100644
--- a/game/enemy/EnemySubScene.cpp
+++ b/game/enemy/EnemySubScene.cpp
@@ -26,9 +26,8 @@ int EnemySubScene::create(Scene & scn){
.gravity_scale = 0,
.body_type = Rigidbody::BodyType::DYNAMIC,
- // .collision_layers
- // = {COLL_LAY_BOT_TOP, COLL_LAY_ZAPPER, COLL_LAY_LASER},
.max_linear_velocity = 400,
+ .collision_layers = {COLL_LAY_BOT_TOP,COLL_LAY_PLAYER_BULLET},
.collision_layer = COLL_LAY_ENEMY,
});
diff --git a/game/main.cpp b/game/main.cpp
index e341353..3f7e17e 100644
--- a/game/main.cpp
+++ b/game/main.cpp
@@ -9,9 +9,10 @@ using namespace crepe;
int main() {
Engine gameloop;
+ gameloop.add_scene<GameScene>();
gameloop.add_scene<MainMenuScene>();
gameloop.add_scene<ShopMenuScene>();
- gameloop.add_scene<GameScene>();
+
return gameloop.main();
}
diff --git a/game/player/PlayerBulletScript.cpp b/game/player/PlayerBulletScript.cpp
index 50b6617..2bd067d 100644
--- a/game/player/PlayerBulletScript.cpp
+++ b/game/player/PlayerBulletScript.cpp
@@ -17,7 +17,9 @@ void PlayerBulletScript::fixed_update(crepe::duration_t dt){
Transform& transform = this->get_component<Transform>();
Camera& camera = this->get_components_by_name<Camera>("camera").front();
Transform& cam_transform = this->get_components_by_name<Transform>("camera").front();
-
+ Rigidbody& bullet_body = this->get_component<Rigidbody>();
+ transform.rotation += bullet_body.data.angular_velocity;
+ transform.position += bullet_body.data.linear_velocity * dt.count();
vec2 half_screen = camera.viewport_size / 2;
float despawn_location = cam_transform.position.x + half_screen.x + 50;
if(transform.position.x > despawn_location){
@@ -29,11 +31,13 @@ void PlayerBulletScript::despawn_bullet(){
Transform& transform = this->get_component<Transform>();
Rigidbody& bullet_body = this->get_component<Rigidbody>();
bullet_body.active = false;
+ BehaviorScript& bullet_script = this->get_component<BehaviorScript>();
+ bullet_script.active = false;
transform.position = {0,-850};
}
bool PlayerBulletScript::on_collide(const CollisionEvent& e){
- cout << "collision happened with " << e.info.other.metadata.tag << endl;
- //this->despawn_bullet();
+ cout << "player bullet collision happened with " << e.info.other.metadata.tag << endl;
+ this->despawn_bullet();
return false;
}
diff --git a/game/player/PlayerBulletSubScene.cpp b/game/player/PlayerBulletSubScene.cpp
index eb89260..4b36387 100644
--- a/game/player/PlayerBulletSubScene.cpp
+++ b/game/player/PlayerBulletSubScene.cpp
@@ -21,22 +21,24 @@ using namespace std;
int PlayerBulletSubScene::create(Scene & scn){
vec2 size = {20, 20};
static int counter = 0;
- string unique_name = "playerBullet_" + to_string(counter++);
- GameObject player_bullet = scn.new_object(unique_name.c_str(),"PlayerBullet",vec2{0,-850},0,1);
+ string unique_name = "player_bullet_" + to_string(counter++);
+ GameObject player_bullet = scn.new_object(unique_name.c_str(),"player_bullet",vec2{0,-850},0,1);
Rigidbody& player_bullet_body = player_bullet.add_component<Rigidbody>(Rigidbody::Data {
.gravity_scale = 0,
- .body_type = Rigidbody::BodyType::DYNAMIC,
+ .body_type = Rigidbody::BodyType::KINEMATIC,
.linear_velocity = vec2{300,0},
.angular_velocity = 150,
- // .collision_layers = {COLL_LAY_PLAYER},
- // .collision_layer = COLL_LAY_BULLET,
-
+ .kinematic_collision = false,
+ .collision_layers = {COLL_LAY_ENEMY},
+
+ .collision_layer = COLL_LAY_PLAYER_BULLET,
+
});
player_bullet_body.active = false;
BoxCollider& player_bullet_collider = player_bullet.add_component<BoxCollider>(vec2(60, 40));
- player_bullet_collider.active = false;
+ //player_bullet_collider.active = false;
Asset player_bullet_asset {"asset/other_effects/crepe.png"};
Sprite & player_bullet_sprite = player_bullet.add_component<Sprite>(
player_bullet_asset,
@@ -47,6 +49,6 @@ int PlayerBulletSubScene::create(Scene & scn){
.size = vec2(30,0),
}
);
- player_bullet.add_component<BehaviorScript>().set_script<PlayerBulletScript>();
+ player_bullet.add_component<BehaviorScript>().set_script<PlayerBulletScript>().active = false;
return counter;
}
diff --git a/game/player/PlayerEndScript.cpp b/game/player/PlayerEndScript.cpp
index 92e48e3..fb18f2f 100644
--- a/game/player/PlayerEndScript.cpp
+++ b/game/player/PlayerEndScript.cpp
@@ -1,4 +1,3 @@
-#include <iostream>
#include "PlayerEndScript.h"
#include "../Config.h"
@@ -24,7 +23,6 @@ void PlayerEndScript::init() {
}
bool PlayerEndScript::on_collision(const crepe::CollisionEvent & ev) {
- cout << "collision player" << endl;
if (ev.info.other.metadata.name == "floor") {
Transform & transform_player
= this->get_components_by_name<Transform>("player").front();
diff --git a/game/player/PlayerScript.cpp b/game/player/PlayerScript.cpp
index 4e253f4..dc3eec3 100644
--- a/game/player/PlayerScript.cpp
+++ b/game/player/PlayerScript.cpp
@@ -61,7 +61,7 @@ bool PlayerScript::on_collision(const CollisionEvent & ev) {
audio.play();
return false;
- } else if (ev.info.other.metadata.tag == "missile") {
+ } else if (ev.info.other.metadata.tag == "missile" || ev.info.other.metadata.tag == "enemy_bullet") {
for (Animator & anim : animators) {
anim.active = true;
anim.set_anim(5);
@@ -157,21 +157,23 @@ void PlayerScript::fixed_update(crepe::duration_t dt) {
}
void PlayerScript::shoot(const vec2& location,float angle){
- cout << "player shot" << endl;
- RefVector<Transform> bullet_transforms = this->get_components_by_tag<Transform>("PlayerBullet");
+ //cout << "player shot" << endl;
+ RefVector<Transform> bullet_transforms = this->get_components_by_tag<Transform>("player_bullet");
for(Transform& bullet_pos : bullet_transforms){
//cout << "bullet pos x: " << bullet_pos.position.x << " y: " << bullet_pos.position.y << endl;
if(bullet_pos.position.x == 0 && bullet_pos.position.y == -850){
- cout << "bullet found\n";
+ //cout << "bullet found\n";
bullet_pos.position = location;
bullet_pos.position.x += 20;
- cout << "bullet pos x: " << bullet_pos.position.x << " y: " << bullet_pos.position.y << endl;
+ //cout << "bullet pos x: " << bullet_pos.position.x << " y: " << bullet_pos.position.y << endl;
Rigidbody& bullet_body = this->get_components_by_id<Rigidbody>(bullet_pos.game_object_id).front();
BoxCollider bullet_collider = this->get_components_by_id<BoxCollider>(bullet_pos.game_object_id).front();
//bullet_collider.active = true;
bullet_body.active = true;
+ BehaviorScript& bullet_script = this->get_components_by_id<BehaviorScript>(bullet_pos.game_object_id).front();
+ bullet_script.active = true;
return;
}
}
diff --git a/game/player/PlayerSubScene.cpp b/game/player/PlayerSubScene.cpp
index be104b5..1203ca0 100644
--- a/game/player/PlayerSubScene.cpp
+++ b/game/player/PlayerSubScene.cpp
@@ -152,7 +152,7 @@ PlayerSubScene::PlayerSubScene(Scene & scn) {
= {COLL_LAY_BOT_TOP, COLL_LAY_ZAPPER, COLL_LAY_LASER, COLL_LAY_MISSILE,COLL_LAY_BULLET},
.collision_layer = COLL_LAY_PLAYER,
});
- player.add_component<BehaviorScript>().set_script<PlayerScript>().active = false;
+ player.add_component<BehaviorScript>().set_script<PlayerScript>().active = true;
player.add_component<BehaviorScript>().set_script<CoinScript>();
player.add_component<BehaviorScript>().set_script<PlayerEndScript>().active = false;