SHARE
TWEET

Untitled

a guest Oct 16th, 2015 54 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package base;
  2.  
  3. import java.util.LinkedList;
  4. import java.util.ListIterator;
  5. import java.util.TreeSet;
  6.  
  7. import org.apache.commons.collections4.map.*;
  8. import org.apache.commons.lang3.tuple.Pair;
  9.  
  10. import objects.PatternEntry;
  11. import objects.PatternInstance;
  12. import objects.PatternWrapper;
  13. import rules.ConwayLifeRule;
  14. import rules.Ruleset;
  15.  
  16. import com.badlogic.gdx.ApplicationListener;
  17. import com.badlogic.gdx.Gdx;
  18. import com.badlogic.gdx.graphics.Camera;
  19. import com.badlogic.gdx.graphics.Color;
  20. import com.badlogic.gdx.graphics.GL30;
  21. import com.badlogic.gdx.graphics.OrthographicCamera;
  22. import com.badlogic.gdx.graphics.g2d.SpriteBatch;
  23. import com.badlogic.gdx.utils.viewport.ScreenViewport;
  24. import com.badlogic.gdx.utils.viewport.Viewport;
  25.  
  26. /*
  27.  The LibGDX "main" method
  28.  Copyright (C) 2015 Eugene "eaglgenes101" Wang
  29.  This program is free software; you can redistribute it and/or modify
  30.  it under the terms of the GNU General Public License as published by
  31.  the Free Software Foundation; either version 2 of the License, or
  32.  (at your option) any later version.
  33.  This program is distributed in the hope that it will be useful,
  34.  but WITHOUT ANY WARRANTY; without even the implied warranty of
  35.  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  36.  GNU General Public License for more details.
  37.  You should have received a copy of the GNU General Public License along
  38.  with this program; if not, write to the Free Software Foundation, Inc.,
  39.  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  40.  */
  41.  
  42. public class MyGdxGame implements ApplicationListener
  43. {
  44.  
  45.         LinkedList<PatternInstance> currentPatterns;
  46.  
  47.         ReferenceMap<PatternWrapper, PatternEntry> knownPatterns;
  48.  
  49.         SpriteBatch batch;
  50.  
  51.         int genCounter;
  52.  
  53.         Viewport viewport;
  54.  
  55.         byte[][] GOSPER_GLIDER_CELLS = { {1, 0, 1}, {0, 1, 1}, {0, 1, 0}};
  56.  
  57.         byte[][] R_PENTOMINO = { {0, 1, 1}, {1, 1, 0}, {0, 1, 0}};
  58.  
  59.         byte[][] PENTADECATHON_GRANDPARENT = { {1}, {1}, {1}, {1}, {1}, {1}, {1}, {1}, {1}, {1}};
  60.  
  61.         byte[][] MOVE_PUFFER = { {1, 0}, {0, 1}, {0, 1}, {0, 1}, {0, 1}, {1, 0}};
  62.  
  63.         byte[][] BLOCK_LAYING_SWITCH_ENGINE = { {1, 1, 1, 0, 1}, {1, 0, 0, 0, 0}, {0, 0, 0, 1, 1}, {0, 1, 1, 0, 1},
  64.                         {1, 0, 1, 0, 1}};
  65.  
  66.         byte[][] HIGHLIFE_REPLICATOR = { {1, 1, 1, 0, 0}, {1, 0, 0, 1, 0}, {1, 0, 0, 0, 1}, {0, 1, 0, 0, 1},
  67.                         {0, 0, 1, 1, 1}};
  68.  
  69.         byte[][] GLIDERS_X_THE_DOZEN = { {1, 1, 0, 0, 1}, {1, 0, 0, 0, 1}, {1, 0, 0, 1, 1}};
  70.  
  71.         byte[][] EMPTY_PATTERN = {};
  72.  
  73.         Ruleset actingRule = new ConwayLifeRule();
  74.  
  75.         @Override
  76.         public void create()
  77.         {
  78.                 currentPatterns = new LinkedList<PatternInstance>();
  79.                 knownPatterns = new ReferenceMap<>(AbstractReferenceMap.ReferenceStrength.SOFT,
  80.                                 AbstractReferenceMap.ReferenceStrength.SOFT);
  81.                 currentPatterns.add(new PatternInstance(400, -300, R_PENTOMINO, knownPatterns));
  82.                 batch = new SpriteBatch();
  83.                 //will make the new Viewport when the class is created
  84.                 viewport = new ScreenViewport();
  85.                 genCounter = 0;
  86.         }
  87.  
  88.         @Override
  89.         public void dispose()
  90.         {
  91.                 batch.dispose();
  92.         }
  93.  
  94.         @Override
  95.         public void render()
  96.         {
  97.                 Gdx.gl.glClear(GL30.GL_COLOR_BUFFER_BIT); // clears the buffer
  98.                 batch.begin();
  99.                 LinkedList<PatternInstance> newList = new LinkedList<>();
  100.  
  101.                 for (ListIterator<PatternInstance> iter = currentPatterns.listIterator(); iter.hasNext();)
  102.                 {
  103.                         PatternInstance currentPatternInstance = iter.next();
  104.                         PatternInstance[] successors = currentPatternInstance.step(actingRule, knownPatterns);
  105.                         for (PatternInstance i : successors)
  106.                                 newList.add(i);
  107.  
  108.                         batch.draw(Engine.generatePatternTexture(currentPatternInstance.getEntry(), Color.WHITE),
  109.                                         currentPatternInstance.getX(), -currentPatternInstance.getY()
  110.                                                         - currentPatternInstance.getRectangle().getHeight());
  111.                 }
  112.                 batch.end();
  113.                 currentPatterns = newList; // We can afford to be sloppy, the garbage
  114.                                                                         // collector will do it.
  115.  
  116.                 TreeSet<Pair<PatternInstance, PatternInstance>> collisionList = Engine.findCollisions(newList, 5);
  117.                 currentPatterns = Engine.cleanList(Engine.massMerge(collisionList, newList, knownPatterns, actingRule));
  118.  
  119.                 genCounter++;
  120.                 System.out.println(currentPatterns.size());
  121.  
  122.                 // System.gc();
  123.  
  124.         }
  125.  
  126.         @Override
  127.         public void resize(int width, int height)
  128.         {      
  129.                 //not every time the window is resized
  130.                 viewport.apply(true);
  131.                 viewport.update(width, height, true);
  132.         }
  133.  
  134.         @Override
  135.         public void pause()
  136.         {
  137.         }
  138.  
  139.         @Override
  140.         public void resume()
  141.         {
  142.         }
  143. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Not a member of Pastebin yet?
Sign Up, it unlocks many cool features!
 
Top