package vn.kienutt.farm; import android.app.Activity; import android.graphics.Canvas; import android.graphics.Color; import android.graphics.Paint; import android.os.Bundle; import android.os.Handler; import android.os.Looper; import android.view.MotionEvent; import android.view.View; import android.widget.Button; import android.widget.LinearLayout; import android.widget.TextView; public final class FarmActivity extends Activity { private FarmCore farm; private TextView status, message; private PlotView plots; private int selected = 0; private final Handler timer = new Handler(Looper.getMainLooper()); private final Runnable tick = new Runnable() { public void run() { farm.tick(System.currentTimeMillis()); refresh(); timer.postDelayed(this, 1000); } }; private int dp(int n) { return (int) (n * getResources().getDisplayMetrics().density + .5f); } @Override public void onCreate(Bundle state) { super.onCreate(state); String data = state != null ? state.getString("farm") : getPreferences(MODE_PRIVATE).getString("farm", null); try { farm = data == null ? new FarmCore(System.currentTimeMillis()) : FarmCore.restore(data); } catch (RuntimeException e) { farm = new FarmCore(System.currentTimeMillis()); } if (state != null) selected = state.getInt("selected", 0); LinearLayout root = new LinearLayout(this); root.setOrientation(LinearLayout.VERTICAL); root.setBackgroundColor(Color.rgb(238, 246, 230)); root.setOnApplyWindowInsetsListener( (v, i) -> { v.setPadding( dp(12) + i.getSystemWindowInsetLeft(), dp(8) + i.getSystemWindowInsetTop(), dp(12) + i.getSystemWindowInsetRight(), dp(8) + i.getSystemWindowInsetBottom()); return i; }); TextView title = new TextView(this); title.setText("Nông trại nhỏ · Android Java"); title.setTextSize(23); root.addView(title); status = new TextView(this); status.setTextSize(17); root.addView(status); plots = new PlotView(); root.addView(plots, new LinearLayout.LayoutParams(-1, 0, 1)); for (String[] row : new String[][] { {"Cà rốt · 3 xu / 30 giây", "Ngô · 5 xu / 60 giây"}, {"Tưới cây", "Thu hoạch"} }) { LinearLayout line = new LinearLayout(this); for (String label : row) { Button b = new Button(this); b.setText(label); b.setOnClickListener( v -> { long now = System.currentTimeMillis(); String result = label.startsWith("Cà") ? farm.sow(selected, 1, now) : label.startsWith("Ngô") ? farm.sow(selected, 2, now) : label.startsWith("Tưới") ? farm.water(selected, now) : farm.harvest(selected, now); message.setText(result); save(); refresh(); }); line.addView(b, new LinearLayout.LayoutParams(0, -2, 1)); } root.addView(line); } message = new TextView(this); message.setText("Chạm ô đất, gieo hạt, tưới rồi chờ cây chín. Mỗi 10 giây hồi 1 năng lượng."); root.addView(message); setContentView(root); refresh(); } private void save() { getPreferences(MODE_PRIVATE).edit().putString("farm", farm.encode()).apply(); } private void refresh() { status.setText( farm.coins() + " xu · " + farm.energy() + "/20 năng lượng · Ô " + (selected + 1)); plots.invalidate(); } @Override public void onResume() { super.onResume(); timer.post(tick); } @Override public void onPause() { timer.removeCallbacks(tick); save(); super.onPause(); } @Override public void onSaveInstanceState(Bundle b) { b.putString("farm", farm.encode()); b.putInt("selected", selected); super.onSaveInstanceState(b); } private final class PlotView extends View { private final Paint p = new Paint(Paint.ANTI_ALIAS_FLAG); private float cell, left, top; PlotView() { super(FarmActivity.this); setContentDescription("12 ô đất. Chạm để chọn ô trồng cây."); } @Override protected void onDraw(Canvas c) { super.onDraw(c); cell = Math.min(getWidth() / 4f, getHeight() / 3f); left = (getWidth() - cell * 4) / 2; top = (getHeight() - cell * 3) / 2; long now = System.currentTimeMillis(); p.setTextAlign(Paint.Align.CENTER); for (int i = 0; i < 12; i++) { float x = left + (i % 4) * cell, y = top + (i / 4) * cell; p.setColor(i == selected ? Color.rgb(35, 126, 83) : Color.rgb(125, 80, 48)); c.drawRoundRect(x + 4, y + 4, x + cell - 4, y + cell - 4, dp(10), dp(10), p); p.setColor(Color.WHITE); p.setTextSize(cell * .15f); c.drawText("Ô " + (i + 1), x + cell / 2, y + cell * .21f, p); int kind = farm.crop(i); if (kind == 0) { c.drawText("Đất trống", x + cell / 2, y + cell * .58f, p); continue; } double progress = farm.progress(i, now); p.setColor(kind == 1 ? Color.rgb(255, 166, 61) : Color.rgb(245, 222, 74)); float rad = (float) (cell * (.1 + .11 * progress)); c.drawCircle(x + cell / 2, y + cell * .5f, rad, p); p.setColor(Color.WHITE); c.drawText( farm.ready(i, now) ? "Thu hoạch!" : farm.watered(i) ? "Đã tưới" : "Cần tưới", x + cell / 2, y + cell * .8f, p); p.setColor(Color.rgb(69, 174, 113)); c.drawRect( x + cell * .1f, y + cell * .87f, (float) (x + cell * .1f + cell * .8f * progress), y + cell * .92f, p); } } @Override public boolean onTouchEvent(MotionEvent e) { if (e.getAction() != MotionEvent.ACTION_UP) return true; performClick(); int col = (int) Math.floor((e.getX() - left) / cell), row = (int) Math.floor((e.getY() - top) / cell); if (col >= 0 && col < 4 && row >= 0 && row < 3) { selected = row * 4 + col; refresh(); } return true; } @Override public boolean performClick() { super.performClick(); return true; } } }