Skip to content
Permalink
Browse files

finished part 6

  • Loading branch information
thatgamesguy committed Jul 3, 2018
1 parent 9a32b74 commit 95ce84f975ec3d1305d316c17c123f037dfc9922
Showing with 12,796 additions and 18 deletions.
  1. +1 −3 Part 1/src/ObjectCollection.cpp
  2. +0 −2 Part 2/src/ObjectCollection.cpp
  3. +5 −0 Part 3/src/C_NeuralNetwork.cpp
  4. +2 −0 Part 3/src/C_NeuralNetwork.hpp
  5. +0 −2 Part 3/src/ObjectCollection.cpp
  6. +5 −0 Part 4/src/C_NeuralNetwork.cpp
  7. +2 −0 Part 4/src/C_NeuralNetwork.hpp
  8. +2 −1 Part 4/src/GeneticAlgorithm.cpp
  9. +2 −0 Part 4/src/GeneticAlgorithm.hpp
  10. +0 −2 Part 4/src/ObjectCollection.cpp
  11. BIN Part 5/resources/that_games_guy_logo.png
  12. BIN Part 5/resources/ufo1.png
  13. BIN Part 5/resources/ufo2.png
  14. BIN Part 5/resources/ufo3.png
  15. BIN Part 5/resources/ufo4.png
  16. +83 −0 Part 5/src/Animation.cpp
  17. +50 −0 Part 5/src/Animation.hpp
  18. +45 −0 Part 5/src/Bitmask.cpp
  19. +26 −0 Part 5/src/Bitmask.hpp
  20. +68 −0 Part 5/src/C_Animation.cpp
  21. +36 −0 Part 5/src/C_Animation.hpp
  22. +26 −0 Part 5/src/C_DamageOnCollision.cpp
  23. +25 −0 Part 5/src/C_DamageOnCollision.hpp
  24. +27 −0 Part 5/src/C_DamageOnWallHit.cpp
  25. +26 −0 Part 5/src/C_DamageOnWallHit.hpp
  26. +13 −0 Part 5/src/C_Energy.cpp
  27. +19 −0 Part 5/src/C_Energy.hpp
  28. +13 −0 Part 5/src/C_InstanceID.cpp
  29. +17 −0 Part 5/src/C_InstanceID.hpp
  30. +63 −0 Part 5/src/C_KeyboardMovement.cpp
  31. +27 −0 Part 5/src/C_KeyboardMovement.hpp
  32. +86 −0 Part 5/src/C_NeuralNetwork.cpp
  33. +40 −0 Part 5/src/C_NeuralNetwork.hpp
  34. +43 −0 Part 5/src/C_ScreenWrapAround.cpp
  35. +21 −0 Part 5/src/C_ScreenWrapAround.hpp
  36. +81 −0 Part 5/src/C_Sight.cpp
  37. +58 −0 Part 5/src/C_Sight.hpp
  38. +70 −0 Part 5/src/C_Sprite.cpp
  39. +36 −0 Part 5/src/C_Sprite.hpp
  40. +13 −0 Part 5/src/C_TimeAlive.cpp
  41. +19 −0 Part 5/src/C_TimeAlive.hpp
  42. +50 −0 Part 5/src/C_Transform.cpp
  43. +29 −0 Part 5/src/C_Transform.hpp
  44. +30 −0 Part 5/src/C_Velocity.cpp
  45. +23 −0 Part 5/src/C_Velocity.hpp
  46. +24 −0 Part 5/src/Component.hpp
  47. +55 −0 Part 5/src/Game.cpp
  48. +36 −0 Part 5/src/Game.hpp
  49. +196 −0 Part 5/src/GeneticAlgorithm.cpp
  50. +52 −0 Part 5/src/GeneticAlgorithm.hpp
  51. +41 −0 Part 5/src/Input.cpp
  52. +31 −0 Part 5/src/Input.hpp
  53. +17 −0 Part 5/src/Main.cpp
  54. +131 −0 Part 5/src/NeuralNetwork.cpp
  55. +34 −0 Part 5/src/NeuralNetwork.hpp
  56. +16 −0 Part 5/src/Neuron.cpp
  57. +14 −0 Part 5/src/Neuron.hpp
  58. +12 −0 Part 5/src/NeuronLayer.cpp
  59. +14 −0 Part 5/src/NeuronLayer.hpp
  60. +57 −0 Part 5/src/Object.cpp
  61. +70 −0 Part 5/src/Object.hpp
  62. +78 −0 Part 5/src/ObjectCollection.cpp
  63. +30 −0 Part 5/src/ObjectCollection.hpp
  64. +64 −0 Part 5/src/ResourceAllocator.hpp
  65. +21 −0 Part 5/src/Scene.hpp
  66. +38 −0 Part 5/src/SceneGame.cpp
  67. +37 −0 Part 5/src/SceneGame.hpp
  68. +50 −0 Part 5/src/SceneSplashScreen.cpp
  69. +41 −0 Part 5/src/SceneSplashScreen.hpp
  70. +76 −0 Part 5/src/SceneStateMachine.cpp
  71. +30 −0 Part 5/src/SceneStateMachine.hpp
  72. +27 −0 Part 5/src/Tile.h
  73. +169 −0 Part 5/src/TileMapParser.cpp
  74. +55 −0 Part 5/src/TileMapParser.hpp
  75. +19 −0 Part 5/src/Utilities.h
  76. +56 −0 Part 5/src/Window.cpp
  77. +26 −0 Part 5/src/Window.hpp
  78. +22 −0 Part 5/src/WorkingDirectory.cpp
  79. +24 −0 Part 5/src/WorkingDirectory.hpp
  80. +2,596 −0 Part 5/src/rapidxml.hpp
  81. +174 −0 Part 5/src/rapidxml_iterators.hpp
  82. +421 −0 Part 5/src/rapidxml_print.hpp
  83. +122 −0 Part 5/src/rapidxml_utils.hpp
  84. +100 −0 Part 6/resources/ga_log.txt
  85. BIN Part 6/resources/that_games_guy_logo.png
  86. BIN Part 6/resources/ufo1.png
  87. BIN Part 6/resources/ufo2.png
  88. BIN Part 6/resources/ufo3.png
  89. BIN Part 6/resources/ufo4.png
  90. +83 −0 Part 6/src/Animation.cpp
  91. +50 −0 Part 6/src/Animation.hpp
  92. +45 −0 Part 6/src/Bitmask.cpp
  93. +26 −0 Part 6/src/Bitmask.hpp
  94. +68 −0 Part 6/src/C_Animation.cpp
  95. +36 −0 Part 6/src/C_Animation.hpp
  96. +26 −0 Part 6/src/C_DamageOnCollision.cpp
  97. +25 −0 Part 6/src/C_DamageOnCollision.hpp
  98. +27 −0 Part 6/src/C_DamageOnWallHit.cpp
  99. +26 −0 Part 6/src/C_DamageOnWallHit.hpp
  100. +13 −0 Part 6/src/C_Energy.cpp
  101. +19 −0 Part 6/src/C_Energy.hpp
  102. +13 −0 Part 6/src/C_InstanceID.cpp
  103. +17 −0 Part 6/src/C_InstanceID.hpp
  104. +63 −0 Part 6/src/C_KeyboardMovement.cpp
  105. +27 −0 Part 6/src/C_KeyboardMovement.hpp
  106. +86 −0 Part 6/src/C_NeuralNetwork.cpp
  107. +40 −0 Part 6/src/C_NeuralNetwork.hpp
  108. +43 −0 Part 6/src/C_ScreenWrapAround.cpp
  109. +21 −0 Part 6/src/C_ScreenWrapAround.hpp
  110. +81 −0 Part 6/src/C_Sight.cpp
  111. +58 −0 Part 6/src/C_Sight.hpp
  112. +70 −0 Part 6/src/C_Sprite.cpp
  113. +36 −0 Part 6/src/C_Sprite.hpp
  114. +13 −0 Part 6/src/C_TimeAlive.cpp
  115. +19 −0 Part 6/src/C_TimeAlive.hpp
  116. +50 −0 Part 6/src/C_Transform.cpp
  117. +29 −0 Part 6/src/C_Transform.hpp
  118. +30 −0 Part 6/src/C_Velocity.cpp
  119. +23 −0 Part 6/src/C_Velocity.hpp
  120. +24 −0 Part 6/src/Component.hpp
  121. +55 −0 Part 6/src/Game.cpp
  122. +36 −0 Part 6/src/Game.hpp
  123. +209 −0 Part 6/src/GeneticAlgorithm.cpp
  124. +57 −0 Part 6/src/GeneticAlgorithm.hpp
  125. +41 −0 Part 6/src/Input.cpp
  126. +31 −0 Part 6/src/Input.hpp
  127. +17 −0 Part 6/src/Main.cpp
  128. +201 −0 Part 6/src/NeuralNetwork.cpp
  129. +37 −0 Part 6/src/NeuralNetwork.hpp
  130. +16 −0 Part 6/src/Neuron.cpp
  131. +14 −0 Part 6/src/Neuron.hpp
  132. +12 −0 Part 6/src/NeuronLayer.cpp
  133. +14 −0 Part 6/src/NeuronLayer.hpp
  134. +57 −0 Part 6/src/Object.cpp
  135. +70 −0 Part 6/src/Object.hpp
  136. +78 −0 Part 6/src/ObjectCollection.cpp
  137. +30 −0 Part 6/src/ObjectCollection.hpp
  138. +64 −0 Part 6/src/ResourceAllocator.hpp
  139. +21 −0 Part 6/src/Scene.hpp
  140. +38 −0 Part 6/src/SceneGame.cpp
  141. +37 −0 Part 6/src/SceneGame.hpp
  142. +50 −0 Part 6/src/SceneSplashScreen.cpp
  143. +41 −0 Part 6/src/SceneSplashScreen.hpp
  144. +76 −0 Part 6/src/SceneStateMachine.cpp
  145. +30 −0 Part 6/src/SceneStateMachine.hpp
  146. +27 −0 Part 6/src/Tile.h
  147. +169 −0 Part 6/src/TileMapParser.cpp
  148. +55 −0 Part 6/src/TileMapParser.hpp
  149. +19 −0 Part 6/src/Utilities.h
  150. +56 −0 Part 6/src/Window.cpp
  151. +26 −0 Part 6/src/Window.hpp
  152. +22 −0 Part 6/src/WorkingDirectory.cpp
  153. +24 −0 Part 6/src/WorkingDirectory.hpp
  154. +2,596 −0 Part 6/src/rapidxml.hpp
  155. +174 −0 Part 6/src/rapidxml_iterators.hpp
  156. +421 −0 Part 6/src/rapidxml_print.hpp
  157. +122 −0 Part 6/src/rapidxml_utils.hpp
  158. +6 −0 Xcode Project/AI Experiments 1 - UFO/AI Experiments 1 - UFO.xcodeproj/project.pbxproj
  159. +10 −0 Xcode Project/AI Experiments 1 - UFO/AI Experiments 1 - UFO/C_NeuralNetwork.cpp
  160. +4 −2 Xcode Project/AI Experiments 1 - UFO/AI Experiments 1 - UFO/C_NeuralNetwork.hpp
  161. +13 −0 Xcode Project/AI Experiments 1 - UFO/AI Experiments 1 - UFO/C_TimeAlive.cpp
  162. +19 −0 Xcode Project/AI Experiments 1 - UFO/AI Experiments 1 - UFO/C_TimeAlive.hpp
  163. +111 −4 Xcode Project/AI Experiments 1 - UFO/AI Experiments 1 - UFO/GeneticAlgorithm.cpp
  164. +15 −0 Xcode Project/AI Experiments 1 - UFO/AI Experiments 1 - UFO/GeneticAlgorithm.hpp
  165. +121 −0 Xcode Project/AI Experiments 1 - UFO/AI Experiments 1 - UFO/NeuralNetwork.cpp
  166. +9 −0 Xcode Project/AI Experiments 1 - UFO/AI Experiments 1 - UFO/NeuralNetwork.hpp
  167. +0 −2 Xcode Project/AI Experiments 1 - UFO/AI Experiments 1 - UFO/ObjectCollection.cpp
