Skip to content

Instantly share code, notes, and snippets.

@kylekellogg
Last active February 8, 2017 01:47
Show Gist options
  • Save kylekellogg/4d12bda444b482eef49354636583b8a1 to your computer and use it in GitHub Desktop.
Save kylekellogg/4d12bda444b482eef49354636583b8a1 to your computer and use it in GitHub Desktop.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.kris.firsttry.MainActivity">
<Button
android:text="$$$"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/GetMoney"
android:onClick="countIN"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<Button
android:text="Makes A Dude"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/MakeADude"
android:onClick="MakeDude"
android:layout_below="@+id/GetMoney"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignRight="@+id/GetMoney"
android:layout_alignEnd="@+id/GetMoney" />
<Button
android:text="Makes A Guy"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="MakeGuy"
android:id="@+id/MakeAGuy"
android:layout_below="@+id/MakeADude"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_toLeftOf="@+id/DudeCounter"
android:layout_toStartOf="@+id/DudeCounter" />
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/CurrentMoney"
android:text="Current Money"
android:layout_alignParentTop="true"
android:layout_above="@+id/MakeADude"
android:layout_toRightOf="@+id/GetMoney"
android:layout_toEndOf="@+id/GetMoney"
android:textAlignment="center" />
<TextView
android:text="Number Of Dudes"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/DudeCounter"
android:layout_alignBottom="@+id/MakeADude"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:layout_below="@+id/CurrentMoney"
android:layout_toRightOf="@+id/GetMoney"
android:layout_toEndOf="@+id/GetMoney"
android:textAlignment="center" />
<TextView
android:text="Number Of Guys"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/GuyCounter"
android:layout_alignBottom="@+id/MakeAGuy"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:layout_below="@+id/DudeCounter"
android:layout_toRightOf="@+id/MakeADude"
android:layout_toEndOf="@+id/MakeADude"
android:textAlignment="center" />
<Button
android:text="Upgrade Click Value"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/MakeAGuy"
android:onClick="ClickerUpgrade"
android:layout_toLeftOf="@+id/GuyCounter"
android:layout_toStartOf="@+id/GuyCounter"
android:id="@+id/UpgradeClickValue" />
<TextView
android:text="Click Upgrade Cost"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/GuyCounter"
android:layout_toRightOf="@+id/UpgradeClickValue"
android:id="@+id/ClickUpgrade"
android:layout_alignBottom="@+id/UpgradeClickValue"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:textAlignment="center" />
</RelativeLayout>
package com.kylekellogg.kristest;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.TextView;
import java.lang.ref.WeakReference;
import java.util.Locale;
import java.util.Timer;
import java.util.TimerTask;
public class MainActivity extends AppCompatActivity {
Thread thread;
TextView CurrentMoney;
TextView DudeCounter;
TextView GuyCounter;
int counter = 0;
int dudes = 0;
int guys = 0;
int ClickValue = 1;
int DudeCost = 10;
int GuyCost = 10;
int GuyExchange = 100;
int ClickUpCost = 100;
Handler handler;
Bundle customBundle;
@Override
protected void onCreate(Bundle savedInstanceState) {
// First, super & setContentView
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Second, we grab our references from the view
CurrentMoney = (TextView) findViewById(R.id.CurrentMoney);
DudeCounter = (TextView) findViewById(R.id.DudeCounter);
GuyCounter = (TextView) findViewById(R.id.GuyCounter);
// You can add more data in here if you need to!
customBundle = new Bundle();
customBundle.putInt("money", counter);
customBundle.putInt("dudes", dudes);
customBundle.putInt("guys", guys);
// Instantiate the things we'll be using later on
handler = new CustomHandler(CurrentMoney, DudeCounter, GuyCounter);
// Instantiate the things using ^ those things ;)
thread = new Thread(new Runnable() {
@Override
public void run() {
boolean shouldRun = true;
while (shouldRun) {
updateMoneyDudesAndGuys();
try {
Thread.sleep(100);
} catch (InterruptedException e) {
// This will stop the Thread
shouldRun = false;
e.printStackTrace();
}
}
}
});
thread.start();
// Combined the two Timers, we only needed one
new Timer().schedule(new TimerTask() {
public void run () {
counter += ClickValue;
if (dudes > 0) {
counter += dudes;
}
if (guys > 0) {
dudes += guys;
}
}
}, 1000, 1000);
}
public void countIN (View view) {
counter += ClickValue;
updateMoneyDudesAndGuys();
}
public void MakeDude (View view) {
if (counter >= DudeCost) {
counter -= DudeCost;
dudes++;
DudeCost = (int) Math.pow(DudeCost, 1.1);
updateMoneyDudesAndGuys();
}
}
public void MakeGuy (View view) {
if (counter >= GuyExchange) {
counter -= GuyExchange;
guys++;
GuyExchange = GuyExchange * 2;
updateMoneyDudesAndGuys();
} else if (dudes >= GuyCost) {
dudes -= GuyCost;
guys++;
GuyCost = (int) Math.pow(GuyCost, 1.1);
updateMoneyDudesAndGuys();
}
}
public void ClickerUpgrade (View view) {
if (counter >= ClickUpCost) {
counter -= ClickUpCost;
ClickValue = ClickValue * 2;
ClickUpCost = (int) Math.pow(ClickUpCost, 1.1);
updateMoneyDudesAndGuys();
}
}
private void updateMoneyDudesAndGuys () {
// You can add more here if you need to, just make sure to add it in onCreate too!
customBundle.putInt("money", counter);
customBundle.putInt("dudes", dudes);
customBundle.putInt("guys", guys);
// Sends the message
Message message = Message.obtain();
message.setData(customBundle);
handler.sendMessage(message);
}
static class CustomHandler extends Handler {
private final WeakReference<TextView> mMoneyTextView;
private final WeakReference<TextView> mDudesTextView;
private final WeakReference<TextView> mGuysTextView;
CustomHandler (TextView moneyTextView, TextView dudesTextView, TextView guysTextView) {
mMoneyTextView = new WeakReference<>(moneyTextView);
mDudesTextView = new WeakReference<>(dudesTextView);
mGuysTextView = new WeakReference<>(guysTextView);
}
@Override
public void handleMessage(Message msg) {
// Gets the data
Bundle bundle = msg.getData();
Integer money = bundle.getInt("money");
Integer dudes = bundle.getInt("dudes");
Integer guys = bundle.getInt("guys");
// Gets references to the TextViews
TextView moneyTextView = mMoneyTextView.get();
TextView dudesTextView = mDudesTextView.get();
TextView guysTextView = mGuysTextView.get();
// Update all if able to do so!
if (moneyTextView != null) {
moneyTextView.setText(String.format(Locale.getDefault(), "%d", money));
}
if (dudesTextView != null) {
dudesTextView.setText(String.format(Locale.getDefault(), "%d", dudes));
}
if (guysTextView != null) {
guysTextView.setText(String.format(Locale.getDefault(), "%d", guys));
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment