Skip to content

Instantly share code, notes, and snippets.

View JakeSteam's full-sized avatar
🤖

Jake Lee JakeSteam

🤖
View GitHub Profile
@JakeSteam
JakeSteam / PuzzleGenerator.java
Created January 5, 2017 16:00
Asynchronous Puzzle Generator for Android, published on Game Dev Algorithms blog
package uk.co.jakelee.cityflow.components;
import android.app.Activity;
import android.app.Dialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.AsyncTask;
import android.widget.TextView;
import java.util.ArrayList;
@JakeSteam
JakeSteam / DisplayHelper.java
Last active February 12, 2019 02:34
"Android: Displaying Levels At Optimum Zoom" helper functions for GameDevAlgorithms
public int getTileWidth() {
return dpToPixel(Constants.TILE_WIDTH);
}
public int getTileHeight() {
return dpToPixel(Constants.TILE_HEIGHT);
}
public int dpToPixel(float dp) {
Resources resources = context.getResources();
@JakeSteam
JakeSteam / DisplayHelper.java
Created January 7, 2017 01:17
"Android: Displaying Levels At Optimum Zoom" core functions for GameDevAlgorithms
public TileDisplaySetup setupTileDisplay(PuzzleDisplayer puzzleDisplayer, List<Tile> tiles, ZoomableViewGroup tileContainer, Tile selectedTile, ImageView selectedTileImage, boolean isEditor) {
tileContainer.removeAllViews();
Setting minimumMillisForDrag = Setting.get(Constants.SETTING_MINIMUM_MILLIS_DRAG);
int dragDelay = minimumMillisForDrag != null ? minimumMillisForDrag.getIntValue() : 200;
Pair<Integer, Integer> maxXY = TileHelper.getMaxXY(tiles);
DisplayValues displayValues = getDisplayValues(puzzleDisplayer.getActivity(), maxXY.first + 1, maxXY.second + 1);
float optimumScale = displayValues.getZoomFactor();
@JakeSteam
JakeSteam / activity_main.xml
Created January 10, 2017 22:21
"Android: Asynchronous Database ORM Install" snippets for GameDevAlgorithms
<RelativeLayout
android:id="@+id/progressWrapper"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/mainLogo">
<uk.co.jakelee.cityflow.components.TextViewFont
android:id="@+id/progressText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
@JakeSteam
JakeSteam / VisitorHelper.java
Created January 24, 2017 19:11
"Android: Selecting A Weighted Random Item From A List" for GameDevAlgorithms.com
private static Visitor_Type selectVisitorType() {
Visitor_Type visitor = new Visitor_Type();
List<Visitor_Type> visitorTypes = Visitor_Type.findWithQuery(Visitor_Type.class,
"SELECT * FROM VisitorType WHERE visitor_id NOT IN (SELECT type FROM Visitor)");
// Work out the total weighting.
double totalWeighting = 0.0;
for (Visitor_Type type : visitorTypes) {
totalWeighting += type.getWeighting();
@JakeSteam
JakeSteam / AnvilActivity.java
Created January 24, 2017 20:15
"Android: Playing Random Sounds" for GameDevAlgorithms.com
SoundHelper.playSound(this, SoundHelper.smithingSounds);
@JakeSteam
JakeSteam / AttackHelper.java
Created February 19, 2017 13:56
"Calculating Weapon Damage With Ideal Ranges" for GameDevAlgorithms.com
int baseDamage = weapon.getDamage();
int weaponMinDistance = weapon.getWeaponDistance().getMinimum();
int weaponMaxDistance = weapon.getWeaponDistance().getMaximum();
// If in the weapon range, full damage
if (distance >= weaponMinDistance && distance <= weaponMaxDistance) {
return baseDamage;
}
int tilesOutOfRange = distance > weaponMaxDistance ? distance - weaponMaxDistance : weaponMinDistance - distance;
@JakeSteam
JakeSteam / LevelHelper.java
Last active March 10, 2024 16:00
"Converting Levels Into XP & Vice Versa" for GameDevAlgorithms.com
public static int convertXpToLevel(int xp) {
// Level = 0.05 * sqrt(xp)
return (int) (Constants.LEVEL_MODIFIER * Math.sqrt(xp));
}
public static int convertLevelToXp(int level) {
// XP = (Level / 0.05) ^ 2
return (int) Math.pow(level / Constants.LEVEL_MODIFIER, 2);
}
@JakeSteam
JakeSteam / ExportActivity.java
Created April 6, 2017 18:46
"Exporting Levels Into QR Codes Using ZXing" for GameDevAlgorithms.com
private void populateCard() {
...
StorageHelper.fillWithQrDrawable((ImageView) findViewById(R.id.puzzleQrCode), exportedText);
}
@JakeSteam
JakeSteam / CreatorActivity.java
Last active February 12, 2019 02:36
"Android: Importing Levels From QR Codes (Camera / File)" for GameDevAlgorithms.com
public void importFromCamera(View v) {
try {
Intent intent = new Intent("com.google.zxing.client.android.SCAN");
intent.putExtra("SCAN_MODE", "QR_CODE_MODE");
startActivityForResult(intent, INTENT_CAMERA);
} catch (Exception e) {
Toast.makeText(this, "Scanning QR codes requires a barcode reader to be installed!", Toast.LENGTH_SHORT).show();
Uri marketUri = Uri.parse("market://details?id=com.google.zxing.client.android");
Intent marketIntent = new Intent(Intent.ACTION_VIEW, marketUri);
startActivity(marketIntent);