@@ -47,9 +47,7 @@ void ObjectCollection::ProcessNewObjects()
{
o->Start();
}

objects.assign(newObjects.begin(), newObjects.end());


objects.insert(objects.end(), newObjects.begin(), newObjects.end());

newObjects.clear();
@@ -48,8 +48,6 @@ void ObjectCollection::ProcessNewObjects()
o->Start();
}

objects.assign(newObjects.begin(), newObjects.end());

objects.insert(objects.end(), newObjects.begin(), newObjects.end());

newObjects.clear();
@@ -31,6 +31,11 @@ void C_NeuralNetwork::SetWindowSize(const sf::Vector2u& windowSize)
this->windowSize = windowSize;
}

const NeuralNetwork& C_NeuralNetwork::Get() const
{
return neuralNetwork;
}

std::vector<float> C_NeuralNetwork::BuildNetworkInput()
{
std::vector<float> networkInput;
@@ -19,6 +19,8 @@ public Component

void SetWindowSize(const sf::Vector2u& windowSize);

const NeuralNetwork& Get() const;

private:
std::vector<float> BuildNetworkInput();

@@ -48,8 +48,6 @@ void ObjectCollection::ProcessNewObjects()
o->Start();
}

objects.assign(newObjects.begin(), newObjects.end());

