diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..60ffe4f --- /dev/null +++ b/.gitignore @@ -0,0 +1,38 @@ +# Gradle files +.gradle/ +build/ + +# Local configuration file (sdk path, etc) +local.properties + +# Log/OS Files +*.log + +# Android Studio generated files and folders +captures/ +.externalNativeBuild/ +.cxx/ +*.apk +output.json + +# IntelliJ +*.iml +.idea/ +misc.xml +deploymentTargetDropDown.xml +render.experimental.xml + +# Keystore files +*.jks +*.keystore + +# Google Services (e.g. APIs or Firebase) +google-services.json + +# Android Profiling +*.hprof +/app/debug/output-metadata.json + +# Ha! +keystore/* +release \ No newline at end of file diff --git a/DesignMaterials/Screenshots/Phone/Screenshot_2014-12-12-16-58-02.png b/DesignMaterials/Screenshots/Phone/Screenshot_2014-12-12-16-58-02.png new file mode 100755 index 0000000..77dce6e Binary files /dev/null and b/DesignMaterials/Screenshots/Phone/Screenshot_2014-12-12-16-58-02.png differ diff --git a/DesignMaterials/Screenshots/Phone/Screenshot_2014-12-12-16-58-15.png b/DesignMaterials/Screenshots/Phone/Screenshot_2014-12-12-16-58-15.png new file mode 100755 index 0000000..64d41ce Binary files /dev/null and b/DesignMaterials/Screenshots/Phone/Screenshot_2014-12-12-16-58-15.png differ diff --git a/DesignMaterials/Screenshots/Phone/Screenshot_2014-12-12-16-59-37.png b/DesignMaterials/Screenshots/Phone/Screenshot_2014-12-12-16-59-37.png new file mode 100755 index 0000000..c1c18e0 Binary files /dev/null and b/DesignMaterials/Screenshots/Phone/Screenshot_2014-12-12-16-59-37.png differ diff --git a/DesignMaterials/Screenshots/Tablet_07/Screenshot1.png b/DesignMaterials/Screenshots/Tablet_07/Screenshot1.png new file mode 100755 index 0000000..29bf7b1 Binary files /dev/null and b/DesignMaterials/Screenshots/Tablet_07/Screenshot1.png differ diff --git a/DesignMaterials/Screenshots/Tablet_07/Screenshot2.png b/DesignMaterials/Screenshots/Tablet_07/Screenshot2.png new file mode 100755 index 0000000..2a2347d Binary files /dev/null and b/DesignMaterials/Screenshots/Tablet_07/Screenshot2.png differ diff --git a/DesignMaterials/Screenshots/Tablet_07/Screenshot3.png b/DesignMaterials/Screenshots/Tablet_07/Screenshot3.png new file mode 100755 index 0000000..c790504 Binary files /dev/null and b/DesignMaterials/Screenshots/Tablet_07/Screenshot3.png differ diff --git a/DesignMaterials/Screenshots/Tablet_07/Screenshot4.png b/DesignMaterials/Screenshots/Tablet_07/Screenshot4.png new file mode 100755 index 0000000..809d31a Binary files /dev/null and b/DesignMaterials/Screenshots/Tablet_07/Screenshot4.png differ diff --git a/DesignMaterials/Screenshots/Tablet_07/Screenshot5.png b/DesignMaterials/Screenshots/Tablet_07/Screenshot5.png new file mode 100755 index 0000000..edb507d Binary files /dev/null and b/DesignMaterials/Screenshots/Tablet_07/Screenshot5.png differ diff --git a/DesignMaterials/Screenshots/Tablet_07/Screenshot6.png b/DesignMaterials/Screenshots/Tablet_07/Screenshot6.png new file mode 100755 index 0000000..5b21e7e Binary files /dev/null and b/DesignMaterials/Screenshots/Tablet_07/Screenshot6.png differ diff --git a/DesignMaterials/Screenshots/Tablet_07/Screenshot7.png b/DesignMaterials/Screenshots/Tablet_07/Screenshot7.png new file mode 100755 index 0000000..af8c7d1 Binary files /dev/null and b/DesignMaterials/Screenshots/Tablet_07/Screenshot7.png differ diff --git a/DesignMaterials/bullet_2.png b/DesignMaterials/bullet_2.png new file mode 100755 index 0000000..e3b0fdf Binary files /dev/null and b/DesignMaterials/bullet_2.png differ diff --git a/DesignMaterials/bullet_3.png b/DesignMaterials/bullet_3.png new file mode 100755 index 0000000..240b05d Binary files /dev/null and b/DesignMaterials/bullet_3.png differ diff --git a/DesignMaterials/bullet_cc.png b/DesignMaterials/bullet_cc.png new file mode 100755 index 0000000..bfb920c Binary files /dev/null and b/DesignMaterials/bullet_cc.png differ diff --git a/DesignMaterials/bullet_cc2.png b/DesignMaterials/bullet_cc2.png new file mode 100755 index 0000000..105daa5 Binary files /dev/null and b/DesignMaterials/bullet_cc2.png differ diff --git a/DesignMaterials/bullet_cc2_100x100.png b/DesignMaterials/bullet_cc2_100x100.png new file mode 100755 index 0000000..26272ad Binary files /dev/null and b/DesignMaterials/bullet_cc2_100x100.png differ diff --git a/DesignMaterials/bullet_default.png b/DesignMaterials/bullet_default.png new file mode 100755 index 0000000..94a0676 Binary files /dev/null and b/DesignMaterials/bullet_default.png differ diff --git a/DesignMaterials/cctd_bgimage.png b/DesignMaterials/cctd_bgimage.png new file mode 100755 index 0000000..ac0bf26 Binary files /dev/null and b/DesignMaterials/cctd_bgimage.png differ diff --git a/DesignMaterials/cctd_image.png b/DesignMaterials/cctd_image.png new file mode 100755 index 0000000..c003082 Binary files /dev/null and b/DesignMaterials/cctd_image.png differ diff --git a/DesignMaterials/demon_king.png b/DesignMaterials/demon_king.png new file mode 100755 index 0000000..6158251 Binary files /dev/null and b/DesignMaterials/demon_king.png differ diff --git a/DesignMaterials/sprite_green01.png b/DesignMaterials/sprite_green01.png new file mode 100755 index 0000000..a1dda2c Binary files /dev/null and b/DesignMaterials/sprite_green01.png differ diff --git a/DesignMaterials/startmenu_20141221_1117.png b/DesignMaterials/startmenu_20141221_1117.png new file mode 100755 index 0000000..b7a3594 Binary files /dev/null and b/DesignMaterials/startmenu_20141221_1117.png differ diff --git a/DesignMaterials/startmenu_button_20141221_1148.png b/DesignMaterials/startmenu_button_20141221_1148.png new file mode 100755 index 0000000..6255341 Binary files /dev/null and b/DesignMaterials/startmenu_button_20141221_1148.png differ diff --git a/DesignMaterials/startmenu_empty_20141221_1148.png b/DesignMaterials/startmenu_empty_20141221_1148.png new file mode 100755 index 0000000..6ee8bc2 Binary files /dev/null and b/DesignMaterials/startmenu_empty_20141221_1148.png differ diff --git a/app/_gitignore b/app/_gitignore new file mode 100755 index 0000000..796b96d --- /dev/null +++ b/app/_gitignore @@ -0,0 +1 @@ +/build diff --git a/app/build.gradle b/app/build.gradle new file mode 100755 index 0000000..4827d5a --- /dev/null +++ b/app/build.gradle @@ -0,0 +1,25 @@ +apply plugin: 'com.android.application' + +android { + compileSdkVersion 21 + buildToolsVersion "21.1.1" + + defaultConfig { + applicationId "com.hyperling.examplegame_practice" + minSdkVersion 8 + targetSdkVersion 21 + versionCode 5 + versionName "1.04" + } + buildTypes { + release { + minifyEnabled false + proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' + } + } +} + +dependencies { + compile fileTree(dir: 'libs', include: ['*.jar']) + compile 'com.android.support:appcompat-v7:21.0.3' +} diff --git a/app/manifest-merger-release-report.txt b/app/manifest-merger-release-report.txt new file mode 100755 index 0000000..641bda2 --- /dev/null +++ b/app/manifest-merger-release-report.txt @@ -0,0 +1,71 @@ +-- Merging decision tree log --- +manifest +ADDED from AndroidManifest.xml:2:1 + xmlns:android + ADDED from AndroidManifest.xml:2:11 + package + ADDED from AndroidManifest.xml:3:5 + INJECTED from AndroidManifest.xml:0:0 + INJECTED from AndroidManifest.xml:0:0 + android:versionName + INJECTED from AndroidManifest.xml:0:0 + INJECTED from AndroidManifest.xml:0:0 + android:versionCode + INJECTED from AndroidManifest.xml:0:0 + INJECTED from AndroidManifest.xml:0:0 +application +ADDED from AndroidManifest.xml:5:5 +MERGED from com.android.support:appcompat-v7:21.0.3:16:5 +MERGED from com.android.support:support-v4:21.0.3:16:5 + android:label + ADDED from AndroidManifest.xml:8:9 + android:allowBackup + ADDED from AndroidManifest.xml:6:9 + android:icon + ADDED from AndroidManifest.xml:7:9 + android:theme + ADDED from AndroidManifest.xml:9:9 +activity#com.hyperling.examplegame_practice.MainActivity +ADDED from AndroidManifest.xml:11:9 + android:screenOrientation + ADDED from AndroidManifest.xml:14:13 + android:label + ADDED from AndroidManifest.xml:13:13 + android:name + ADDED from AndroidManifest.xml:12:13 +intent-filter#android.intent.action.MAIN+android.intent.category.LAUNCHER +ADDED from AndroidManifest.xml:15:13 +action#android.intent.action.MAIN +ADDED from AndroidManifest.xml:16:17 + android:name + ADDED from AndroidManifest.xml:16:25 +category#android.intent.category.LAUNCHER +ADDED from AndroidManifest.xml:18:17 + android:name + ADDED from AndroidManifest.xml:18:27 +activity#com.hyperling.examplegame_practice.Help +ADDED from AndroidManifest.xml:22:9 + android:screenOrientation + ADDED from AndroidManifest.xml:25:13 + android:label + ADDED from AndroidManifest.xml:24:13 + android:name + ADDED from AndroidManifest.xml:23:13 +activity#com.hyperling.examplegame_practice.Game +ADDED from AndroidManifest.xml:28:9 + android:screenOrientation + ADDED from AndroidManifest.xml:31:13 + android:label + ADDED from AndroidManifest.xml:30:13 + android:name + ADDED from AndroidManifest.xml:29:13 +uses-sdk +INJECTED from AndroidManifest.xml:0:0 reason: use-sdk injection requested +MERGED from com.android.support:appcompat-v7:21.0.3:15:5 +MERGED from com.android.support:support-v4:21.0.3:15:5 + android:targetSdkVersion + INJECTED from AndroidManifest.xml:0:0 + INJECTED from AndroidManifest.xml:0:0 + android:minSdkVersion + INJECTED from AndroidManifest.xml:0:0 + INJECTED from AndroidManifest.xml:0:0 diff --git a/app/proguard-rules.pro b/app/proguard-rules.pro new file mode 100755 index 0000000..feea2c7 --- /dev/null +++ b/app/proguard-rules.pro @@ -0,0 +1,17 @@ +# Add project specific ProGuard rules here. +# By default, the flags in this file are appended to flags specified +# in /home/usb/android-sdk/tools/proguard/proguard-android.txt +# You can edit the include path and order by changing the proguardFiles +# directive in build.gradle. +# +# For more details, see +# http://developer.android.com/guide/developing/tools/proguard.html + +# Add any project specific keep options here: + +# If your project uses WebView with JS, uncomment the following +# and specify the fully qualified class name to the JavaScript interface +# class: +#-keepclassmembers class fqcn.of.javascript.interface.for.webview { +# public *; +#} diff --git a/app/src/androidTest/java/com/hyperling/examplegame_practice/ApplicationTest.java b/app/src/androidTest/java/com/hyperling/examplegame_practice/ApplicationTest.java new file mode 100755 index 0000000..acebac3 --- /dev/null +++ b/app/src/androidTest/java/com/hyperling/examplegame_practice/ApplicationTest.java @@ -0,0 +1,13 @@ +package com.hyperling.examplegame_practice; + +import android.app.Application; +import android.test.ApplicationTestCase; + +/** + * Testing Fundamentals + */ +public class ApplicationTest extends ApplicationTestCase { + public ApplicationTest() { + super(Application.class); + } +} \ No newline at end of file diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml new file mode 100755 index 0000000..772715e --- /dev/null +++ b/app/src/main/AndroidManifest.xml @@ -0,0 +1,35 @@ + + + + + + + + + + + + + + + + + + + + + diff --git a/app/src/main/java/com/hyperling/examplegame_practice/Bullet.java b/app/src/main/java/com/hyperling/examplegame_practice/Bullet.java new file mode 100755 index 0000000..2d8392b --- /dev/null +++ b/app/src/main/java/com/hyperling/examplegame_practice/Bullet.java @@ -0,0 +1,110 @@ +package com.hyperling.examplegame_practice; + +import android.graphics.Bitmap; +import android.graphics.BitmapFactory; +import android.graphics.Canvas; +import android.graphics.Rect; + +/** + * Created by usb on 12/19/14. + */ +public class Bullet { + + int speed = 100; + int pauseSpeed = 1; + int xSpeed, ySpeed; + int damage; + + Bitmap bmp; + + int width, height; + + int centerX, centerY; + + int startX, startY; + int x, y; + + Minion minion; + + boolean xLocked, yLocked; + boolean spent = false; + + public Bullet(GameView gv, Bitmap bmp, int damage, int startX, int startY, Minion minion){ + width = bmp.getWidth(); + height = bmp.getHeight(); + + centerX = startX; + centerY = startY; + + this.startX = centerX - width/2; + this.startY = centerY - height/2; + + x = this.startX; + y = this.startY; + + this.minion = minion; + + this.bmp = bmp; + + this.damage = damage; + + xSpeed = gv.getWidth()/speed; + ySpeed = gv.getHeight()/speed; + xLocked = false; + yLocked = false; + } + + public void update(){ + + // Minion location towards minion + int x2 = minion.centerX; + int y2 = minion.centerY; + + // Which direction do we need to move? + // Find the sign on the direction, then multiply by the speed + if(xLocked){ + x = minion.centerX - width/2; + } + else { + try { + x = x + (((x2 - centerX) / Math.abs(x2 - centerX)) * xSpeed * pauseSpeed); + } catch (Exception e) { + e.printStackTrace(); + } + } + + if(yLocked) { + y = minion.centerY - height / 2; + } + else { + try { + y = y + (((y2 - centerY) / Math.abs(y2 - centerY)) * ySpeed * pauseSpeed); + } catch (Exception e) { + e.printStackTrace(); + } + } + + centerX = x + width/2; + centerY = y + height/2; + } + + public void onDraw(Canvas canvas){ + update(); + + Rect src = new Rect(startX, startY, startX + width, startY + height); + + Rect dest = new Rect(x, y, x + width, y + height); + + canvas.drawBitmap(bmp, src, dest, null); + } + + public boolean isCollision(){ + boolean collide = false; + + if((x + width >= minion.x || x < minion.x + minion.width) && (y + height > y || y <= minion.y + minion.height)){ + collide = true; + } + + return collide; + } +} diff --git a/app/src/main/java/com/hyperling/examplegame_practice/Changelog b/app/src/main/java/com/hyperling/examplegame_practice/Changelog new file mode 100755 index 0000000..99f8f82 --- /dev/null +++ b/app/src/main/java/com/hyperling/examplegame_practice/Changelog @@ -0,0 +1,13 @@ +v1.04 +Got onto git! + +v1.03 + +v1.02 +Fixed pause button +Added Help menu +Added graphics to start menu +Note: Do not hit the back button while in game + +v1.01 +Added the icon! \ No newline at end of file diff --git a/app/src/main/java/com/hyperling/examplegame_practice/Game.java b/app/src/main/java/com/hyperling/examplegame_practice/Game.java new file mode 100755 index 0000000..45350c2 --- /dev/null +++ b/app/src/main/java/com/hyperling/examplegame_practice/Game.java @@ -0,0 +1,112 @@ +package com.hyperling.examplegame_practice; + +import android.support.v7.app.ActionBarActivity; +import android.os.Bundle; +import android.view.Menu; +import android.view.MenuItem; + + +public class Game extends ActionBarActivity { + + public String filename = "/data/data/com.hyperling.examplegame_practice/savedata"; + + public SAL sal; + + public boolean contin = true; + public boolean exists = false; + + public boolean mainPause = false; + + public static GameView gameview; + + public Game(){ + //Empty + } + + public Game(boolean bool){ + this.contin = bool; + } + + @Override + protected void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + setContentView(R.layout.activity_load); + + gameview = new GameView(Game.this); + + sal = new SAL(filename); + + if(contin){ + //try{ + // exists = sal.check(); + //} catch(Exception e){System.out.println("Error checking in file exists in Continue Game");} + + //if(exists){ + System.out.println("Loading continue.."); + //sal.load(gameview); + onRestart(); + //} + } + + setContentView(gameview); + } + + public void onStart(){ + super.onStart(); + } + + protected void onStop(){ + super.onStop(); + + sal.save(gameview); + //gameview.stopThreads(); + } + + protected void onRestart(){ + super.onRestart(); + + sal.load(gameview); + } + + + @Override + public boolean onCreateOptionsMenu(Menu menu) { + // Inflate the menu; this adds items to the action bar if it is present. + getMenuInflater().inflate(R.menu.menu_game, menu); + return true; + } + + @Override + public boolean onOptionsItemSelected(MenuItem item) { + // Handle action bar item clicks here. The action bar will + // automatically handle clicks on the Home/Up button, so long + // as you specify a parent activity in AndroidManifest.xml. + int id = item.getItemId(); + if (id == R.id.action_reset) { + gameview = new GameView(Game.this); + setContentView(gameview); + return true; + } + else if (id == R.id.action_pause) { + mainPause = !mainPause; + if(mainPause){ + //gameview.stopThreads(); + item.setTitle("Resume"); + } + else{ + //gameview.startThreads(); + item.setTitle("Pause"); + } + try { + gameview.setMainPause(mainPause); + } + catch(Exception e){ + e.printStackTrace(); + System.out.println(e.toString()); + System.out.println(mainPause); + } + return true; + } + return super.onOptionsItemSelected(item); + } +} diff --git a/app/src/main/java/com/hyperling/examplegame_practice/GameGrid.java b/app/src/main/java/com/hyperling/examplegame_practice/GameGrid.java new file mode 100755 index 0000000..c666ff0 --- /dev/null +++ b/app/src/main/java/com/hyperling/examplegame_practice/GameGrid.java @@ -0,0 +1,262 @@ +package com.hyperling.examplegame_practice; + +import android.graphics.Canvas; +import android.graphics.Color; +import android.graphics.Paint; + +import java.util.ArrayList; + +/** + * Created by usb on 11/9/14. + */ +public class GameGrid { + GameView gv; + Paint paint = new Paint(); + + static int ROWS; + static int COLUMNS; + + static GridCell[][] CELL; + + public GridCell startCell; + public GridCell endCell; + + public GridCell basicUpgradeCell = new GridCell(); + public GridCell slowUpgradeCell = new GridCell(); + + ArrayList buildCells = new ArrayList(); + ArrayList towers = new ArrayList(); + + //ArrayList pauseButton = new ArrayList(); + + static int CELL_COUNT; + + static int CELL_WIDTH; + static int CELL_HEIGHT; + + boolean showUpgrades; + + public GameGrid(int numRows, int numColumns, GameView gameView){ + paint.setStrokeWidth(0); + + ROWS = numRows; + COLUMNS = numColumns; + + CELL = new GridCell[ROWS][COLUMNS]; + + gv = gameView; + + CELL_COUNT = ROWS*COLUMNS; + + CELL_WIDTH = gv.getWidth()/COLUMNS; + CELL_HEIGHT = gv.getHeight()/ROWS; + + int q = 0; + for(int i = 0; i < ROWS; i++){ + for(int j = 0; j < COLUMNS; j++){ + int[] id = {j,i}; // Need to be switched for some reason + CELL[i][j] = new GridCell(id, CELL_WIDTH, CELL_HEIGHT); + + ++q; + if(q == CELL_COUNT){ + System.out.println("All cells created!"); + } + } + } + } + + public void onDraw(Canvas canvas){ + + paintCells(canvas); + + paintBuildableBorders(canvas); + + paintTowers(canvas); + + paintTowerInfo(canvas); + + if(showUpgrades){ + paintUpgradeChoices(canvas); + } + else{ + basicUpgradeCell.color = new GridCell().color; + slowUpgradeCell.color = new GridCell().color; + + basicUpgradeCell = new GridCell(); + slowUpgradeCell = new GridCell(); + } + + } + + private void paintCells(Canvas canvas){ + for(int i = 0; i < ROWS; i++){ + for(int j = 0; j < COLUMNS; j++){ + + paint.setColor(CELL[i][j].getColor()); + + int side[] = CELL[i][j].getSides(); + + if (side[0] < 0) { + side[0] = 0; + } + if (side[1] < 0) { + side[1] = 0; + } + if (side[2] > gv.getWidth()) { + side[2] = gv.getWidth(); + } + if (side[3] > gv.getHeight()) { + side[3] = gv.getHeight(); + } + + canvas.drawRect(side[0], side[1], side[2], side[3], paint); + //} catch(Exception e){ + //System.out.println("GameGrid.paintCells() threw an error at cell[" + i + "][" + j + "]: " + e.toString()); + //System.out.println("(int)Color is " + tempC); + //} + + } + } + } + + public void paintBuildableBorders(Canvas canvas){ + for(GridCell buildCell : buildCells){ + + paint.setColor(buildCell.buildableInterior); + + /* + int[] temp = buildCell.id; + int i = temp[0]; + int j = temp[1]; + */ + + int[]side = buildCell.side; + + int xSpace = buildCell.width/10; + int ySpace = buildCell.height/10; + + canvas.drawRect(side[0] + xSpace, side[1] + ySpace, side[2] - xSpace, side[3] - ySpace, paint); + } + } + + public void paintTowers(Canvas canvas){ + String slow = ""; + for(GridCell tower : towers){ + + paint.setColor(tower.innerColor); + + /* + int[] temp = tower.id; + int i = temp[0]; + int j = temp[1]; + */ + int[]side = tower.side; + + int xSpace = tower.width/4; + int ySpace = tower.height/4; + + canvas.drawRect(side[0] + xSpace, side[1] + ySpace, side[2] - xSpace, side[3] - ySpace, paint); + + paint.setColor(tower.buildableInterior); + + if(tower.isSlowTower){ + slow = "S"; + } else { + slow = ""; + } + + canvas.drawText(tower.towerType + slow, tower.centerForTextX, tower.centerForTextY, paint); + } + } + + public void paintTowerInfo(Canvas canvas){ + GridCell fakeTower = new GridCell(); + fakeTower.setTower(); + + int firstcost = 2; + int cost = firstcost; + int upgradecost; + int spent; + + GridCell printCell = CELL[ROWS-2][0]; + + paint.setColor(Color.YELLOW); + canvas.drawText("Tower build costs:", printCell.centerForTextX, printCell.centerForTextY, paint); + + for(int i = 0; i < COLUMNS/2; i++){ + printCell = CELL[ROWS-1][i]; + printCell.color = Color.WHITE; + + upgradecost = fakeTower.towerUpgradeCost; + + paint.setColor(fakeTower.innerColor); + canvas.drawText("" + cost, printCell.centerForTextX, printCell.centerForTextY, paint); + + fakeTower.upgrade(); + + cost = upgradecost; + } + } + + public void paintUpgradeChoices(Canvas canvas){ + GridCell printCell = CELL[ROWS-2][COLUMNS-4]; + + paint.setColor(Color.YELLOW); + canvas.drawText("Upgrade choices:", printCell.centerForTextX, printCell.centerForTextY, paint); + + int choice = 0; + for(int i = COLUMNS-1; i >= COLUMNS - printCell.towerChoices; i--){ + printCell = CELL[ROWS-1][i]; + + if(choice == 0){ + printCell.color = Color.RED; + canvas.drawText("Basic", printCell.centerForTextX, printCell.centerForTextY, paint); + basicUpgradeCell = printCell; + } + else if(choice == 1){ + printCell.color = Color.BLUE; + canvas.drawText("Slow", printCell.centerForTextX, printCell.centerForTextY, paint); + slowUpgradeCell = printCell; + } + + choice = choice + 1; + } + + } + + public void setStartCell(int[] id){ + CELL[id[0]][id[1]].setStart(); + startCell = CELL[id[0]][id[1]]; + } + + public void setEndCell(int[] id){ + + CELL[id[0]][id[1]].setEnd(); + endCell = CELL[id[0]][id[1]]; + } + + public void setPathCell(int[] id){ + CELL[id[0]][id[1]].setPath(); + } + + public void setBuildableCell(int[] id){ + CELL[id[0]][id[1]].setBuildable(); + buildCells.add(CELL[id[0]][id[1]]); + } + + public void setTowerCell(int[] id){ + CELL[id[0]][id[1]].setTower(); + towers.add(CELL[id[0]][id[1]]); + //System.out.println("Tower added: " + id[0] + ", " + id[1]); + } + + /* + public void setPausableCell(int[] id){ + pauseButton.add(CELL[id[0]][id[1]]); + } + */ + + public void setNullCell(int[] id){ + CELL[id[0]][id[1]].setNull(); + } +} diff --git a/app/src/main/java/com/hyperling/examplegame_practice/GameLoopThread.java b/app/src/main/java/com/hyperling/examplegame_practice/GameLoopThread.java new file mode 100755 index 0000000..75d7bc5 --- /dev/null +++ b/app/src/main/java/com/hyperling/examplegame_practice/GameLoopThread.java @@ -0,0 +1,57 @@ +package com.hyperling.examplegame_practice; + +import android.annotation.SuppressLint; +import android.graphics.Canvas; + +/** + * Created by usb on 10/23/14. + */ +public class GameLoopThread extends Thread{ + + private GameView gameView; + private boolean running = false; + + static final long FPS = 10; + + public GameLoopThread(GameView gv){ + gameView = gv; + } + + public void setRunning(boolean run){ + running = run; + } + + @SuppressLint("WrongCall") + @Override + public void run(){ + + long tick = 1000 / FPS; + long startTime; + long sleepTime; + + while(running){ + Canvas c = null; + startTime = System.currentTimeMillis(); + try { + c = gameView.getHolder().lockCanvas(); + synchronized (gameView.getHolder()) { + gameView.onDraw(c); + } + } finally { + if (c != null) { + gameView.getHolder().unlockCanvasAndPost(c); + } + } + sleepTime = tick - (System.currentTimeMillis() - startTime); + + try { + if (sleepTime > 0) { + sleep(sleepTime); + } + else { + sleep(10); + } + } catch (Exception e) {} + } + } +} diff --git a/app/src/main/java/com/hyperling/examplegame_practice/GameView.java b/app/src/main/java/com/hyperling/examplegame_practice/GameView.java new file mode 100755 index 0000000..4624404 --- /dev/null +++ b/app/src/main/java/com/hyperling/examplegame_practice/GameView.java @@ -0,0 +1,938 @@ +package com.hyperling.examplegame_practice; + +import android.annotation.SuppressLint; +import android.app.Activity; +import android.app.AlertDialog; +import android.content.Context; +import android.content.DialogInterface; +import android.graphics.Bitmap; +import android.graphics.BitmapFactory; +import android.graphics.Canvas; +import android.graphics.Color; +import android.graphics.Paint; +import android.os.Looper; +import android.view.MotionEvent; +import android.view.SurfaceHolder; +import android.view.SurfaceView; + +import java.io.Serializable; +import java.util.ArrayList; +import java.util.Timer; +import java.util.TimerTask; + +/** + * Created by usb on 10/16/14. + * + * + */ +public class GameView extends SurfaceView implements Serializable { + + private SurfaceHolder holder; + + private ArrayList minions = new ArrayList(); + private ArrayList bullets = new ArrayList(); + //private int [][] minionTypes = new int [3][20]; // {amount, drawable, role} + + private GameLoopThread gameLoopThread; + private Context baseContext; + + Paint paint = new Paint(); + + + Timer timer = new Timer(); + TimerTask startGameTask; + TimerTask startNextLevelTask; + + + GameGrid gameGrid; + + // Map specs + int numRows = 9; + int numColumns = 16; + int[] startCoords = {4, 0}; + int[] endCoords = {4, 15}; + int[] pathCoordsX = {4,4,4,4,4,4,4,4,4,4 ,4 ,4, 4, 4 ,-666}; // Row + int[] pathCoordsY = {1,2,3,4,5,6,7,8,9,10,11,12,13,14,-666};// Column + int[] buildableCoordsX = {5,5,5,5,5,5,5,5,5, 5, 5, 5, 3,3,3,3,3,3,3,3,3, 3, 3, 3, -666}; + int[] buildableCoordsY = {2,3,4,5,6,7,8,9,10,11,12,13,2,3,4,5,6,7,8,9,10,11,12,13,-666}; + /* + int[] pausableCoordsX = { 1, 1, 1, 1, 2, 2, 2, 2,-666}; + int[] pausableCoordsY = {11,12,13,14,11,12,13,14,-666}; + */ + + int startX, startY, endX, endY; + int[] startSides, endSides; + + int defaultLives = 10; + int lives = defaultLives; + + //boolean spawn = true; + int defaultExtraSpawn = 3; + int spawnNum = defaultExtraSpawn; + int spawnMore = spawnNum; + int spawnMoreBosses = 0; + + int defaultRole = 1; + int role = defaultRole; + + int defaultSpeed = 250; + int speed = defaultSpeed; + + boolean remove; + int livesLost; + + GridCell selectedTower; + + boolean gridCreated = false; + + boolean shoot; + int shootMinion; + int shootTower; + + boolean gameOver = false; + + boolean paused = false; + private Boolean mainPause = false; + public long mainPauseTime = 0; + public long mainUnPauseTime = 0; + private int minionPauseSpeed = 0; + int startTime = 2000; + + int round = 1; + int wave = 0; + + int killCount = 0; + + int defaultMoney = 4; + int money = defaultMoney; + + private long lastClick; + + @SuppressLint("WrongCall") + public GameView(Context context) { + + super(context); + + baseContext = context; + + gameLoopThread = new GameLoopThread(this); + + holder = getHolder(); + + holder.addCallback(new SurfaceHolder.Callback() { + + @Override + public void surfaceDestroyed(SurfaceHolder holder) { + + boolean retry = true; + + gameLoopThread.setRunning(false); + + while (retry) { + + try { + + gameLoopThread.join(); + + retry = false; + + } catch (InterruptedException e) { + + } + + gameLoopThread = new GameLoopThread(GameView.this); + } + } + + @Override + public void surfaceCreated(SurfaceHolder holder) { + /* + Canvas c = holder.lockCanvas(null); + onDraw(c); + holder.unlockCanvasAndPost(c); + */ + paint.setColor(Color.YELLOW); + paint.setStrokeWidth(0); + + /* + timer = new Timer(); + startGameTask = new TimerTask() { + @Override + public void run() { + createMinions(); + + gameLoopThread.setRunning(true); + + gameLoopThread.start(); + } + }; + + startNextLevelTask = new TimerTask() { + @Override + public void run() { + createMinions(); + } + }; + */ + + createGrid(); + + paused = true; + + startGameTask = new TimerTask() { + @Override + public void run() { + paused = false; + + if(wave == 0){ + startNextLevel(); + } + + startTime = 0; + } + }; + + gameLoopThread.setRunning(true); + + gameLoopThread.start(); + + //System.out.println("Game will start in 5 seconds..."); + timer.schedule(startGameTask, startTime); + } + + @Override + public void surfaceChanged(SurfaceHolder holder, int format, + int width, int height) { + } + + }); + + } + + + @Override + protected void onDraw(Canvas canvas) { + try { + canvas.drawColor(Color.BLACK); + + // Display stats + canvas.drawText("Level: Round " + round + " wave " + wave, gameGrid.CELL[0][0].x, gameGrid.CELL[0][0].centerY, paint); + canvas.drawText("Money: " + money, gameGrid.CELL[1][0].x, gameGrid.CELL[1][0].y, paint); + canvas.drawText("Lives left: " + lives, gameGrid.CELL[1][0].x, gameGrid.CELL[1][0].centerY, paint); + + if (!gameOver) { + + gameGrid.onDraw(canvas); + + shootMinion = 0; + + for (GridCell tower : gameGrid.towers) { + tower.isReloading(mainPauseTime, mainUnPauseTime); + gameGrid.paintTowers(canvas); + } + + if (mainPause) { + for (Minion minion : minions) { + if (minion.xSpeed > 0) { + minionPauseSpeed = minion.xSpeed; + minion.xSpeed = 0; + } + minion.onDraw(canvas); + } + for(Bullet bullet : bullets){ + if(bullet.pauseSpeed == 1){ + bullet.pauseSpeed = 0; + } + bullet.onDraw(canvas); + } + } else { + for (Minion minion : minions) { + if (minion.xSpeed == 0) { + minion.xSpeed = minionPauseSpeed; + } + } + + for (Bullet bullet : bullets){ + if(bullet.pauseSpeed == 0){ + bullet.pauseSpeed = 1; + } + } + + if (!paused) { + /* + // Paint the track! + paintSquare(canvas, startX, startY, Color.GREEN); + paintSquare(canvas, endX, endY, Color.RED); + paintRect(canvas, startX + trackSize / 2, startY + trackSize / 2, endX - startX - trackSize, trackSize, Color.YELLOW); + */ + + if (wave == 6) { + //Spawn more bosses + if (minions.size() == 0 && spawnMoreBosses > 0) { + createBosses(); + --spawnMoreBosses; + } + } else { + // Check if able to spawn + if (minions.size() == 0 && spawnMore > 0) { + createMinions(); + --spawnMore; + } + } + + // Move the bullets + for(Bullet bullet : bullets){ + bullet.onDraw(canvas); + System.out.println("Drawing bullet for minion " + minions.indexOf(bullet.minion)); + + } + + // See if bullet is hitting minion + /* + for(Bullet bullet : bullets){ + if (!bullet.spent) { + if (bullet.minion.health > 0 && bullet.minion.isCollision(bullet.centerX, bullet.centerY)) { + damageMinion(bullet.minion, bullet.damage); + bullet.spent = true; + } + } + } + */ + for(Bullet bullet : bullets){ + if (!bullet.spent) { + if (bullet.minion.health > 0 && bullet.isCollision()) { + damageMinion(bullet.minion, bullet.damage); + bullet.spent = true; + } + } + } + + //for (int n = minions.size() - 1; n >= 0; n--) { + for (Minion minion : minions) { + //Minion minion = minions.get(n); + + minion.onDraw(canvas); + + + + /* + // Check if able to spawn + if(minion.isCollision(startSides)){ + spawn = false; + } else { + spawn = true; + } + */ + + if (wave == 6) { + //Spawn more bosses + if ((!minions.get(minions.size() - 1).isCollision(startSides)) && spawnMoreBosses > 0) { + createBosses(); + --spawnMoreBosses; + } + } else { + // Check if able to spawn + if ((!minions.get(minions.size() - 1).isCollision(startSides)) && spawnMore > 0) { + createMinions(); + --spawnMore; + } + } + + + + /* + // Check if under tower fire + int[] minionCell = minion.getCellLocation(numRows, numColumns); + int i = minionCell[0]; + int j = minionCell[1]; + GridCell testTower; + + // Test left cell + if(i > 1) { + testTower = gameGrid.CELL[i - 1][j]; + if (testTower.isTower && System.currentTimeMillis() - testTower.lastShot > 500) { + shootMinion(testTower, minion); + break; + } + } + // Test top cell + if(j > 1) { + testTower = gameGrid.CELL[i][j - 1]; + if (testTower.isTower && System.currentTimeMillis() - testTower.lastShot > 500) { + shootMinion(testTower, minion); + break; + } + } + // Test right cell + if(i < numRows) { + testTower = gameGrid.CELL[i + 1][j]; + if (testTower.isTower && System.currentTimeMillis() - testTower.lastShot > 500) { + shootMinion(testTower, minion); + break; + } + } + // Test bottom cell + if(j < numColumns) { + testTower = gameGrid.CELL[i][j + 1]; + if (testTower.isTower && System.currentTimeMillis() - testTower.lastShot > 500) { + shootMinion(testTower, minion); + break; + } + } + */ + /* + for(GridCell towerCell: gameGrid.towers){ + shoot = !towerCell.isReloading(); + //System.out.println("Tower " + towerCell.id[0] + ", " + towerCell.id[1] + "is in range: " + towerCell.isInRange(minion.x, minion.y)); + if(shoot) { + if(towerCell.isInRange(minion.x, minion.y)){ + shootMinion(towerCell, minion); + break; + } + } + } + */ + + for (GridCell towerCell : gameGrid.towers) { + //System.out.println("Tower " + towerCell.id[0] + ", " + towerCell.id[1] + "is in range: " + towerCell.isInRange(minion.x, minion.y)); + if (!towerCell.isReloading(mainPauseTime, mainUnPauseTime)) { + if (towerCell.isInRange(minion.x, minion.y)) { + if (minion.x >= minions.get(shootMinion).x) { + shoot = true; + shootMinion = minions.indexOf(minion); + shootTower = gameGrid.towers.indexOf(towerCell); + //shootMinion(towerCell, minion); + //break; + } else { + shoot = true; + } + } + } + } + + /* + // Check if minion has reached end + if(minion.isCollision(endSides)){ + --lives; + remove = true; + } + */ + + // Check if minion has reached end + if (minion.isCollision(endSides)) { + remove = true; + livesLost = 1; + } + } + + for(Bullet bullet : bullets){ + if(bullet.spent){ + bullets.remove(bullet); + } + } + + if (shoot) { + if (minions.get(shootMinion).health <= gameGrid.towers.get(shootTower).damage) { // && shootMinion == 0) { + remove = false; + } + shootMinion(gameGrid.towers.get(shootTower), minions.get(shootMinion)); + shoot = false; + } + + + // Remove minion if needed + if (remove) { + for(Bullet bullet : bullets){ + if(minions.indexOf(bullet.minion) == 0){ + bullet.spent = true; + } + } + minions.remove(minions.get(0)); + lives = lives - livesLost; + //money = money + livesLost; + remove = false; + } + + /* + // Spawn more if needed + if(spawnMore > 0 && spawn){ + createMinions(); + --spawnMore; + } + + // Remove minion if needed + if(remove){ + minions.remove(minions.get(0)); + remove = false; + } + */ + + // If minions have been defeated, advance levels! + if (spawnMore == 0 && spawnMoreBosses == 0 && minions.size() == 0) { + startNextLevel(); + } + + // If all lives have been lost, it's game over! + if (lives <= 0) { + gameOver = true; + } + + /* + // Update the runners + try { + for (Minion minion : minions) { + minion.onDraw(canvas); + canvas.drawText("" + minion.health, minion.x + minion.width / 8, minion.y - trackSize / 2, paint); + + //Spawn minion after last leaves spawn + if (minion.x > startX + trackSize / 2 && spawnMore > 0) { + spawn = true; + } + else { + spawn = false; + } + + // If a minion reaches the end, remove him and a life + if (minion.isCollision(endX, endY)) { + minions.remove(minion); + --lives; + + // If out of lives, game over! + if (lives <= 0) { + gameOver = true; + } + } + + } + } + catch(Exception e){ + System.out.println(e.toString()); + } + + if(spawn){ + minions.add(createMinion(R.drawable.sprite_green01, 5)); + --spawnMore; + spawn = false; + } + */ + } + } + } else { + canvas.drawText("GAME OVER", getWidth() / 2, getHeight() / 2, paint); + canvas.drawText("Round " + round + " wave " + wave, getWidth() / 2, getHeight() / 2 + 20, paint); + } + } + catch(Exception e){ + System.out.println("Error in onDraw!"); + e.printStackTrace(); + System.out.println("Error in onDraw!"); + } + } + + private void createBosses(){ + minions.add(createMinion(R.drawable.demon_king, round*10, speed*2)); + + } + + private void createMinions(){ + //bmp = BitmapFactory.decodeResource(getResources(), R.drawable.sprite_green01); + /* */ + + minions.add(createMinion(R.drawable.sprite_green01, role, speed)); + + // */ + /* + for(; minionTypes[0][level] >= 0; --minionTypes[0][level]){ + minions.add(createMinion(minionTypes[1][level], minionTypes[2][level])); + } + // */ + } + + private Minion createMinion(int resource, int role, int speed) { + + Bitmap bmp = BitmapFactory.decodeResource(getResources(), resource); + + return new Minion(this, bmp, role, speed, startX, startY); + + } + + private Bullet createBullet(int bulletresource, int damage, int startX, int startY, Minion minion){ + + Bitmap bulletbmp = BitmapFactory.decodeResource(getResources(), bulletresource); + + return new Bullet(this, bulletbmp, damage, startX, startY, minion); + } + + private void createGrid(){ + + if(!gridCreated) { + gameGrid = new GameGrid(numRows, numColumns, GameView.this); + + gameGrid.setStartCell(startCoords); + gameGrid.setEndCell(endCoords); + + for (int i = 0; true; i++) { + if (pathCoordsX[i] == -666) { + break; + } + int[] tempID = {pathCoordsX[i], pathCoordsY[i]}; + gameGrid.setPathCell(tempID); + } + + for (int i = 0; true; i++) { + if (buildableCoordsX[i] == -666) { + break; + } + int[] tempID = {buildableCoordsX[i], buildableCoordsY[i]}; + gameGrid.setBuildableCell(tempID); + } + + /* + for(int i = 0; true; i++){ + if(pausableCoordsX[i] == -666){ + break; + } + int[] tempID = {pausableCoordsX[i], pausableCoordsY[i]}; + gameGrid.setPausableCell(tempID); + } + */ + + startX = gameGrid.startCell.centerX; + startY = gameGrid.startCell.centerY; + startSides = gameGrid.startCell.getSides(); + + endX = gameGrid.endCell.centerX; + endY = gameGrid.endCell.centerY; + endSides = gameGrid.endCell.getSides(); + + gridCreated = true; + } + } +/* + private void paintSquare(Canvas canvas, int x, int y, int color){ + paint.setColor(color); + paint.setStrokeWidth(0); + int side[] = new int[4]; + + side[0] = x - trackSize/2;//left + side[1] = y + trackSize/2;//top + side[2] = x + trackSize/2;//right + side[3] = y - trackSize/2;//bottom + + if(side[0] < 0) { + side[0] = 0; + } + if(side[1] < 0) { + side[1] = 0; + } + if(side[2] > getWidth()) { + side[2] = getWidth(); + } + if(side[3] > getHeight()) { + side[3] = getHeight(); + } + + canvas.drawRect(side[0], side[1], side[2], side[3], paint); + } + + private void paintRect(Canvas canvas, int x, int y, int width, int height, int color){ + paint.setColor(color); + paint.setStrokeWidth(0); + int side[] = new int[4]; + + side[0] = x;//left + side[1] = y;//top + side[2] = x + width;//right + side[3] = y - height;//bottom + + if(side[0] < 0) { + side[0] = 0; + } + if(side[1] < 0) { + side[1] = 0; + } + if(side[2] > getWidth()) { + side[2] = getWidth(); + } + if(side[3] > getHeight()) { + side[3] = getHeight(); + } + + canvas.drawRect(side[0], side[1], side[2], side[3], paint); + } +*/ + + @Override + public boolean onTouchEvent(MotionEvent event){ + + if (System.currentTimeMillis() - lastClick > 250) { + + lastClick = System.currentTimeMillis(); + + float x = event.getX(); + + float y = event.getY(); + + synchronized (getHolder()) { + + /* + for (int i = minions.size() - 1; i >= 0; i--) { + + Minion minion = minions.get(i); + + if (minion.isCollision(x, y)) { + + damageMinion(minion); + + if (minions.size() == 0) { + startNextLevel(); + } + break; + + } + + } + */ + /* + for(GridCell pause : gameGrid.pauseButton){ + if(pause.isCollision(x,y) && !paused){ + paused = true; + pauseTime = System.currentTimeMillis(); + } + else if(pause.isCollision(x,y) && paused){ + paused = false; + unPauseTime = System.currentTimeMillis(); + } + } + */ + + for(GridCell tower: gameGrid.towers){ + if(tower.isCollision(x,y)){ //) && money >= tower.towerUpgradeCost){ + /* + money = money - tower.towerUpgradeCost; + tower.upgrade(); + */ + gameGrid.showUpgrades = true; + selectedTower = tower; + } + } + + if(gameGrid.showUpgrades){ + + if(!selectedTower.isSlowTower){ + if(gameGrid.basicUpgradeCell.isCollision(x,y) && money >= selectedTower.towerUpgradeCost){ + money = money - selectedTower.towerUpgradeCost; + selectedTower.upgrade(); + gameGrid.showUpgrades = false; + } + else if(gameGrid.slowUpgradeCell.isCollision(x,y)){ + selectedTower.setSlowTower(); + gameGrid.showUpgrades = false; + } + } + else if(selectedTower.isSlowTower){ + if(gameGrid.basicUpgradeCell.isCollision(x,y)){ + selectedTower.setBasicTower(); + gameGrid.showUpgrades = false; + } + else if(gameGrid.slowUpgradeCell.isCollision(x,y) && money >= selectedTower.towerUpgradeCost){ + money = money - selectedTower.towerUpgradeCost; + selectedTower.upgrade(); + gameGrid.showUpgrades = false; + } + } + } + + for(int i = 0; true; i++){ + if(buildableCoordsX[i] == -666){ + break; + } + if(gameGrid.CELL[buildableCoordsX[i]][buildableCoordsY[i]].isCollision(x,y) && !gameGrid.CELL[buildableCoordsX[i]][buildableCoordsY[i]].isTower){ + if(money >= gameGrid.CELL[buildableCoordsX[i]][buildableCoordsY[i]].defaultTowerCost) { + int[] temp = {buildableCoordsX[i], buildableCoordsY[i]}; + gameGrid.setTowerCell(temp); + money = money - gameGrid.CELL[buildableCoordsX[i]][buildableCoordsY[i]].defaultTowerCost; + } + } + } + + } + + } + + return true; + } + + private void startNextLevel(){ + //long pauseTime = System.currentTimeMillis(); + // Pause for 5 seconds + //while(System.currentTimeMillis() - pauseTime < 5000){}; + + /* + while(killCount >= 5){ + money = money + round; + killCount = killCount - 5; + } + */ + //money = money + round; + + ++wave; + + if(wave > 6){ + //money = money + round; + + ++round; + wave = 1; + ++lives; + + defaultSpeed = defaultSpeed - 5; + + role = role + round-1; + + money = money + role; + + spawnMore = spawnNum - spawnNum/3; + + //spawnNum = defaultExtraSpawn + (round-1); + } + + ++spawnNum; + + speed = defaultSpeed - wave*wave; + + startNextLevelTask = new TimerTask() { + @Override + public void run() { + paused = false; + if (wave == 6){ + spawnMoreBosses = round -1; + createBosses(); + } else { + spawnMore = spawnNum; + createMinions(); + } + } + }; + paused = true; + timer.schedule(startNextLevelTask, 3000); + } + + /* + private void startNewGame(){ + AlertDialog.Builder builder = new AlertDialog.Builder(baseContext); + builder.setMessage("Game over, try again?"); + + builder.setNegativeButton("No", new DialogInterface.OnClickListener() { + @Override + public void onClick(DialogInterface dialogInterface, int i) { + android.os.Process.killProcess(android.os.Process.myPid()); + + dialogInterface.cancel(); + } + }); + + builder.setPositiveButton("Yes", new DialogInterface.OnClickListener() { + @Override + public void onClick(DialogInterface dialogInterface, int i) { + lives = defaultLives; + level = 0; + gameGrid = new GameGrid(numRows, numColumns, GameView.this); + + createMinions(); + + dialogInterface.cancel(); + } + }); + + AlertDialog alertDialog = builder.create(); + alertDialog.show(); + } + */ + + public void shootMinion(GridCell tower, Minion minion){ + tower.lastShot = System.currentTimeMillis(); + tower.isReloading(mainPauseTime, mainUnPauseTime); + + if(tower.isSlowTower){ + slowMinion(minion); + } + + bullets.add(createBullet(R.drawable.bullet_cc2, tower.damage, tower.centerX, tower.centerY, minion)); + + // Now called on collision with bullet + //damageMinion(minion, tower.damage); + } + + public void damageMinion(Minion minion, int damage){ + minion.health = minion.health - damage; + + if(minion.health <= 0) { + for(Bullet bullet : bullets){ + if(bullet.minion.health <= 0){ + bullets.remove(bullet); + } + } + System.out.println("Dying minion health = " + minion.health); + minions.remove(minion); + ++killCount; + + // Award for kill count + if(wave == 6){ + ++money; + } else { + while (killCount >= 5) { + ++money; + killCount = killCount - 5; + } + } + } + } + + public void slowMinion(Minion minion){ + minion.slowedTime = System.currentTimeMillis(); + minion.isSlowed = true; + } + + public void setMainPause(Boolean pause){ + mainPause = pause; + + if(mainPause){ + mainPauseTime = System.currentTimeMillis(); + } else { + mainUnPauseTime = System.currentTimeMillis(); + } + } + /* + + public void stopThreads(){ + gameLoopThread.setRunning(false); + try { + //gameLoopThread.join(); + gameLoopThread.suspend(); + } catch (Exception e) { + System.out.println(e.toString()); + e.printStackTrace(); + } + } + + public void startThreads(){ + gameLoopThread.setRunning(true); + + //gameLoopThread.start(); + } + */ + + /* + public void loadData(){ + + } + + public String saveData(){ + String save; + + save = ""; + + return save; + } + */ +} diff --git a/app/src/main/java/com/hyperling/examplegame_practice/GridCell.java b/app/src/main/java/com/hyperling/examplegame_practice/GridCell.java new file mode 100755 index 0000000..5b72bd3 --- /dev/null +++ b/app/src/main/java/com/hyperling/examplegame_practice/GridCell.java @@ -0,0 +1,271 @@ +package com.hyperling.examplegame_practice; + +import android.graphics.Color; +/** + * Created by usb on 11/9/14. + */ +public class GridCell { + int[] id; // x,y == row,column + int x,y; // coordinates + + int height; + int width; + + int side[] = new int[4]; // Left, Top, Right, Bottom + + int centerX; // center coordinates + int centerY; // center coordinates + int centerForTextX; + int centerForTextY; + + int startC = Color.GREEN; + int endC = Color.RED; + int pathC = Color.YELLOW; + int buildableC = Color.DKGRAY; + int buildableInterior = Color.CYAN; + int towerC = Color.BLUE; + int reloadingC = Color.WHITE; + int nullC = Color.TRANSPARENT; + + int color = nullC; + int innerColor = color; + + int towerType = 1; + + int defaultTowerCost = 2; + int towerCost = defaultTowerCost; + int towerUpgradeCost; + int towerChoices = 2; + + boolean isStart; + boolean isEnd; + boolean isPath; + boolean isBuildable; + boolean isTower; + boolean isSlowTower = false; + private boolean isReloading = false; + boolean isNull = true; + + long lastShot = 0; + int defaultReload = 2500; + int reload = defaultReload; + + int damage = 1; + + public GridCell(){ + findTowerCost(); + } + + public GridCell(int[] id, int width, int height){ + this.id = id; + + x = id[0] * width; + y = id[1] * height; + + this.height = height; + this.width = width; + + side[0] = x;//left + side[1] = y;//top + side[2] = x + width;//right + side[3] = y + height;//bottom + + centerX = (side[0]+side[2])/2; + centerY = (side[1]+side[3])/2; + + centerForTextX = centerX - width/24; + centerForTextY = centerY + height/16; + + findTowerCost(); + /* + if(side[0] < 0) { + side[0] = 0; + } + if(side[1] < 0) { + side[1] = 0; + } + if(side[2] > getWidth()) { + side[2] = getWidth(); + } + if(side[3] > getHeight()) { + side[3] = getHeight(); + } + */ + } + + public int getColor(){ + return color; + } + + public int[] getSides(){ + return side; + } + + private void setAllFalse(){ + isStart = false; + isEnd = false; + isPath = false; + isBuildable = false; + isTower = false; + isNull = true; + } + + public void setStart(){ + color = startC; + + setAllFalse(); + isStart = true; + } + + public void setEnd(){ + color = endC; + + setAllFalse(); + isEnd = true; + } + + public void setPath(){ + color = pathC; + + setAllFalse(); + isPath = true; + } + + public void setBuildable(){ + color = buildableC; + + setAllFalse(); + isBuildable = true; + } + + public void setTower(){ + color = buildableC; + innerColor = towerC; + + setAllFalse(); + isTower = true; + } + + public void setBasicTower(){ + isSlowTower = false; + + damage = damage * 2; + if(damage == 0) { + damage = 1; + } + } + + public void setSlowTower(){ + isSlowTower = true; + + damage = damage / 2; + } + + public void setTowerReloading(){ + innerColor = reloadingC; + + isReloading = true; + } + + public void setTowerReloaded(){ + innerColor = towerC; + + isReloading = false; + } + + public void setNull(){ + color = nullC; + + setAllFalse(); + isNull = true; + } + + public void upgrade(){ + towerCost = towerCost + towerUpgradeCost; + + ++towerType; + + //if(!isSlowTower) { + switch (towerType) { + case 2: + towerC = Color.RED; + //towerUpgradeCost = 8; + break; + case 3: + towerC = Color.MAGENTA; + //towerUpgradeCost = 16; + break; + case 4: + towerC = Color.BLACK; + break; + } + innerColor = towerC; + //} + + damage = damage * 2; + reload = defaultReload / towerType; + + findTowerCost(); + } + + public boolean isCollision(float x2, float y2){ + boolean collision = false; + + if(x2 > side[0] && x2 < side[2] && y2 > side[1] && y2 < side[3]){ + collision = true; + } + + return collision; + } + + public boolean isInRange(int x2, int y2){ + boolean inRange = false; + + if(x2 > side[0] - width && x2 < side[2] + width && y2 > side[1] - height && y2 < side[3] + height){ + inRange = true; + } + + return inRange; + } + + /* + public boolean isReloading(){ + if(System.currentTimeMillis() - lastShot > reload){ + setTowerReloaded(); + } else { + setTowerReloading(); + } + return isReloading; + } + */ + + public boolean isReloading(long pauseTime, long unPauseTime){ + if(lastShot > pauseTime) { + if (System.currentTimeMillis() - lastShot > reload) { + setTowerReloaded(); + } else { + setTowerReloading(); + } + } + else if(unPauseTime > pauseTime){ + if ((System.currentTimeMillis() - (unPauseTime - pauseTime))- lastShot > reload) { + setTowerReloaded(); + } else { + setTowerReloading(); + } + } + return isReloading; + } + + private void findTowerCost(){ + int nextTowerType = towerType + 1; + towerUpgradeCost = (defaultTowerCost * (damage * 2 ) * nextTowerType) - towerCost; + + if(isSlowTower){ + towerUpgradeCost = towerUpgradeCost*2; + } + } +} + + + diff --git a/app/src/main/java/com/hyperling/examplegame_practice/Help.java b/app/src/main/java/com/hyperling/examplegame_practice/Help.java new file mode 100755 index 0000000..9c1f018 --- /dev/null +++ b/app/src/main/java/com/hyperling/examplegame_practice/Help.java @@ -0,0 +1,29 @@ +package com.hyperling.examplegame_practice; + +import android.app.Activity; +import android.content.Intent; +import android.os.Bundle; +import android.view.View; +import android.widget.Button; + +/** + * Created by usb on 12/20/14. + */ +public class Help extends Activity{ + + Button btnBTS; + + public void onCreate(Bundle savedInstanceState){ + super.onCreate(savedInstanceState); + setContentView(R.layout.activity_help); + + btnBTS = (Button) findViewById(R.id.btnBackToStart); + + btnBTS.setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View view) { + startActivity(new Intent(Help.this, MainActivity.class)); + } + }); + } +} diff --git a/app/src/main/java/com/hyperling/examplegame_practice/MainActivity.java b/app/src/main/java/com/hyperling/examplegame_practice/MainActivity.java new file mode 100755 index 0000000..2bee84a --- /dev/null +++ b/app/src/main/java/com/hyperling/examplegame_practice/MainActivity.java @@ -0,0 +1,114 @@ +package com.hyperling.examplegame_practice; + +import android.app.Activity; +import android.content.Intent; +import android.os.Bundle; +import android.view.View; +import android.widget.Button; + + +public class MainActivity extends Activity { + + public Game game; + + Button btnStart, btnContinue, btnHelp; + + @Override + protected void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + setContentView(R.layout.activity_main); + + // Deletes any old save files + //getBaseContext().deleteFile("savedata"); + + btnStart = (Button) findViewById(R.id.btnStart); + btnContinue = (Button) findViewById(R.id.btnContinue); + btnHelp = (Button) findViewById(R.id.btnHelp); + + + btnStart.setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View view) { + game = new Game(false); + startActivity(new Intent(MainActivity.this, game.getClass())); + } + }); + + btnContinue.setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View view) { + game = new Game(true); + startActivity(new Intent(MainActivity.this, game.getClass())); + } + }); + + btnHelp.setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View view) { + startActivity(new Intent(MainActivity.this, Help.class)); + } + }); + } + + + /* + protected void onPause(){ + super.onPause(); + + // Stop threads + gameview.stopThreads(); + } + + protected void onResume(){ + super.onResume(); + + // Restore previous session + //setContentView(gameview); + + //gameview.startThreads(); + } + */ + + /* + @Override + public boolean onCreateOptionsMenu(Menu menu) { + // Inflate the menu; this adds items to the action bar if it is present. + getMenuInflater().inflate(R.menu.main, menu); + return true; + } + + @Override + public boolean onOptionsItemSelected(MenuItem item) { + // Handle action bar item clicks here. The action bar will + // automatically handle clicks on the Home/Up button, so long + // as you specify a parent activity in AndroidManifest.xml. + int id = item.getItemId(); + if (id == R.id.action_reset) { + gameview = new GameView(MainActivity.this); + setContentView(gameview); + return true; + } + else if (id == R.id.action_pause) { + mainPause = !mainPause; + if(mainPause){ + //gameview.stopThreads(); + item.setTitle("Resume"); + } + else{ + //gameview.startThreads(); + item.setTitle("Pause"); + } + try { + gameview.setMainPause(mainPause); + } + catch(Exception e){ + e.printStackTrace(); + System.out.println(e.toString()); + System.out.println(mainPause); + } + return true; + } + return super.onOptionsItemSelected(item); + } + */ +} diff --git a/app/src/main/java/com/hyperling/examplegame_practice/Minion.java b/app/src/main/java/com/hyperling/examplegame_practice/Minion.java new file mode 100755 index 0000000..ea09f5b --- /dev/null +++ b/app/src/main/java/com/hyperling/examplegame_practice/Minion.java @@ -0,0 +1,207 @@ +package com.hyperling.examplegame_practice; + +import android.graphics.Bitmap; +import android.graphics.Canvas; +import android.graphics.Rect; + +/** + * Created by usb on 10/23/14. + */ +public class Minion { + + // How many rows and columns? Must find the little guy + private static final int BMP_ROWS = 4; + private static final int BMP_COLUMNS = 3; + + // Direction the guy moves according to quadrants + String directions[] = {"down", "left", "right", "up"}; + + // Default speed + private final int SPEED_PER_STEP; + + // Variables + int x, y, xSpeed, ySpeed, currentFrame, width, height; + + int centerX, centerY; + + GameView gameView; + + Bitmap bmp; + + //int role; //determines speed and health + int health; + + long slowedTime; + boolean isSlowed; + int slow = 1; + + public Minion(GameView gv, Bitmap bm, int role, int speed, int startX, int startY){ + gameView = gv; + bmp = bm; + health = role; + this.SPEED_PER_STEP = speed; + + xSpeed = SPEED_PER_STEP; + ySpeed = 0; + + //Find size of individual character, not entire bitmap + width = bmp.getWidth() / BMP_COLUMNS; + height = bmp.getHeight() / BMP_ROWS; + + x = startX; + y = startY - (height/2); + } + + private void update(){ + /* + //Turn around if hitting x walls + if(x > gameView.getWidth() - width - xSpeed){ + xSpeed = -xSpeed; + } + if(x + xSpeed < 0){ + xSpeed = Math.abs(xSpeed); + } + */ + int movement; + + if(isSlowed){ + slow = 3; + } + + if(xSpeed > 0) { + movement = (gameView.getWidth() / (xSpeed)); + } else { + movement = 0; + } + x = x + movement/slow; + centerX = x + width/2; + + + /* + //Turn around if hitting y walls + if(y > gameView.getWidth() - height - ySpeed){ + ySpeed = -ySpeed; + } + if(y + ySpeed < 0){ + ySpeed = Math.abs(ySpeed); + } + */ + + if(ySpeed > 0) { + movement = (gameView.getHeight() / ySpeed); + } else { + movement = 0; + } + y = y + movement/slow; + centerY = y + height/2; + + // Make him look like he's walking + // %BMP_COLUMNS makes it stay withing bounds + currentFrame = ++currentFrame % BMP_COLUMNS; + + if(isSlowed){ + if(System.currentTimeMillis() - slowedTime > 1000){ + isSlowed = false; + slow = 1; + } + } + } + + public void onDraw(Canvas canvas) { + + // Move the guy + update(); + + int srcX = currentFrame * width; + int srcY = getAnimationRow() * height; + + Rect src = new Rect(srcX, srcY, srcX + width, srcY + height); + + Rect dest = new Rect(x, y, x + width, y + height); + + canvas.drawBitmap(bmp, src, dest, null); + } + + private int getAnimationRow() { + + String dir = ""; + String xdir = ""; + String ydir = ""; + int i; + + // If stationary, look down + if(xSpeed == 0 && ySpeed == 0){ + dir = "down"; + } + else { + // Find x direction + if(xSpeed > 0) { + xdir = "right"; + } else { + xdir = "left"; + } + + // Find y direction + if(ySpeed > 0){ + ydir = "down"; + } + else{ + ydir = "up"; + } + + // Use whichever is stronger + if(Math.abs(xSpeed) > Math.abs(ySpeed)){ + dir = xdir; + } + else{ + dir = ydir; + } + } + + // Match i for the direction + for(i = 0; i < 4; i++){ + //System.out.println("dir is " + dir); + //System.out.println("x + xSpeed are " + (x+xSpeed)); + //System.out.println("i says " + directions[i]); + if(directions[i].equals(dir)){ + break; + } + } + //System.out.println(i); + return i; + } + + public boolean isCollision(int x2, int y2) { + + return x2 > x && x2 < x + width && y2 > y && y2 < y + height; + + } + + public boolean isCollision(int[] side) { + boolean isInside = false; + + if(x > side[0] && x < side[2] && y > side[1] && y < side[3]){ + isInside = true; + } + + return isInside; + //return x2 > x && x2 < x + width && y2 > y && y2 < y + height; + + } + + /* + public int getRole() { + + return role; + } + */ + + public int[] getCellLocation(int numRows, int numColumns){ + int i = gameView.getHeight()/numRows; + int j = gameView.getWidth()/numColumns; + + int[] a = {i,j}; + return a; + } + +} diff --git a/app/src/main/java/com/hyperling/examplegame_practice/SAL.java b/app/src/main/java/com/hyperling/examplegame_practice/SAL.java new file mode 100755 index 0000000..f6a102b --- /dev/null +++ b/app/src/main/java/com/hyperling/examplegame_practice/SAL.java @@ -0,0 +1,78 @@ +package com.hyperling.examplegame_practice; + +import android.content.Context; + +import java.io.File; +import java.io.FileInputStream; +import java.io.FileOutputStream; +import java.io.ObjectInputStream; +import java.io.ObjectOutputStream; + +/** + * Created by usb on 12/12/14. + */ +// Save and Load +public class SAL { + + //GameView gv; + String filename; + + public SAL(String fn){ + //gv = gameView; + filename = fn; + } + + /* + public SAL(GameView gameView, String fn){ + gv = gameView; + filename = fn; + } + */ + + public void save(GameView gv){ + + try{ + FileOutputStream fileOutputStream = new FileOutputStream(filename); + ObjectOutputStream objectOutputStream = new ObjectOutputStream(fileOutputStream); + objectOutputStream.writeObject(gv); + objectOutputStream.close(); + } + catch(Exception e){ + System.out.println(e.toString()); + e.printStackTrace(); + System.out.println(e.toString()); + } + } + + public void load(GameView gv){ + + try{ + FileInputStream fileInputStream = new FileInputStream(filename); + ObjectInputStream objectInputStream = new ObjectInputStream(fileInputStream); + gv = (GameView) objectInputStream.readObject(); + objectInputStream.close(); + } + catch(Exception e){ + System.out.println(e.toString()); + e.printStackTrace(); + System.out.println(e.toString()); + } + + //return gv; + } + + /* + public boolean check(){ + boolean fileExists = false; + + try + { + File file = new File(filename); + fileExists = file.exists(); + } + catch(Exception e){System.out.println("Error checking in file exists in SAL");} + + return fileExists; + } + */ +} diff --git a/app/src/main/res/drawable-hdpi/bullet_2.png b/app/src/main/res/drawable-hdpi/bullet_2.png new file mode 100755 index 0000000..e3b0fdf Binary files /dev/null and b/app/src/main/res/drawable-hdpi/bullet_2.png differ diff --git a/app/src/main/res/drawable-hdpi/bullet_3.png b/app/src/main/res/drawable-hdpi/bullet_3.png new file mode 100755 index 0000000..240b05d Binary files /dev/null and b/app/src/main/res/drawable-hdpi/bullet_3.png differ diff --git a/app/src/main/res/drawable-hdpi/bullet_cc.png b/app/src/main/res/drawable-hdpi/bullet_cc.png new file mode 100755 index 0000000..bfb920c Binary files /dev/null and b/app/src/main/res/drawable-hdpi/bullet_cc.png differ diff --git a/app/src/main/res/drawable-hdpi/bullet_cc2.png b/app/src/main/res/drawable-hdpi/bullet_cc2.png new file mode 100755 index 0000000..105daa5 Binary files /dev/null and b/app/src/main/res/drawable-hdpi/bullet_cc2.png differ diff --git a/app/src/main/res/drawable-hdpi/bullet_cc2_100x100.png b/app/src/main/res/drawable-hdpi/bullet_cc2_100x100.png new file mode 100755 index 0000000..26272ad Binary files /dev/null and b/app/src/main/res/drawable-hdpi/bullet_cc2_100x100.png differ diff --git a/app/src/main/res/drawable-hdpi/bullet_default.png b/app/src/main/res/drawable-hdpi/bullet_default.png new file mode 100755 index 0000000..94a0676 Binary files /dev/null and b/app/src/main/res/drawable-hdpi/bullet_default.png differ diff --git a/app/src/main/res/drawable-hdpi/cctd_image.png b/app/src/main/res/drawable-hdpi/cctd_image.png new file mode 100755 index 0000000..c003082 Binary files /dev/null and b/app/src/main/res/drawable-hdpi/cctd_image.png differ diff --git a/app/src/main/res/drawable-hdpi/demon_king.png b/app/src/main/res/drawable-hdpi/demon_king.png new file mode 100755 index 0000000..6158251 Binary files /dev/null and b/app/src/main/res/drawable-hdpi/demon_king.png differ diff --git a/app/src/main/res/drawable-hdpi/ic_launcher.png b/app/src/main/res/drawable-hdpi/ic_launcher.png new file mode 100755 index 0000000..96a442e Binary files /dev/null and b/app/src/main/res/drawable-hdpi/ic_launcher.png differ diff --git a/app/src/main/res/drawable-hdpi/sprite_green01.png b/app/src/main/res/drawable-hdpi/sprite_green01.png new file mode 100755 index 0000000..a1dda2c Binary files /dev/null and b/app/src/main/res/drawable-hdpi/sprite_green01.png differ diff --git a/app/src/main/res/drawable-hdpi/startmenu_20141221_1117.png b/app/src/main/res/drawable-hdpi/startmenu_20141221_1117.png new file mode 100755 index 0000000..b7a3594 Binary files /dev/null and b/app/src/main/res/drawable-hdpi/startmenu_20141221_1117.png differ diff --git a/app/src/main/res/drawable-hdpi/startmenu_button_20141221_1148.png b/app/src/main/res/drawable-hdpi/startmenu_button_20141221_1148.png new file mode 100755 index 0000000..6255341 Binary files /dev/null and b/app/src/main/res/drawable-hdpi/startmenu_button_20141221_1148.png differ diff --git a/app/src/main/res/drawable-hdpi/startmenu_empty_20141221_1148.png b/app/src/main/res/drawable-hdpi/startmenu_empty_20141221_1148.png new file mode 100755 index 0000000..6ee8bc2 Binary files /dev/null and b/app/src/main/res/drawable-hdpi/startmenu_empty_20141221_1148.png differ diff --git a/app/src/main/res/drawable-mdpi/ic_launcher.png b/app/src/main/res/drawable-mdpi/ic_launcher.png new file mode 100755 index 0000000..359047d Binary files /dev/null and b/app/src/main/res/drawable-mdpi/ic_launcher.png differ diff --git a/app/src/main/res/drawable-xhdpi/ic_launcher.png b/app/src/main/res/drawable-xhdpi/ic_launcher.png new file mode 100755 index 0000000..71c6d76 Binary files /dev/null and b/app/src/main/res/drawable-xhdpi/ic_launcher.png differ diff --git a/app/src/main/res/drawable-xxhdpi/ic_launcher.png b/app/src/main/res/drawable-xxhdpi/ic_launcher.png new file mode 100755 index 0000000..4df1894 Binary files /dev/null and b/app/src/main/res/drawable-xxhdpi/ic_launcher.png differ diff --git a/app/src/main/res/layout/activity_help.xml b/app/src/main/res/layout/activity_help.xml new file mode 100755 index 0000000..906496f --- /dev/null +++ b/app/src/main/res/layout/activity_help.xml @@ -0,0 +1,41 @@ + + + +