Changeset 8edd04e in lost-haven for main/SpawnPoint.java
- Timestamp:
- Jun 7, 2020, 3:04:32 PM (5 years ago)
- Branches:
- master
- Children:
- a49176d
- Parents:
- 155577b
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
main/SpawnPoint.java
r155577b r8edd04e 1 1 package main; 2 2 3 import java.util.Iterator; 4 import java.util.Random; 5 3 6 public class SpawnPoint { 4 Point loc; 5 long lastSpawned; 6 long interval; 7 Creature creature; 8 9 public SpawnPoint(Point loc, long interval, Creature creature) { 10 this.loc = loc; 11 this.interval = interval; 12 this.creature = creature; 13 lastSpawned = 0; 14 } 15 16 //calling class handles the actual spawning of the creature if this method returns true 17 public boolean spawn() { 18 if((System.currentTimeMillis()-lastSpawned) >= interval) { 19 lastSpawned = System.currentTimeMillis(); 20 return true; 21 }else { 22 return false; 23 } 24 } 7 8 static int numSpawnPoints = 0; 9 10 Point loc; 11 long lastSpawned; 12 long interval; 13 Creature creature; 14 int numSpawns; 15 int maxSpawns; 16 int idNum; 17 18 public SpawnPoint(Creature creature, Point loc, long interval, int maxSpawns) { 19 this.creature = creature; 20 this.loc = loc; 21 this.interval = interval; 22 this.maxSpawns = maxSpawns; 23 this.lastSpawned = 0L; 24 this.idNum = ++numSpawnPoints; 25 } 26 27 public Creature spawn() { 28 if (System.currentTimeMillis() - this.lastSpawned >= this.interval && this.numSpawns < this.maxSpawns) { 29 this.lastSpawned = System.currentTimeMillis(); 30 Creature cr = this.creature.copy(); 31 Point newLoc = null; 32 Random gen = new Random(); 33 boolean goodLoc = false, occupied = false; 34 while (!goodLoc) { 35 newLoc = new Point(this.loc.getX() - 400 + gen.nextInt(800), this.loc.getY() - 400 + gen.nextInt(800)); 36 if (newLoc.getX() >= 0 && newLoc.getY() >= 0 && LostHavenRPG.map.getLoc(newLoc.getX() / 100, newLoc.getY() / 100).isPassable() && Point.dist(newLoc, this.loc) <= 400.0D) { 37 occupied = false; 38 int xLow = newLoc.getX() / 100 - 2; 39 int xHigh = xLow + 5; 40 int yLow = newLoc.getY() / 100 - 2; 41 int yHigh = yLow + 5; 42 if (xLow < 0) { 43 xLow = 0; 44 } 45 if (xHigh > LostHavenRPG.map.getLength()) { 46 xHigh = LostHavenRPG.map.getLength(); 47 } 48 if (yLow < 0) { 49 yLow = 0; 50 } 51 if (yHigh > LostHavenRPG.map.getHeight()) { 52 yHigh = LostHavenRPG.map.getHeight(); 53 } 54 for (int x = xLow; x < xHigh; x++) { 55 for (int y = yLow; y < yHigh; y++) { 56 Iterator<Creature> iter = LostHavenRPG.map.getLoc(x, y).getCreatures().iterator(); 57 while (iter.hasNext() && !occupied) { 58 Creature cur = iter.next(); 59 if (Point.dist(cur.getLoc(), newLoc) < 100.0D) { 60 occupied = true; 61 } 62 } 63 } 64 } 65 if (!occupied) { 66 goodLoc = true; 67 } 68 } 69 } 70 cr.setLoc(newLoc); 71 cr.setSpawnPoint(this); 72 this.numSpawns++; 73 return cr; 74 } 75 76 return null; 77 } 78 79 public CreatureType getType() { 80 return this.creature.getType(); 81 } 82 83 public Point getLoc() { 84 return this.loc; 85 } 86 87 public void removeCreature() { 88 this.numSpawns--; 89 } 90 91 public void clear() { 92 this.numSpawns = 0; 93 this.lastSpawned = 0L; 94 } 25 95 }
Note:
See TracChangeset
for help on using the changeset viewer.