Guest User

Untitled

a guest
May 21st, 2015
227
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.33 KB | None | 0 0
  1. //
  2. //  Storage.h
  3. //  Fission
  4. //
  5. //  Created by C0deH4cker on 2/28/14.
  6. //  Copyright (c) 2014 C0deH4cker. All rights reserved.
  7. //
  8.  
  9. #ifndef _FSN_STORAGE_H_
  10. #define _FSN_STORAGE_H_
  11.  
  12. #include "Component.h"
  13. #include <stack>
  14. #include <queue>
  15. #include "macros.h"
  16. #include "Grid.h"
  17. #include "Atom.h"
  18.  
  19. namespace fsn {
  20.     struct StoredData {
  21.         int mass;
  22.         int energy;
  23.     };
  24.    
  25.     template <class T>
  26.     class Storage: public virtual Component {
  27.     public:
  28.         static const StoredData& getTop(std::stack<StoredData> stack) {
  29.             return stack.top();
  30.         }
  31.        
  32.         static const StoredData& getTop(std::queue<StoredData> queue) {
  33.             return queue.front();
  34.         }
  35.        
  36.         Storage(char type)
  37.         : Component(type) { }
  38.        
  39.         virtual bool onHit(Atom& atom) {
  40.             bool destroy = false;
  41.            
  42.             if(atom.energy >= 0) {
  43.                 // Push
  44.                 stored.push({atom.mass, atom.energy});
  45.                 destroy = true;
  46.             }
  47.             else if(stored.empty()) {
  48.                 // Pop, but empty, so reflect
  49.                 atom.dir ^= 2;
  50.                 atom.energy = -atom.energy;
  51.             }
  52.             else {
  53.                 // Pop
  54.                 const StoredData& top = getTop(stored);
  55.                 atom.mass = top.mass;
  56.                 atom.energy = top.energy;
  57.                 stored.pop();
  58.             }
  59.            
  60.             return destroy;
  61.         }
  62.        
  63.     private:
  64.         T stored;
  65.     };
  66.    
  67.     typedef Storage<std::stack<StoredData>> Stack;
  68.     typedef Storage<std::queue<StoredData>> Queue;
  69. }
  70.  
  71.  
  72. #endif /* _FSN_STACK_H_ */
Advertisement
Add Comment
Please, Sign In to add comment