summary refs log tree commit diff stats
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/behaviour_system.cpp32
-rw-r--r--src/behaviour_system.h24
-rw-r--r--src/game.cpp4
-rw-r--r--src/game.h6
-rw-r--r--src/main.cpp14
-rw-r--r--src/map.cpp2
-rw-r--r--src/map.h1
-rw-r--r--src/sprite.h3
-rw-r--r--src/system.h1
9 files changed, 82 insertions, 5 deletions
diff --git a/src/behaviour_system.cpp b/src/behaviour_system.cpp new file mode 100644 index 0000000..8f17329 --- /dev/null +++ b/src/behaviour_system.cpp
@@ -0,0 +1,32 @@
1#include "behaviour_system.h"
2#include <random>
3#include "game.h"
4#include "character_system.h"
5#include "direction.h"
6
7void BehaviourSystem::tick(double dt) {
8 timer_.accumulate(dt);
9 while (timer_.step()) {
10 for (int spriteId : game_.getSprites()) {
11 Sprite& sprite = game_.getSprite(spriteId);
12 if (sprite.wander) {
13 // 75% chance of changing what's happening
14 if (std::bernoulli_distribution(0.75)(game_.getRng())) {
15 // 50% chance of choosing a direction or stopping
16 if (std::bernoulli_distribution(0.5)(game_.getRng())) {
17 Direction dir;
18 switch (std::uniform_int_distribution(0,3)(game_.getRng())) {
19 case 0: dir = Direction::left; break;
20 case 1: dir = Direction::up; break;
21 case 2: dir = Direction::right; break;
22 default: dir = Direction::down; break;
23 }
24 game_.getSystem<CharacterSystem>().moveInDirection(spriteId, dir);
25 } else {
26 game_.getSystem<CharacterSystem>().stopDirecting(spriteId);
27 }
28 }
29 }
30 }
31 }
32}
diff --git a/src/behaviour_system.h b/src/behaviour_system.h new file mode 100644 index 0000000..0eb4772 --- /dev/null +++ b/src/behaviour_system.h
@@ -0,0 +1,24 @@
1#ifndef BEHAVIOUR_SYSTEM_H_FC908ABE
2#define BEHAVIOUR_SYSTEM_H_FC908ABE
3
4#include "system.h"
5#include "timer.h"
6
7class Game;
8
9class BehaviourSystem : public System {
10public:
11
12 static constexpr SystemKey Key = SystemKey::Behaviour;
13
14 explicit BehaviourSystem(Game& game) : game_(game) {}
15
16 void tick(double dt) override;
17
18private:
19
20 Game& game_;
21 Timer timer_ { 500 };
22};
23
24#endif /* end of include guard: BEHAVIOUR_SYSTEM_H_FC908ABE */
diff --git a/src/game.cpp b/src/game.cpp index dbd314a..729e665 100644 --- a/src/game.cpp +++ b/src/game.cpp
@@ -62,6 +62,10 @@ void Game::loadMap(std::string filename) {
62 getSprite(spriteId).hasShadow = p.shadow; 62 getSprite(spriteId).hasShadow = p.shadow;
63 } 63 }
64 getSprite(spriteId).interactionScript = p.interactionScript; 64 getSprite(spriteId).interactionScript = p.interactionScript;
65 if (p.wander) {
66 getSystem<CharacterSystem>().initSprite(spriteId);
67 getSprite(spriteId).wander = true;
68 }
65 } 69 }
66 70
67 for (const Trigger& t : map_->getTriggers()) { 71 for (const Trigger& t : map_->getTriggers()) {
diff --git a/src/game.h b/src/game.h index 9340c65..72dd337 100644 --- a/src/game.h +++ b/src/game.h
@@ -8,6 +8,7 @@
8#include <memory> 8#include <memory>
9#include <map> 9#include <map>
10#include <set> 10#include <set>
11#include <random>
11#include "sprite.h" 12#include "sprite.h"
12#include "map.h" 13#include "map.h"
13#include "consts.h" 14#include "consts.h"
@@ -20,7 +21,7 @@ class Renderer;
20class Game { 21class Game {
21public: 22public:
22 23
23 explicit Game(Renderer& renderer) : font_("../res/font.txt", renderer) {} 24 Game(Renderer& renderer, std::mt19937& rng) : font_("../res/font.txt", renderer), rng_(&rng) {}
24 25
25 Mixer& getMixer() { return mixer_; } 26 Mixer& getMixer() { return mixer_; }
26 27
@@ -83,6 +84,8 @@ public:
83 84
84 const Font& getFont() const { return font_; } 85 const Font& getFont() const { return font_; }
85 86
87 std::mt19937& getRng() { return *rng_; }
88
86private: 89private:
87 90
88 Mixer mixer_; 91 Mixer mixer_;
@@ -97,6 +100,7 @@ private:
97 std::map<std::string, int> spritesByAlias_; 100 std::map<std::string, int> spritesByAlias_;
98 std::unique_ptr<Map> map_; 101 std::unique_ptr<Map> map_;
99 Font font_; 102 Font font_;
103 std::mt19937* rng_;
100}; 104};
101 105
102#endif /* end of include guard: GAME_H_E6F1396E */ 106#endif /* end of include guard: GAME_H_E6F1396E */
diff --git a/src/main.cpp b/src/main.cpp index f220dc0..9f503e1 100644 --- a/src/main.cpp +++ b/src/main.cpp
@@ -1,5 +1,6 @@
1#include <iostream> 1#include <iostream>
2#include <memory> 2#include <memory>
3#include <random>
3#include "renderer.h" 4#include "renderer.h"
4#include "game.h" 5#include "game.h"
5#include "timer.h" 6#include "timer.h"
@@ -12,12 +13,14 @@
12#include "message_system.h" 13#include "message_system.h"
13#include "script_system.h" 14#include "script_system.h"
14#include "effect_system.h" 15#include "effect_system.h"
16#include "behaviour_system.h"
15 17
16void loop(Renderer& renderer) { 18void loop(Renderer& renderer, std::mt19937& rng) {
17 Game game(renderer); 19 Game game(renderer, rng);
18 game.emplaceSystem<ScriptSystem>(); 20 game.emplaceSystem<ScriptSystem>();
19 game.emplaceSystem<TransformSystem>(); 21 game.emplaceSystem<TransformSystem>();
20 game.emplaceSystem<InputSystem>(); 22 game.emplaceSystem<InputSystem>();
23 game.emplaceSystem<BehaviourSystem>();
21 game.emplaceSystem<CharacterSystem>(); 24 game.emplaceSystem<CharacterSystem>();
22 game.emplaceSystem<AnimationSystem>(); 25 game.emplaceSystem<AnimationSystem>();
23 game.emplaceSystem<CameraSystem>(); 26 game.emplaceSystem<CameraSystem>();
@@ -26,7 +29,7 @@ void loop(Renderer& renderer) {
26 29
27 game.loadMap("map2"); 30 game.loadMap("map2");
28 31
29 vec2i warpLoc = game.getMap().getWarpPoint("debugWarp_mailboxes"); 32 vec2i warpLoc = game.getMap().getWarpPoint("debugWarp_rightside");
30 33
31 int lucasSprite = game.emplaceSprite("lucas"); 34 int lucasSprite = game.emplaceSprite("lucas");
32 game.getSystem<TransformSystem>().initSprite(lucasSprite, warpLoc); 35 game.getSystem<TransformSystem>().initSprite(lucasSprite, warpLoc);
@@ -83,11 +86,14 @@ void loop(Renderer& renderer) {
83} 86}
84 87
85int main(int, char**) { 88int main(int, char**) {
89 std::random_device randomEngine;
90 std::mt19937 rng(randomEngine());
91
86 try 92 try
87 { 93 {
88 Renderer renderer; 94 Renderer renderer;
89 95
90 loop(renderer); 96 loop(renderer, rng);
91 } catch (const sdl_error& ex) 97 } catch (const sdl_error& ex)
92 { 98 {
93 std::cout << "SDL error (" << ex.what() << ")" << std::endl; 99 std::cout << "SDL error (" << ex.what() << ")" << std::endl;
diff --git a/src/map.cpp b/src/map.cpp index 3686bbd..7bff071 100644 --- a/src/map.cpp +++ b/src/map.cpp
@@ -76,6 +76,8 @@ Map::Map(std::string_view name) : name_(name) {
76 p.interactionScript = property.getStringValue(); 76 p.interactionScript = property.getStringValue();
77 } else if (property.getName() == "shadow") { 77 } else if (property.getName() == "shadow") {
78 p.shadow = property.getBoolValue(); 78 p.shadow = property.getBoolValue();
79 } else if (property.getName() == "wander") {
80 p.wander = property.getBoolValue();
79 } 81 }
80 } 82 }
81 83
diff --git a/src/map.h b/src/map.h index 7dbe613..c5ecc64 100644 --- a/src/map.h +++ b/src/map.h
@@ -24,6 +24,7 @@ struct Prototype {
24 std::string animationFilename; 24 std::string animationFilename;
25 std::string interactionScript; 25 std::string interactionScript;
26 bool shadow = false; 26 bool shadow = false;
27 bool wander = false;
27}; 28};
28 29
29struct Trigger { 30struct Trigger {
diff --git a/src/sprite.h b/src/sprite.h index 19dbf92..82849fa 100644 --- a/src/sprite.h +++ b/src/sprite.h
@@ -78,6 +78,9 @@ public:
78 // Input 78 // Input
79 bool controllable = false; 79 bool controllable = false;
80 bool player = false; 80 bool player = false;
81
82 // Behaviour
83 bool wander = false;
81}; 84};
82 85
83#endif /* end of include guard: SPRITE_H_70503825 */ 86#endif /* end of include guard: SPRITE_H_70503825 */
diff --git a/src/system.h b/src/system.h index 01d6cec..c0b0637 100644 --- a/src/system.h +++ b/src/system.h
@@ -5,6 +5,7 @@ enum class SystemKey {
5 Script, 5 Script,
6 Transform, 6 Transform,
7 Input, 7 Input,
8 Behaviour,
8 Character, 9 Character,
9 Animation, 10 Animation,
10 Camera, 11 Camera,