// WARNING: This file is auto-generated and any changes to it will be overwritten import lang.stride.*; import java.util.*; import greenfoot.*; import javax.swing.*; /** * */ public class Item extends Thing { protected int value; protected Prinzessin prinzessin; /** * */ public Item() { super(2); this.value = (Greenfoot.getRandomNumber(10) + 1) * 100; } /** * */ protected void addedToWorld(World world) { world.addObject( new Textbox(this.getClass().getSimpleName() + ": " + this.value + "$", new Font("Rockwell", true, false, 14), new Color(230, 250, 230), 200, 50), this.getX(), this.getY() + 50); } /** * Act - do whatever the Item wants to do. This method is called whenever the 'Act' or 'Run' button gets pressed in the environment. */ public void act() { if (Greenfoot.mouseClicked(this)) { Prinzessin prinzessin = (Prinzessin)this.getWorld().getObjects(Prinzessin.class).get(0); if (this.value > prinzessin.getCredit()) { /* not enought credits to buy item*/ String text = "Zu wenig Geld (" + prinzessin.getCredit() + "$)\nBenötigt: " + this.value + "$"; String title = "Problem!"; Message.showDialog(text, title); } else { prinzessin.setCredit(prinzessin.getCredit() - this.value); prinzessin.addToInventory(this); String inventoryItems = ""; for (final Item item : prinzessin.getInventory()) { inventoryItems = "\n* " + inventoryItems + item.getClass().getSimpleName() + " *"; } Message.showDialog("Gekauft: " + this.getClass().getSimpleName() + " für " + this.value + "$\nSie besitzen:" + inventoryItems + "\nSie haben jetzt noch " + prinzessin.getCredit() + "\nGehen Sie in Ihr Haus, um es einzurichten", "Erfolgreicher Kauf!"); this.getWorld().removeObject(this); } } } }