Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //created by /u/MrMusAddict
- World w = new World();
- int waterSeed = int(random(10000000)); //to-do, create a global seed that feeds into these individual seeds
- int sandSeed = int(random(10000000));
- int ironSeed = int(random(10000000));
- int coalSeed = int(random(10000000));
- int copperSeed = int(random(10000000));
- int stoneSeed = int(random(10000000));
- float waterFrequency = 0.4;
- float sandFrequency = 0.35;
- float ironFrequency = 0.7;
- float coalFrequency = 0.7;
- float copperFrequency = 0.7;
- float stoneFrequency = 0.7;
- float tileSize = 20;
- //setup the progam
- void setup() {
- size(600, 600);
- }
- //global draw function
- void draw() {
- background(0);
- translate(width/2, height/2);
- w.update();
- }
- //keyboard controls
- void keyPressed() {
- if (key == 'w') { //move up
- w.p.up = true;
- }
- if (key == 's') { //move down
- w.p.down = true;
- }
- if (key == 'd') { //move right
- w.p.right = true;
- }
- if (key == 'a') { //move left
- w.p.left = true;
- }
- if (key == 'e') { //open inventory (currently a blank square)
- w.p.showingInv = !w.p.showingInv;
- }
- }
- void keyReleased() { //stop moving
- if (key == 'w') {
- w.p.up = false;
- }
- if (key == 's') {
- w.p.down = false;
- }
- if (key == 'd') {
- w.p.right = false;
- }
- if (key == 'a') {
- w.p.left = false;
- }
- }
- void mousePressed() {
- if (!(w.p.showingInv)) { //don't "mine" ore if the inventory is open
- for (FloorTile t : w.p.visibleTiles) { //for every visible tile
- if (t.pos.x == floor(-w.p.pos.x/tileSize+(mouseX-width/2)/tileSize) &&
- t.pos.y == floor(-w.p.pos.y/tileSize+(mouseY-height/2)/tileSize)) { //if the tile is the one we clicked
- if (!(t.ore == null)) {//if the tile has an ore
- w.p.inv.addItem(t.ore); //add that ore to our inventory
- t.removeOre(); //remove qty 1 ore from the tile
- }
- }
- }
- }
- }
- class FloorTile {
- color typeC; //color of grass, water, sand
- color oreC; //color of ore
- PVector pos; //2D vector (x and y position)
- String type; //is this grass, water, or sand?
- String ore; //starts out null, but can be set to something
- int oreCount = 10; //the initial ore count is 10 (this value is stored even if a tile doesn't have ore)
- FloorTile(String c_, PVector pos_) { //constructor function. Calls for a string (type), and position vector
- type = c_; //set the type
- if (c_ == "Water") {
- typeC = color(28, 107, 160); //set the color to water
- } else if (c_ == "Grass") {
- typeC = color(75, 140, 97); //set the color to grass
- } else if (c_ == "Sand") {
- typeC = color(239, 221, 111); //set the color to sand
- } else {
- typeC = color(0, 0, 0); //Invalid. Set to black
- }
- 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
- pos = pos_; //set position
- }
- void show(Player p) {//show function
- noStroke(); //no borders on primitives
- fill(typeC); //primitive color is the type color
- //for all intents and purposes, the player is stationary in the middle of the screen.
- //Therefore, translate the world by player position, and translate this specific tile by its XY coordinate
- translate(pos.x*tileSize+p.pos.x, pos.y*tileSize+p.pos.y);
- rect(0, 0, tileSize, tileSize); //draw a rectangle
- fill(oreC); //primitive color is the ore color
- ellipse(tileSize/2, tileSize/2, tileSize/2, tileSize/2); //draw an circle centered on the current tile.
- translate(-pos.x*tileSize-p.pos.x, -pos.y*tileSize-p.pos.y);
- }
- void setOre(String s) { //function to set the type or ore (called in world generation).
- ore = s;
- if (s == "Iron") {
- oreC = color(180, 180, 180);
- }
- if (s == "Coal") {
- oreC = color(50, 50, 50);
- }
- if (s == "Copper") {
- oreC = color(184, 115, 51);
- }
- if (s == "Stone") {
- oreC = color(112, 88, 75);
- }
- }
- void removeOre() { //remove ore function
- if (oreCount > 0) { //only remove ore if we have 1 or more on this tile
- oreCount--; //remove one ore
- if (oreCount == 0) { //if we depleted ore
- oreC = typeC; //change the ore color back to grass / sand
- ore = null; //nullify the ore type
- }
- }
- }
- }
- class Inventory { //Inventory. Very rudamentory.
- int iron = 0;
- int coal = 0;
- int copper = 0;
- int stone = 0;
- void addItem(String s) {
- if (s == "Iron") {
- iron++;
- print("\n" + "Iron (" + iron + ")"); //this will print in the processing console
- }
- if (s == "Coal") {
- coal++;
- print("\n" + "Coal (" + coal + ")");
- }
- if (s == "Copper") {
- copper++;
- print("\n" + "Copper (" + copper + ")");
- }
- if (s == "Stone") {
- stone++;
- print("\n" + "Stone (" + stone + ")");
- }
- }
- void show(){ //does nothing of note yet, aside from making a rectangle for some planned UI
- fill(180,180,180,220);
- rect(-width/3, -height/3, 2*width/3, 2*height/3);
- }
- }
- class Player {
- //height and width of player
- float wi = 15;
- float hi = 25;
- PVector pos = new PVector(0, 0); //position vector
- //control booleans
- boolean up = false;
- boolean down = false;
- boolean left = false;
- boolean right = false;
- //array list of visible tiles (tiles within the bounds of the window)
- ArrayList<FloorTile> visibleTiles = new ArrayList<FloorTile>();
- Inventory inv = new Inventory(); //new inventory
- boolean showingInv = false; //start by hiding the inventory
- void update() {
- //move the player, pending the boolean movement values
- if (up) {
- pos.y += 2;
- }
- if (down) {
- pos.y -= 2;
- }
- if (right) {
- pos.x -= 2;
- }
- if (left) {
- pos.x += 2;
- }
- //draw the player (simple grey rectangle)
- noStroke();
- fill(100, 100, 100);
- rect(-wi/2, -hi/2, wi, hi);
- //if we're supposed to show the inventory, show the inventory
- if(showingInv){
- inv.show();
- }
- }
- void updateVisibleTiles(ArrayList<FloorTile> tiles) { //get all visible tiles
- visibleTiles.clear(); //clear the visibleTiles array list
- for (FloorTile t : tiles) { //for every tile we've generated thus far
- if(pos.x/tileSize > -t.pos.x-17
- && pos.x/tileSize < -t.pos.x+16
- && pos.y/tileSize > -t.pos.y-17
- && pos.y/tileSize < -t.pos.y+16){ //if this tile is within the window bounds
- visibleTiles.add(t); //add the tile to our visible tiles
- }
- }
- }
- }
- class World {
- ArrayList<FloorTile> tiles = new ArrayList<FloorTile>(); //the master array list of ALL tiles
- Player p = new Player();
- float resolution = 20.0; //the resolution of noise (higher values = finer detail)
- World() {//constructor function
- addTiles(); //add tiles
- }
- void update() {//update function
- if (p.visibleTiles.size() < 1089) {//if the size of our visibleTile array list is less than 1089 (33x33)
- addTiles(); //then we've walked into a non-generated part of the world, so add those tiles to the master list.
- }
- p.updateVisibleTiles(tiles); //reconstruct the array list of "visible" tiles (tiles in the window)
- for (FloorTile t : p.visibleTiles) { //show all visible tiles
- t.show(p);
- }
- p.update(); //update the player.
- }
- //adding tiles. Each tile has an XY coordinate. The coordinate indicates which row/collumn the tile is in.
- //This means that a coord of 3,5 is 3 tiles over, 5 tiles down (relative to 0,0).
- void addTiles() {
- //from player X position (converted to tile coordinate) +/- 16
- for (int i = int(-p.pos.x/tileSize - 17); i < int(-p.pos.x/tileSize + 16); i++) {
- //from player Y position (converted to tile coordinate) +/- 16
- for (int j = int(-p.pos.y/tileSize - 17); j < int(-p.pos.y/tileSize + 16); j++) {
- boolean found = false; //assume there's no tile there
- for (FloorTile ti : tiles) { //check if a tile with coordinates i,j (x,y) is in the master list
- if (ti.pos.x == i && ti.pos.y == j) {
- found = true; //if so, then a tile is there
- }
- }
- if (!found) { //only generate a new tile if one was not already found at that coordinate
- //set your noise values for each aspect of the tile
- //noise values are between 0 and 1
- //"i" is the x coordinate, "j" is the y coordinate
- //We subtract 10000 from i and j, because these noise functions mirror each quadrant.
- //By that I mean that noise(10,20) == noise(-10,20) == noise(10,-20) == noise(-10,-20).
- //Divide the i & j values by resolution. This "zooms in" on the detail of the noise.
- noiseSeed(waterSeed);
- float waterRan = noise((i-10000)/resolution, (j-10000)/resolution);
- noiseSeed(sandSeed); //
- float sandRan = noise((i-10000)/resolution, (j-10000)/resolution);
- noiseSeed(ironSeed);
- float ironRan = noise((i-10000)/resolution, (j-10000)/resolution);
- noiseSeed(coalSeed);
- float coalRan = noise((i-10000)/resolution, (j-10000)/resolution);
- noiseSeed(copperSeed);
- float copperRan = noise((i-10000)/resolution, (j-10000)/resolution);
- noiseSeed(stoneSeed);
- float stoneRan = noise((i-10000)/resolution, (j-10000)/resolution);
- if (waterRan < waterFrequency) { //if the seeded random value for water is below the threshhold, make it water.
- tiles.add(new FloorTile("Water", new PVector(i, j)));
- } else if (sandRan < sandFrequency) { //if the seeded random value for sand is below the threshhold, make it sand.
- tiles.add(new FloorTile("Sand", new PVector(i, j)));
- } else { //otherwise, it's grass
- tiles.add(new FloorTile("Grass", new PVector(i, j)));
- }
- //we just added the tile, therefore we want to check the last tile in our array list (size()-1)
- if (!(tiles.get(tiles.size()-1).type == "Water")) { //if the tile is NOT water, then check our ores
- //These are not if-else statement, just if statements.
- //Therefore, each ore will overwrite the previous ore data, if it passes the threshhold.
- //Example: all four ores are above their threshhold, but because coal is last it overwrites all previous ore.
- if (stoneRan > stoneFrequency) {
- tiles.get(tiles.size()-1).setOre("Stone"); //set to stone if above the threshhold
- }
- if (copperRan > copperFrequency) {
- tiles.get(tiles.size()-1).setOre("Copper"); //set to copper if above the threshhold
- }
- if (ironRan > ironFrequency) {
- tiles.get(tiles.size()-1).setOre("Iron"); //set to iron if above the threshhold
- }
- if (coalRan > coalFrequency) {
- tiles.get(tiles.size()-1).setOre("Coal"); //set to coal if above the threshhold
- }
- }
- }
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement