Best Unobtainium_ruby code snippet using Unobtainium.has
World.java
Source:World.java
...115 //Using try catch for any errors and reading the csv file116 try (Scanner scanner = new Scanner(new FileReader("assets/objects.csv"))) {117 118 //Using scanner in a while to read the data of the file119 while (scanner.hasNextLine()) {120 String text=scanner.nextLine();121 String[] columns = text.split(",");122 String temp=columns[0];123 124 /*Using a switch case to create and store required buildings, resources and units 125 * in their respective array lists126 */127 switch(temp) {128 case "command_centre":129 buildings.add(new CommandCentre(Integer.parseInt(columns[1]),Integer.parseInt(columns[2])));130 break;131 132 case "engineer":133 units.add(new Engineer(Integer.parseInt(columns[1]),Integer.parseInt(columns[2])));134 break;135 136 case "metal_mine":137 resources.add(new Metal(Integer.parseInt(columns[1]),Integer.parseInt(columns[2])));138 break;139 140 case "unobtainium_mine":141 resources.add(new Unobtainium(Integer.parseInt(columns[1]),Integer.parseInt(columns[2])));142 break;143 144 case "pylon":145 buildings.add(new Pylon(Integer.parseInt(columns[1]),Integer.parseInt(columns[2])));146 break;147 }148 }149 } 150 catch (FileNotFoundException e) {151 // TODO Auto-generated catch block152 e.printStackTrace();153 }154 155 }156 157 /** Checks if a unit is to be selected, returns unit if true and null otherwise158 * @param units, and ArrayList containing all the units in world159 * @param input The Slick object for user inputs.160 */161 public Unit selectUnit(ArrayList<Unit> units,Input input) {162 //Looping through all the units in world163 for (Unit unit:units){164 165 /*Checking if the click is within 'selectDistance' amount of pixels away if true selects the unit,166 * snaps the camera to its position and returns the unit167 */168 if(unit.distanceUnit(input.getAbsoluteMouseX()-camera.getX(),input.getAbsoluteMouseY()-camera.getY())<selectDistance) {169 unit.setisSelected(true);170 worldX=unit.getX();171 worldY=unit.getY();172 camera.snap(this);173 return unit;174 }175 }176 177 //If no unit is to be selected return null178 return null;179 }180 181 /** Checks if a building is to be selected, returns building if true and null otherwise182 * @param buildings, and ArrayList containing all the buildings in world183 * @param input The Slick object for user inputs.184 */185 public Building selectBuilding(ArrayList<Building> buildings,Input input) {186 187 //Looping through all the buildings in world188 for (Building building:buildings){189 190 /*Checking if the click is within 'selectDistance' amount of pixels away if true selects the building,191 * snaps the camera to its position and returns the building192 */193 if(building.distanceBuilding(input.getAbsoluteMouseX()-camera.getX(),input.getAbsoluteMouseY()-camera.getY())<selectDistance) {194 building.isSelected=true;195 worldX=building.getX();196 worldY=building.getY();197 camera.snap(this);198 return building;199 }200 }201 202 //If no building is to be selected return null203 return null;204 }205 206 /** Finds the closest CommandCentre to a unit207 * @param unit an object of class Unit208 * @param buildings, and ArrayList containing all the buildings in world209 */210 public Building closestCmdCent(Unit unit,ArrayList<Building> buildings) {211 Building temp=null;212 float mindist= Float.MAX_VALUE;213 214 //Loops through all buildings in world215 for(Building building:buildings) {216 217 //Checks whether building is a CommandCentre218 if (building instanceof CommandCentre) 219 {220 //Checks if the CommandCentre is the closest one or not221 if(building.distanceBuilding(unit.getX(),unit.getY())<mindist) {222 temp=building;223 mindist=(float) building.distanceBuilding(unit.getX(),unit.getY());224 }225 }226 }227 228 //Returns closest CommandCentre229 return temp;230 }231 232 233 /** Update the game state for a frame.234 * @param input The Slick object for user inputs.235 * @param delta Time passed since last frame (milliseconds).236 * @throws SlickException 237 */238 239 public void update(Input input, int delta) throws SlickException {240 241 //Movement of the camera using keys WASD242 if(input.isKeyDown(Input.KEY_W)||input.isKeyDown(Input.KEY_S)||input.isKeyDown(Input.KEY_A)||input.isKeyDown(Input.KEY_D)) {243 camera.KeyMove(input, this,delta);244 unitMoving=false;245 camera.isOffset=true;246 }247 248 //Check for if a unit or building is to be selected249 if(input.isMousePressed(Input.MOUSE_LEFT_BUTTON)) {250 251 //First deselect any previously selected Unit if selected252 if(selectedUnit!=null) {253 selectedUnit.setisSelected(false);254 if(selectedUnit.distanceUnit(selectedUnit.getDestX(), selectedUnit.getDestY())>selectDistance) {255 unitMoving=true;256 }257 }258 259 //Then deselect any previously selected building if selected260 if(selectedBuilding!=null) {261 selectedBuilding.isSelected=false;262 }263 264 //Calling selectedUnits and then selectBuilding if no unit is selected265 selectedUnit=selectUnit(units,input);266 if(selectedUnit==null) {267 selectedBuilding=selectBuilding(buildings,input);268 }269 else{270 selectedBuilding=null;271 }272 273 }274 275 //Checks for when a unit is selected276 if(selectedUnit!=null) {277 278 //Checks for when mouse right clicks while a unit is selected279 if(input.isMousePressed(Input.MOUSE_RIGHT_BUTTON)) {280 281 //Discontinuing mining282 if(selectedUnit instanceof Engineer) {283 ((Engineer) selectedUnit).isMining=false;284 ((Engineer) selectedUnit).pastTime=0;285 }286 287 //Setting destination of unit to mouse coordinates unless a builder is building288 if(!(selectedUnit instanceof Builder && ((Builder )selectedUnit).isCreating)) {289 selectedUnit.setdestX(input.getAbsoluteMouseX()-camera.getX());290 selectedUnit.setdestY(input.getAbsoluteMouseY()-camera.getY());291 unitMoving=true;292 }293 }294 295 //Checks for when a unit is moving and is selected, adjusts camera accordingly296 if(unitMoving==true) {297 if (camera.isOffset==false) {298 camera.UnitMove(selectedUnit,this);299 }300 }301 }302 303 304 //Calling Unit.update to update all units in world305 Unit.update(this, delta,input,map);306 307 //Check to see if any truck has build a CommandCentre and needs to be destroyed308 if(truckdestroyed!=null) {309 units.remove(truckdestroyed);310 units.trimToSize();311 truckdestroyed=null;312 }313 //Calling Building.update to update all buildings in world314 Building.update(this,input,delta);315 }316 317 /** Render the entire screen, so it reflects the current game state.318 * @param g The Slick graphics object, used for drawing.319 * @throws SlickException 320 */321 ...
Engineer.java
Source:Engineer.java
1package shadowBuild;2import java.util.ArrayList;3import org.newdawn.slick.Graphics;4import org.newdawn.slick.Image;5import org.newdawn.slick.Input;6import org.newdawn.slick.SlickException;7/** This class contains all the engineer objects and their interactions with other classes8 */9public class Engineer extends Unit{10 11 private static final double SPEED = 0.1;12 private static final String ENGINEER_PATH = "assets/units/engineer.png";13 private static final int NEAR_RESOURCE = 32; 14 private static final int TIME_NEEDED = 5000;15 16 private static final int INITIAL_CAPACITY = 2;17 private Image image = new Image(ENGINEER_PATH);18 public Engineer(double x, double y) throws SlickException {19 super(x, y);20 }21 22 /** Find the closest command centre23 * @param buildingList A list of all the buildings on map.24 * @return The closest command centre25 */26 public Building findClosestCommandCentre(ArrayList<Building>buildingList) {27 double distance = -1;28 Building buildingTarget = null;29 for(Building building: buildingList) {30 if(building instanceof CommandCentre) {31 if(distance == -1) {32 distance = World.distance(this.getX(), this.getY(), building.getX(), building.getY());33 buildingTarget = building;34 }else {35 if(distance > World.distance(this.getX(), this.getY(), building.getX(), building.getY())) {36 distance = World.distance(this.getX(), this.getY(), building.getX(), building.getY());37 buildingTarget = building;38 } 39 }40 41 }42 }43 return buildingTarget;44 }45 46 private int nearResourceTime = 0;47 private boolean autoMoving = false;48 49 public boolean getAutoMoving() {50 return autoMoving;51 }52 public void setAutoMoving(boolean state) {53 this.autoMoving = state;54 }55 private double fromX = -1;56 private double fromY = -1;57 58 private boolean isMining = false;59 private boolean clicked = false;60 private boolean isMoving = false;61 62 /** Get the mining state of the engineer63 * @return Return true if the engineer is mining else false64 */65 private boolean getMiningState() {66 return isMining;67 }68 69 /** Set the mining state of the engineer70 * @param state Another mining state71 */72 private void setMiningState(boolean state) {73 this.isMining = state;74 }75 /** Get the state of right mouse76 * @return Return true if mouse right is clicked else false77 */78 private boolean getClickState() {79 return clicked;80 }81 /** Set the state of the right mouse82 * @return Return true if mouse right is clickes else false83 */84 private void setClickState(boolean state) {85 this.clicked = state;86 }87 88 private boolean minedMetal = false;89 90 public boolean getMinedMetal() {91 return minedMetal;92 }93 94 public void setMinedMetal(boolean state) {95 this.minedMetal = state;96 }97 98 /** Get the moving state of the engineer99 * @return Return true if the engineer is moving else false100 */101 public boolean getMovingState() {102 return isMoving;103 }104 public void setMovingState(boolean state) {105 this.isMoving = state;106 }107 108 private boolean backToResource = false;109 110 111 private double targetX = -1;112 private double targetY = -1;113 114 115 /** Update the engineer's mining process116 * @param delta Time passed since last frame (milliseconds).117 * @param resourceList A list of all the resources on map118 * @param buildingList A list of all the buildings on map.119 */120 public void mining(ArrayList<Resource>resourceList, ArrayList<Building>buildingList, int delta) {121 for(Resource resource:resourceList) {122 if(World.distance(this.getX(), this.getY(), resource.getX(), resource.getY()) <= NEAR_RESOURCE) {123 if(resource instanceof Metal) {124 setMinedMetal(true);125 }126 if(resource instanceof Unobtainium){127 setMinedMetal(false);128 }129 nearResourceTime += delta;130 if(nearResourceTime >= TIME_NEEDED) {131 targetX = findClosestCommandCentre(buildingList).getX();132 targetY = findClosestCommandCentre(buildingList).getY();133 fromX = getX();134 fromY = getY();135 setMovingState(true);136 nearResourceTime = 0;137 setMiningState(true);138 }139 140 }141 }142 }143 144 /** Update the movement of the engineer145 * @param resourceList A list of all the resources on map146 * @param buildingList A list of all the buildings on map.147 * @param unitList A list of all the units on map148 * @param resourceInfo The info of the amount of all the resources149 * @param world Game world150 */151 public void engineerMove(World world, ArrayList<Resource>resourceList, ArrayList<Building>buildingList, ArrayList<Unit>unitList, ResourceHelper resourceInfo) {152 153 // get the capacity of the engineer154 int capacity = 2 + world.getActivatedPylonCount();155 156 157 if (World.distance(getX(), getY(), targetX, targetY) <= getSpeed()) {158 if(getClickState()) {159 setClickState(false);160 setMovingState(false); 161 }162 163 if(getMiningState()) {164 if(minedMetal) {165 if(resourceInfo.mapHasEnoughMetal(resourceInfo.getOwnedMetal() + capacity)) {166 resourceInfo.setOwnedMetal(resourceInfo.getOwnedMetal() + capacity);167 }else {168 resourceInfo.setOwnedMetal(resourceInfo.getTotalMetal());169 }170 171 }else {172 if(resourceInfo.mapHasEnoughUnobtainium(resourceInfo.getOwnedUnobtainium() + capacity)) {173 resourceInfo.setOwnedUnobtainium(resourceInfo.getOwnedUnobtainium() + capacity);174 }else {175 resourceInfo.setOwnedUnobtainium(resourceInfo.getTotalUnobtainium());176 }177 178 }179 180 targetX = fromX;181 targetY = fromY;182 setMiningState(false);183 backToResource = true;184 185 }186 187 if(backToResource) {188 mining(resourceList, buildingList, world.getDelta());189 190 }191 192 } else {193 194 // Calculate the appropriate x and y distances195 if((targetX != -1) && (targetY != -1)) {196 setMovingState(true);197 double theta = Math.atan2(targetY - getY(), targetX - getX());198 199 double dx = (double)Math.cos(theta) * world.getDelta() * getSpeed();200 201 double dy = (double)Math.sin(theta) * world.getDelta() * getSpeed();202 // Check the tile is free before moving; otherwise, we stop moving203 if (!(world.isPositionSolid(getX() + dx, getY() + dy))) {204 setX(getX()+dx);205 setY(getY()+dy);206 } else {207 //resetTarget();208 }209 210 }211 212 }213 214 }215 216 /** If mouse is clicked somewhere else, interrupt the mining process and move to the target217 * @param input Input from the system218 * @param delta Time passed since last frame (milliseconds).219 * @param resourceList A list of all the resources on map220 * @param buildingList A list of all the buildings on map.221 * @param unitList A list of all the units on map222 * @param resourceInfo The info of the amount of all the resources223 * @param world Game world224 */225 @Override226 public void doWork(Input input, int delta, ArrayList<Resource> resourceList, ArrayList<Building> buildingList,ArrayList<Unit> unitList,227 ResourceHelper resourceInfo, World world) throws SlickException {228 229 Camera camera = world.getCamera();230 231 // If the mouse button is being clicked, set the target to the cursor location232 if (input.isMousePressed(Input.MOUSE_RIGHT_BUTTON)) {233 234 targetX = camera.screenXToGlobalX(input.getMouseX());235 targetY = camera.screenYToGlobalY(input.getMouseY());236 237 setClickState(true);238 setMiningState(false);239 nearResourceTime = 0;240 backToResource = false;241 }242 243 // if it reaches a resource, then start mining244 if(!getClickState()) {245 mining(resourceList, buildingList, delta);246 }247 }248 249 250 public double getSpeed() {251 return SPEED;252 }253 254 public Image getImage() {255 return image;256 }257 258 259 public void setImage(Image image) {260 this.image = image;261 }262 263 @Override264 public void drawCommandText(Graphics g) { 265 }266 267}...
ResourceHelper.java
Source:ResourceHelper.java
...35 this.ownedUnobtainium = amount;36 }37 38 /* Use resources */39 public boolean hasEnoughMetal(int amount) {40 if((this.ownedMetal - amount) < 0) {41 return false;42 }43 return true; 44 }45 46 public void useMetal(int amount) {47 if(hasEnoughMetal(amount)) {48 this.ownedMetal = this.ownedMetal - amount;49 }50 }51 52 public boolean hasEnoughUnobtainium(int amount) {53 if((this.ownedUnobtainium - amount) < 0) {54 return false;55 }56 return true; 57 }58 59 public void useUnobtainium(int amount) {60 if(hasEnoughUnobtainium(amount)) {61 this.ownedUnobtainium = this.ownedUnobtainium - amount;62 }63 }64 65 public boolean mapHasEnoughMetal(int minedMetal) {66 if(minedMetal <= METAL_ON_MAP) {67 return true;68 }69 return false;70 }71 72 public boolean mapHasEnoughUnobtainium(int minedUnobtainium) {73 if(minedUnobtainium <= UNOBTAINIUM_ON_MAP) {74 return true;...
has
Using AI Code Generation
1u.add('a')2u.add('b')3u.add('c')4u.add('d')5u.add('e')6u.add('f')7u.has('c')8u.has('g')9u.add('a')10u.add('b')11u.add('c')12u.add('d')13u.add('e')14u.add('f')15u.has('c')16u.has('g')17u.add('a')18u.add('b')19u.add('c')20u.add('d')21u.add('e')22u.add('f')23u.has('c')24u.has('g')25u.add('a')26u.add('b')27u.add('c')28u.add('d')29u.add('e')30u.add('f')31u.has('c')32u.has('g')33u.add('a')
has
Using AI Code Generation
1u.add("a")2puts u.has("a")3puts u.has("b")4u.add("a")5puts u.has("a")6puts u.has("b")7u.add("a")8puts u.has("a")9puts u.has("b")10u.add("a")11puts u.has("a")12puts u.has("b")13u.add("a")14puts u.has("a")15puts u.has("b")16u.add("a")17puts u.has("a")18puts u.has("b")19u.add("a")20puts u.has("a")21puts u.has("b")22u.add("a")23puts u.has("a")24puts u.has("b")
has
Using AI Code Generation
1if hash.has?(:foo)2if hash.has?(:bar)3if hash.has?(:baz)4if hash.has?(:qux)5puts hash[:foo] if hash.has?(:foo)6puts hash[:bar] if hash.has?(:bar)7puts hash[:baz] if hash.has?(:baz)8puts hash[:qux] if hash.has?(:qux)
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!