objects.insert(objects.end(), newObjects.begin(), newObjects.end());

newObjects.clear();
@@ -31,6 +31,11 @@ void C_NeuralNetwork::SetWindowSize(const sf::Vector2u& windowSize)
this->windowSize = windowSize;
}

const NeuralNetwork& C_NeuralNetwork::Get() const
{
return neuralNetwork;
}

std::vector<float> C_NeuralNetwork::BuildNetworkInput()
{
std::vector<float> networkInput;
@@ -19,6 +19,8 @@ public Component

void SetWindowSize(const sf::Vector2u& windowSize);

const NeuralNetwork& Get() const;

private:
std::vector<float> BuildNetworkInput();

@@ -29,6 +29,7 @@ void GeneticAlgorithm::Update(float deltaTime)
std::shared_ptr<UFOGAData> gaData = std::make_shared<UFOGAData>();
gaData->energy = energy;
gaData->neuralNet = neuralNetwork;
gaData->timeAlive = o->GetComponent<C_TimeAlive>();

AddToPool(gaData);
}
@@ -98,5 +99,5 @@ std::shared_ptr<C_NeuralNetwork> GeneticAlgorithm::SpawnUFO()

bool GeneticAlgorithm::PoolSort(std::shared_ptr<UFOGAData> a, std::shared_ptr<UFOGAData> b)
{
return a->energy->Get() > b->energy->Get();
return a->timeAlive->Get() > b->timeAlive->Get();
}
@@ -17,6 +17,7 @@ struct UFOGAData
{
std::shared_ptr<C_NeuralNetwork> neuralNet;
std::shared_ptr<C_Energy> energy;
std::shared_ptr<C_TimeAlive> timeAlive;
};

