Advertisement
MrMusAddict

Rudimentary Factorio

Mar 3rd, 2017
437
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 11.03 KB | None | 0 0
  1. //created by /u/MrMusAddict
  2.  
  3. World w = new World();
  4. int waterSeed = int(random(10000000)); //to-do, create a global seed that feeds into these individual seeds
  5. int sandSeed = int(random(10000000));
  6. int ironSeed = int(random(10000000));
  7. int coalSeed = int(random(10000000));
  8. int copperSeed = int(random(10000000));
  9. int stoneSeed = int(random(10000000));
  10. float waterFrequency = 0.4;
  11. float sandFrequency = 0.35;
  12. float ironFrequency = 0.7;
  13. float coalFrequency = 0.7;
  14. float copperFrequency = 0.7;
  15. float stoneFrequency = 0.7;
  16.  
  17. float tileSize = 20;
  18.  
  19. //setup the progam
  20. void setup() {
  21.   size(600, 600);
  22. }
  23.  
  24. //global draw function
  25. void draw() {
  26.   background(0);
  27.   translate(width/2, height/2);
  28.   w.update();
  29. }
  30.  
  31. //keyboard controls
  32. void keyPressed() {
  33.   if (key == 'w') { //move up
  34.     w.p.up = true;
  35.   }
  36.   if (key == 's') { //move down
  37.     w.p.down = true;
  38.   }
  39.   if (key == 'd') { //move right
  40.     w.p.right = true;
  41.   }
  42.   if (key == 'a') { //move left
  43.     w.p.left = true;
  44.   }
  45.   if (key == 'e') { //open inventory (currently a blank square)
  46.     w.p.showingInv = !w.p.showingInv;
  47.   }
  48. }
  49.  
  50. void keyReleased() { //stop moving
  51.   if (key == 'w') {
  52.     w.p.up = false;
  53.   }
  54.   if (key == 's') {
  55.     w.p.down = false;
  56.   }
  57.   if (key == 'd') {
  58.     w.p.right = false;
  59.   }
  60.   if (key == 'a') {
  61.     w.p.left = false;
  62.   }
  63. }
  64.  
  65. void mousePressed() {
  66.   if (!(w.p.showingInv)) { //don't "mine" ore if the inventory is open
  67.     for (FloorTile t : w.p.visibleTiles) { //for every visible tile
  68.       if (t.pos.x == floor(-w.p.pos.x/tileSize+(mouseX-width/2)/tileSize) &&
  69.         t.pos.y == floor(-w.p.pos.y/tileSize+(mouseY-height/2)/tileSize)) { //if the tile is the one we clicked
  70.         if (!(t.ore == null)) {//if the tile has an ore
  71.           w.p.inv.addItem(t.ore); //add that ore to our inventory
  72.           t.removeOre(); //remove qty 1 ore from the tile
  73.         }
  74.       }
  75.     }
  76.   }
  77. }
  78.  
  79. class FloorTile {
  80.  
  81.   color typeC; //color of grass, water, sand
  82.   color oreC; //color of ore
  83.   PVector pos; //2D vector (x and y position)
  84.   String type; //is this grass, water, or sand?
  85.   String ore; //starts out null, but can be set to something
  86.   int oreCount = 10; //the initial ore count is 10 (this value is stored even if a tile doesn't have ore)
  87.  
  88.   FloorTile(String c_, PVector pos_) { //constructor function. Calls for a string (type), and position vector
  89.     type = c_; //set the type
  90.  
  91.     if (c_ == "Water") {
  92.       typeC = color(28, 107, 160); //set the color to water
  93.     } else if (c_ == "Grass") {
  94.       typeC = color(75, 140, 97); //set the color to grass
  95.     } else if (c_ == "Sand") {
  96.       typeC = color(239, 221, 111); //set the color to sand
  97.     } else {
  98.       typeC = color(0, 0, 0); //Invalid. Set to black
  99.     }
  100.  
  101.     oreC = typeC; //we always show the ore (even if the tile doesn't have ore), so for now "hide" the ore by setting it to the same color as the background
  102.     pos = pos_; //set position
  103.   }
  104.  
  105.   void show(Player p) {//show function
  106.     noStroke(); //no borders on primitives
  107.     fill(typeC); //primitive color is the type color
  108.     //for all intents and purposes, the player is stationary in the middle of the screen.
  109.     //Therefore, translate the world by player position, and translate this specific tile by its XY coordinate
  110.     translate(pos.x*tileSize+p.pos.x, pos.y*tileSize+p.pos.y);
  111.     rect(0, 0, tileSize, tileSize); //draw a rectangle
  112.     fill(oreC); //primitive color is the ore color
  113.     ellipse(tileSize/2, tileSize/2, tileSize/2, tileSize/2); //draw an circle centered on the current tile.
  114.     translate(-pos.x*tileSize-p.pos.x, -pos.y*tileSize-p.pos.y);
  115.   }
  116.  
  117.   void setOre(String s) { //function to set the type or ore (called in world generation).
  118.     ore = s;
  119.  
  120.     if (s == "Iron") {
  121.       oreC = color(180, 180, 180);
  122.     }
  123.     if (s == "Coal") {
  124.       oreC = color(50, 50, 50);
  125.     }
  126.     if (s == "Copper") {
  127.       oreC = color(184, 115, 51);
  128.     }
  129.     if (s == "Stone") {
  130.       oreC = color(112, 88, 75);
  131.     }
  132.   }
  133.  
  134.   void removeOre() { //remove ore function
  135.     if (oreCount > 0) { //only remove ore if we have 1 or more on this tile
  136.       oreCount--; //remove one ore
  137.       if (oreCount == 0) { //if we depleted ore
  138.         oreC = typeC; //change the ore color back to grass / sand
  139.         ore = null; //nullify the ore type
  140.       }
  141.     }
  142.   }
  143. }
  144.  
  145. class Inventory { //Inventory. Very rudamentory.
  146.   int iron = 0;
  147.   int coal = 0;
  148.   int copper = 0;
  149.   int stone = 0;
  150.  
  151.   void addItem(String s) {
  152.     if (s == "Iron") {
  153.       iron++;
  154.       print("\n" + "Iron (" + iron + ")"); //this will print in the processing console
  155.     }
  156.     if (s == "Coal") {
  157.       coal++;
  158.       print("\n" + "Coal (" + coal + ")");
  159.     }
  160.     if (s == "Copper") {
  161.       copper++;
  162.       print("\n" + "Copper (" + copper + ")");
  163.     }
  164.     if (s == "Stone") {
  165.       stone++;
  166.       print("\n" + "Stone (" + stone + ")");
  167.     }
  168.   }
  169.  
  170.   void show(){ //does nothing of note yet, aside from making a rectangle for some planned UI
  171.     fill(180,180,180,220);
  172.     rect(-width/3, -height/3, 2*width/3, 2*height/3);
  173.   }
  174. }
  175.  
  176. class Player {
  177.   //height and width of player
  178.   float wi = 15;
  179.   float hi = 25;
  180.  
  181.   PVector pos = new PVector(0, 0); //position vector
  182.  
  183.   //control booleans
  184.   boolean up = false;
  185.   boolean down = false;
  186.   boolean left = false;
  187.   boolean right = false;
  188.  
  189.   //array list of visible tiles (tiles within the bounds of the window)
  190.   ArrayList<FloorTile> visibleTiles = new ArrayList<FloorTile>();
  191.  
  192.   Inventory inv = new Inventory(); //new inventory
  193.  
  194.   boolean showingInv = false; //start by hiding the inventory
  195.  
  196.   void update() {
  197.     //move the player, pending the boolean movement values
  198.     if (up) {
  199.       pos.y += 2;
  200.     }
  201.     if (down) {
  202.       pos.y -= 2;
  203.     }
  204.     if (right) {
  205.       pos.x -= 2;
  206.     }
  207.     if (left) {
  208.       pos.x += 2;
  209.     }
  210.  
  211.     //draw the player (simple grey rectangle)
  212.     noStroke();
  213.     fill(100, 100, 100);
  214.     rect(-wi/2, -hi/2, wi, hi);
  215.    
  216.     //if we're supposed to show the inventory, show the inventory
  217.     if(showingInv){
  218.      inv.show();
  219.     }
  220.   }
  221.  
  222.   void updateVisibleTiles(ArrayList<FloorTile> tiles) { //get all visible tiles
  223.     visibleTiles.clear(); //clear the visibleTiles array list
  224.     for (FloorTile t : tiles) { //for every tile we've generated thus far
  225.       if(pos.x/tileSize > -t.pos.x-17
  226.       && pos.x/tileSize < -t.pos.x+16
  227.       && pos.y/tileSize > -t.pos.y-17
  228.       && pos.y/tileSize < -t.pos.y+16){ //if this tile is within the window bounds
  229.         visibleTiles.add(t); //add the tile to our visible tiles
  230.       }
  231.     }
  232.   }
  233. }
  234.  
  235. class World {
  236.  
  237.   ArrayList<FloorTile> tiles = new ArrayList<FloorTile>(); //the master array list of ALL tiles
  238.   Player p = new Player();
  239.  
  240.   float resolution = 20.0; //the resolution of noise (higher values = finer detail)
  241.  
  242.   World() {//constructor function
  243.     addTiles(); //add tiles
  244.   }
  245.  
  246.   void update() {//update function
  247.     if (p.visibleTiles.size() < 1089) {//if the size of our visibleTile array list is less than 1089 (33x33)
  248.       addTiles(); //then we've walked into a non-generated part of the world, so add those tiles to the master list.
  249.     }
  250.     p.updateVisibleTiles(tiles); //reconstruct the array list of "visible" tiles (tiles in the window)
  251.     for (FloorTile t : p.visibleTiles) { //show all visible tiles
  252.       t.show(p);
  253.     }
  254.     p.update(); //update the player.
  255.   }
  256.  
  257.   //adding tiles. Each tile has an XY coordinate. The coordinate indicates which row/collumn the tile is in.
  258.   //This means that a coord of 3,5 is 3 tiles over, 5 tiles down (relative to 0,0).
  259.   void addTiles() {
  260.     //from player X position (converted to tile coordinate) +/- 16
  261.     for (int i = int(-p.pos.x/tileSize - 17); i < int(-p.pos.x/tileSize + 16); i++) {
  262.       //from player Y position (converted to tile coordinate) +/- 16
  263.       for (int j = int(-p.pos.y/tileSize - 17); j < int(-p.pos.y/tileSize + 16); j++) {
  264.         boolean found = false; //assume there's no tile there
  265.         for (FloorTile ti : tiles) { //check if a tile with coordinates i,j (x,y) is in the master list
  266.           if (ti.pos.x == i && ti.pos.y == j) {
  267.             found = true; //if so, then a tile is there
  268.           }
  269.         }
  270.         if (!found) { //only generate a new tile if one was not already found at that coordinate
  271.           //set your noise values for each aspect of the tile
  272.           //noise values are between 0 and 1
  273.           //"i" is the x coordinate, "j" is the y coordinate
  274.           //We subtract 10000 from i and j, because these noise functions mirror each quadrant.
  275.           //By that I mean that noise(10,20) == noise(-10,20) == noise(10,-20) == noise(-10,-20).
  276.           //Divide the i & j values by resolution. This "zooms in" on the detail of the noise.
  277.           noiseSeed(waterSeed);
  278.           float waterRan = noise((i-10000)/resolution, (j-10000)/resolution);
  279.           noiseSeed(sandSeed); //
  280.           float sandRan = noise((i-10000)/resolution, (j-10000)/resolution);
  281.           noiseSeed(ironSeed);
  282.           float ironRan = noise((i-10000)/resolution, (j-10000)/resolution);
  283.           noiseSeed(coalSeed);
  284.           float coalRan = noise((i-10000)/resolution, (j-10000)/resolution);
  285.           noiseSeed(copperSeed);
  286.           float copperRan = noise((i-10000)/resolution, (j-10000)/resolution);
  287.           noiseSeed(stoneSeed);
  288.           float stoneRan = noise((i-10000)/resolution, (j-10000)/resolution);
  289.          
  290.  
  291.           if (waterRan < waterFrequency) { //if the seeded random value for water is below the threshhold, make it water.
  292.             tiles.add(new FloorTile("Water", new PVector(i, j)));
  293.           } else if (sandRan < sandFrequency) { //if the seeded random value for sand is below the threshhold, make it sand.
  294.             tiles.add(new FloorTile("Sand", new PVector(i, j)));
  295.           } else { //otherwise, it's grass
  296.             tiles.add(new FloorTile("Grass", new PVector(i, j)));
  297.           }
  298.  
  299.           //we just added the tile, therefore we want to check the last tile in our array list (size()-1)
  300.           if (!(tiles.get(tiles.size()-1).type == "Water")) { //if the tile is NOT water, then check our ores
  301.             //These are not if-else statement, just if statements.
  302.             //Therefore, each ore will overwrite the previous ore data, if it passes the threshhold.
  303.             //Example: all four ores are above their threshhold, but because coal is last it overwrites all previous ore.
  304.             if (stoneRan > stoneFrequency) {
  305.               tiles.get(tiles.size()-1).setOre("Stone"); //set to stone if above the threshhold
  306.             }
  307.             if (copperRan > copperFrequency) {
  308.               tiles.get(tiles.size()-1).setOre("Copper"); //set to copper if above the threshhold
  309.             }
  310.             if (ironRan > ironFrequency) {
  311.               tiles.get(tiles.size()-1).setOre("Iron"); //set to iron if above the threshhold
  312.             }
  313.             if (coalRan > coalFrequency) {
  314.               tiles.get(tiles.size()-1).setOre("Coal"); //set to coal if above the threshhold
  315.             }
  316.           }
  317.         }
  318.       }
  319.     }
  320.   }
  321. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement