public void run() { init(); while (true) { System.out.print( String.format( "%-20s %-40s", tamagotchi.getLifeState(), tamagotchi.getPhysicalState().getDescription())); System.out.print( String.format( " %30s", "(" + tamagotchi.getHungerIndex() + " , " + tamagotchi.getAge() + " , " + currentDay + ":" + currentHour + ") ? ")); userInput(); } }
@Override protected void onResume() { super.onResume(); if (!readFromSavedInstance) { Log.d(TAMAGOTCHI, "Loading da tama"); loadTamagotchi(); initializePlayingfield(); } if (tama != null) tama.UpdateText(); }
private void userInput() { Scanner inputReader = new Scanner(System.in); String input = inputReader.next().toLowerCase(); if (input.equals("s")) { step(Config.SIMULATION_STEP_HOUR); } else if (input.equals("f")) { tamagotchi.feed(10); } else if (input.equals("b")) { tamagotchi.putToBed(); } else if (input.equals("x")) { shutDown(); } else { System.out.println("Invalid input"); } }
// Step through simulation. // It simulates passage of time. Changes internal state. private void step(Integer stepHours) { LifeState lifeState = tamagotchi.getLifeState(); PhysicalState physicalState = tamagotchi.getPhysicalState(); Integer age = tamagotchi.getAge(); Integer hungerIndex = tamagotchi.getHungerIndex(); currentHour += stepHours; if (currentHour > 24) { currentDay = +1; currentHour = currentHour % 24; age = +1; } // Losses energy and get aged as time passes. tamagotchi.loseHealth(stepHours); tamagotchi.setAge(age); lifeState.action(tamagotchi); physicalState.action(tamagotchi, currentHour); }
/** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); this.buttons = new ArrayList<Button>(); setContentView(R.layout.main); SharedPreferences settings = this.getSharedPreferences(PREFNAME, 0); if (settings.contains("tama_name")) { tama = new Tamagotchi(""); loadTamagotchi(); tama.UpdateText(); } button = (Button) findViewById(R.id.MyButton); output = (TextView) findViewById(R.id.OutputTextView); sua = new ShowUpdateAndroid(output, this); boolean isButtonEnabled = true; if (savedInstanceState != null) { // There is a state to restore output.setText(savedInstanceState.getString("outtext")); if (savedInstanceState.containsKey("tama_name")) { isButtonEnabled = false; } tama.restoreFromBundle(savedInstanceState); tama.setShowUpdater(sua); readFromSavedInstance = true; } else { loadTamagotchi(); if (tama != null) tama.UpdateText(); } button.setOnClickListener( new View.OnClickListener() { @Override public void onClick(View v) { tama = new Tamagotchi("Tama-chan"); tama.setShowUpdater(sua); initializePlayingfield(); } }); Button b1 = ((Button) findViewById(R.id.EatAppleButton)); b1.setOnClickListener( new View.OnClickListener() { @Override public void onClick(View v) { tama.EatFood(Tamagotchi.FoodItem.Apple); } }); Button b2 = ((Button) findViewById(R.id.EatCandyButton)); b2.setOnClickListener( new View.OnClickListener() { @Override public void onClick(View v) { tama.EatFood(Tamagotchi.FoodItem.Candy); } }); Button b3 = ((Button) findViewById(R.id.EatMeatButton)); b3.setOnClickListener( new View.OnClickListener() { @Override public void onClick(View v) { tama.EatFood(Tamagotchi.FoodItem.Meat); } }); Button b4 = ((Button) findViewById(R.id.PoopButton)); b4.setOnClickListener( new View.OnClickListener() { @Override public void onClick(View v) { tama.RemovePoop(); } }); Button b5 = ((Button) findViewById(R.id.PlayButton)); b5.setOnClickListener( new View.OnClickListener() { @Override public void onClick(View v) { tama.PlayGame(Tamagotchi.Game.Simon); } }); Button b6 = ((Button) findViewById(R.id.MedicineButton)); b6.setOnClickListener( new View.OnClickListener() { @Override public void onClick(View v) { tama.GiveMedicine(); } }); buttons.add(b1); buttons.add(b2); buttons.add(b3); buttons.add(b4); buttons.add(b5); buttons.add(b6); for (Button b : buttons) b.setEnabled(!isButtonEnabled); }
private void loadTamagotchi() { SharedPreferences settings = this.getSharedPreferences(PREFNAME, 0); if (tama != null) tama.loadFromSharedPrefs(settings); }
private void saveTamagotchi() { SharedPreferences settings = this.getSharedPreferences(PREFNAME, 0); if (tama != null) tama.saveToSharedPrefs(settings); }
@Override public void onSaveInstanceState(Bundle outState) { super.onSaveInstanceState(outState); if (tama != null) tama.updateBundle(outState); outState.putString("outtext", output.getText().toString()); }
private void initializePlayingfield() { tama.setShowUpdater(sua); if (pi == null) setupAlarm(); for (Button b : buttons) b.setEnabled(true); Log.d(TAMAGOTCHI, "Done initializeing the playing field"); }