class GeneticAlgorithm
@@ -37,6 +38,7 @@ class GeneticAlgorithm
WorkingDirectory& workingDir;
ResourceAllocator<sf::Texture>& textureAllocator;
Window& window;
int maxPoolSize;
};

#endif /* GeneticAlgorithm_hpp */
@@ -48,8 +48,6 @@ void ObjectCollection::ProcessNewObjects()
o->Start();
}

objects.assign(newObjects.begin(), newObjects.end());

objects.insert(objects.end(), newObjects.begin(), newObjects.end());

newObjects.clear();
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
@@ -0,0 +1,83 @@
#include "Animation.hpp"


Animation::Animation(FacingDirection direction) : frames(0), currentFrameIndex(0), currentFrameTime(0.f), direction(direction) { }

void Animation::AddFrame(int textureID, int x, int y, int width, int height, float frameTime)
{
FrameData data;
data.id = textureID;
data.x = x;
data.y = y;
data.width = width;
data.height = height;
data.displayTimeSeconds = frameTime;

frames.push_back(data);
}

const FrameData* Animation::GetCurrentFrame() const
{
if(frames.size() > 0)
{
return &frames[currentFrameIndex];
}

return nullptr;
}

bool Animation::UpdateFrame(float deltaTime)
{
if(frames.size() > 0)
{
currentFrameTime += deltaTime;

if(currentFrameTime >= frames[currentFrameIndex].displayTimeSeconds)
{
currentFrameTime = 0.f;
IncrementFrame();
return true;
}
}

return false;
}

void Animation::IncrementFrame()
{
// check if we reached the last frame
if (currentFrameIndex == (frames.size() - 1))
{
currentFrameIndex = 0;
}
else
{
currentFrameIndex++;
}
}

void Animation::Reset()
{
currentFrameIndex = 0;
currentFrameTime = 0.f;
}

void Animation::SetDirection(FacingDirection dir)
{
if(direction != dir)
{
direction = dir;
for(auto& f : frames)
{
f.x += f.width;
f.width *= -1;
}
}
}

FacingDirection Animation::GetDirection() const
{
return direction;
}


@@ -0,0 +1,50 @@
#ifndef Animation_hpp
#define Animation_hpp

#include <vector>

enum class FacingDirection
{
None,
Left,
Right
};

struct FrameData
{
int id;
int x;
int y;
int width;
int height;
float displayTimeSeconds;
};

class Animation
{
public:
Animation(FacingDirection direction);

void AddFrame(int textureID, int x, int y, int width, int height, float frameTime);

const FrameData* GetCurrentFrame() const;

bool UpdateFrame(float deltaTime);

void Reset();

void SetDirection(FacingDirection dir);

FacingDirection GetDirection() const;

private:
void IncrementFrame();

std::vector<FrameData> frames;
int currentFrameIndex;
float currentFrameTime;
FacingDirection direction;
};


#endif /* Animation_hpp */
@@ -0,0 +1,45 @@
#include "Bitmask.hpp"

Bitmask::Bitmask() : bits(0) { }

void Bitmask::SetMask(Bitmask& other)
{
bits = other.GetMask();
}

uint32_t Bitmask::GetMask() const
{
return bits;
}

bool Bitmask::GetBit(int pos) const
{
return (bits & (1 << pos)) != 0;
}

void Bitmask::SetBit(int pos, bool on)
{
if(on)
{
SetBit(pos);
}
else
{
ClearBit(pos);
}
}

void Bitmask::SetBit(int pos)
{
bits = bits | 1 << pos;
}

void Bitmask::ClearBit(int pos)
{
bits = bits & ~(1 << pos);
}

void Bitmask::Clear()
{
bits = 0;
}
@@ -0,0 +1,26 @@
#ifndef Bitmask_hpp
#define Bitmask_hpp

#include <stdint.h>

class Bitmask
{
public:
Bitmask();

void SetMask(Bitmask& other);
uint32_t GetMask() const;

bool GetBit(int pos) const;
void SetBit(int pos, bool on);

void SetBit(int pos);
void ClearBit(int pos);

void Clear();

private:
uint32_t bits;
};

#endif /* Bitmask_hpp */
@@ -0,0 +1,68 @@
#include "C_Animation.hpp"
#include "Object.hpp"

C_Animation::C_Animation(Object* owner) : Component(owner), currentAnimation(AnimationState::None, nullptr)
{

}

void C_Animation::Awake()
{
sprite = owner->GetComponent<C_Sprite>();
}

void C_Animation::Update(float deltaTime)
{
if(currentAnimation.first != AnimationState::None)
{
bool newFrame = currentAnimation.second->UpdateFrame(deltaTime);

if(newFrame)
{
const FrameData* data = currentAnimation.second->GetCurrentFrame();
sprite->Load(data->id);

sprite->SetTextureRect(data->x, data->y, data->width, data->height);
}
}
}

void C_Animation::AddAnimation(AnimationState state, std::shared_ptr<Animation> animation)
{
auto inserted = animations.insert(std::make_pair(state, animation));

if (currentAnimation.first == AnimationState::None)
{
SetAnimationState(state);
}
}

void C_Animation::SetAnimationState(AnimationState state)
{
if (currentAnimation.first == state)
{
return;
}

auto animation = animations.find(state);
if (animation != animations.end())
{
currentAnimation.first = animation->first;
currentAnimation.second = animation->second;

currentAnimation.second->Reset();
}
}

const AnimationState& C_Animation::GetAnimationState() const
{
return currentAnimation.first;
}

void C_Animation::SetAnimationDirection(FacingDirection dir)
{
if(currentAnimation.first != AnimationState::None)
{
currentAnimation.second->SetDirection(dir);
}
}

0 comments on commit 95ce84f

Please sign in to comment.
You can’t perform that action at this time.