Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.donthavedomainyet.anddontwanttousemyemail.apps.game1.game;
- import com.donthavedomainyet.anddontwanttousemyemail.apps.game1.*;
- import com.donthavedomainyet.anddontwanttousemyemail.apps.game1.annotation.*;
- import com.donthavedomainyet.anddontwanttousemyemail.apps.game1.game.entity.*;
- import com.donthavedomainyet.anddontwanttousemyemail.apps.game1.game.entity.boss.*;
- import com.donthavedomainyet.anddontwanttousemyemail.apps.game1.game.position.*;
- import android.app.Activity;
- import android.content.*;
- import android.content.res.Configuration;
- import android.graphics.*;
- import android.preference.PreferenceManager;
- import android.util.*;
- import android.view.*;
- import java.util.*;
- import com.donthavedomainyet.anddontwanttousemyemail.apps.game1.MainActivity;
- import com.donthavedomainyet.anddontwanttousemyemail.apps.game1.game.position.Point;
- public class MainGamePanel extends SurfaceView implements SurfaceHolder.Callback
- {
- private static final String TAG = MainGamePanel.class.getSimpleName();
- private static final boolean debug = BuildConfig.DEBUG;
- private Context context;
- private MainActivity main;
- private Explosion[] explosions;
- private int numberDroids;
- private long rounds = 0L;
- private Random rand;
- private GameThread thread;
- private static List<Droid> droidList;
- private static List<Obstacle> obstacleList;
- private static List<Heal> healList;
- private static List<Spike> corners;
- private static List<DamageBoost> dmgList;
- private static List<AdvHeal> advHealList;
- private static List<Shield> shieldList;
- private String avgFps = "";
- private Boolean showRange;
- private static Player player;
- private int rd = 1;
- private final int healChance;
- private boolean gameOver = false;
- private final int maxheals = 1;
- private final float droidSpeedX, droidSpeedY;
- private final int healthDroids;
- private boolean bossRound = false;
- private int remainingDmg = 0;
- private int remainingHeal = 0;
- private int healAmount = 0;
- private int baseRange = 0;
- private int toAddRange = 0;
- private int bossSize = 0;
- private int baseSize = 0;
- private int droidSize = 0;
- private Bitmap healBmp;
- private Bitmap dmgBmp;
- private Bitmap boss1Bmp;
- private Bitmap advHealBmp;
- private Bitmap shieldBmp;
- private Bitmap droidBmp;
- private int bossNumber = 1;
- private int reasonGameOver = 0;
- private String[] difficulties = {"easy", "medium", "hard", "extreme"};
- private String difficulty;
- private int width = 0, height = 0;
- private double ratio;
- @Working
- public MainGamePanel(Context c, MainActivity main, int max_fps, int max_frameskips, float droidSpeedX, float droidSpeedY, int droids, boolean showRange, int healthDroids)
- {
- super(c);
- context = c;
- this.main = main;
- numberDroids = droids;
- SharedPreferences prefs = c.getSharedPreferences("videoPerformance", Context.MODE_PRIVATE);
- SharedPreferences defaultPrefs = PreferenceManager.getDefaultSharedPreferences(context);
- width = defaultPrefs.getInt("width", 540);
- height = defaultPrefs.getInt("height", 960);
- Log.d(TAG, "Width = " + width + ", height = " + height);
- final int orientation = prefs.getInt("orientation", 0);
- switch(orientation)
- {
- case Configuration.ORIENTATION_LANDSCAPE:
- if(width < height)
- {
- int a = width;
- width = height;
- height = a;
- }
- break;
- case Configuration.ORIENTATION_PORTRAIT:
- if(height < width)
- {
- int a = width;
- width = height;
- height = a;
- }
- break;
- }
- if(height > width)
- {
- this.ratio = height / width;
- Player.range = (int) (height / 4.8);
- baseSize = (int) (height / 32);
- bossSize = (int) (height / 12);
- toAddRange = (int) (height / 12.8);
- baseRange = (int) (height / 38.4);
- droidSize = (int) (height / 96);
- }
- else
- {
- this.ratio = width / height;
- Player.range = (int) (width / 4.8);
- baseSize = (int) (width / 32);
- bossSize = (int) (width / 12);
- toAddRange = (int) (width / 12.8);
- baseRange = (int) (width / 38.4);
- droidSize = (int) (width / 96);
- }
- healChance = prefs.getInt("healChance", 3000);
- this.droidSpeedX = droidSpeedX;
- this.droidSpeedY = droidSpeedY;
- this.healthDroids = healthDroids;
- this.width = width;
- this.height = height;
- difficulty = difficulties[PreferenceManager.getDefaultSharedPreferences(context).getInt("difficulty", 0)];
- SharedPreferences playerData = c.getSharedPreferences("pdata", Context.MODE_PRIVATE);
- final int health = playerData.getInt("health", 100);
- final long xp = playerData.getLong("xp", 0);
- player = new Player(BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher), width / 2, height / 2, health, xp);
- this.showRange = showRange;
- Bitmap hBmp = BitmapFactory.decodeResource(getResources(), R.drawable.heal_1);
- healBmp = Bitmap.createScaledBitmap(hBmp, baseSize, baseSize, false);
- Bitmap b1Bmp = BitmapFactory.decodeResource(getResources(), R.drawable.droid_1);
- droidBmp = Bitmap.createScaledBitmap(b1Bmp, droidSize, droidSize, false);
- boss1Bmp = Bitmap.createScaledBitmap(b1Bmp, bossSize, bossSize, false);
- Bitmap dBmp = BitmapFactory.decodeResource(getResources(), R.drawable.dmg_1);
- dmgBmp = Bitmap.createScaledBitmap(dBmp, baseSize, baseSize, false);
- Bitmap sBmp = BitmapFactory.decodeResource(getResources(), R.drawable.shield_1);
- shieldBmp = Bitmap.createScaledBitmap(sBmp, baseSize, baseSize, false);
- Matrix matrix = new Matrix();
- matrix.postRotate(180);
- advHealBmp = Bitmap.createBitmap(healBmp, 0, 0, healBmp.getWidth(), healBmp.getHeight(), matrix, true);
- droidList = new ArrayList<Droid>();
- obstacleList = new ArrayList<Obstacle>();
- healList = new ArrayList<Heal>();
- corners = new ArrayList<Spike>();
- dmgList = new ArrayList<DamageBoost>();
- advHealList = new ArrayList<AdvHeal>();
- shieldList = new ArrayList<Shield>();
- Random r = new Random();
- rand = new Random(r.nextInt(99999));
- //obstacleList.add(new Obstacle(BitmapFactory.decodeResource(getResources(), R.drawable.stone_1), r.nextInt(width / 2), r.nextInt(height / 2)));
- for(int i = 0; i < droids; i++)
- {
- droidList.add(new Droid(droidBmp, r.nextInt(width), r.nextInt(height)));
- }
- for(Droid droid : droidList)
- {
- droid.changeSpeed((droidSpeedX * r.nextInt(5)) / 3 + 1, (droidSpeedY * r.nextInt(5)) / 3 + 1);
- droid.setHealth(healthDroids);
- droid.setRangeAndDamage(this.baseRange, this.toAddRange);
- }
- int[] widths = {0, width, 0, width};
- int[] heights = {0, 0, height, height};
- for(int i = 0; i < 4; i++)
- {
- corners.add(new Spike(BitmapFactory.decodeResource(getResources(), R.drawable.stone_1), widths[i], heights[i]));
- }
- getHolder().addCallback(this);
- thread = new GameThread(getHolder(), this, max_fps, max_frameskips);
- setFocusable(true);
- thread.setRunning(true);
- thread.setGameRunning(true);
- thread.start();
- }
- @Working
- public GameThread getGameThread()
- {
- return thread;
- }
- @Working
- @Override
- public void surfaceCreated(SurfaceHolder p1)
- {
- thread.setGameRunning(true);
- }
- @Untested
- @Override
- public void surfaceChanged(SurfaceHolder p1, int p2, int p3, int p4)
- {
- // TODO: Implement this method
- }
- @Working
- @Override
- public void surfaceDestroyed(SurfaceHolder p1)
- {
- thread.setGameRunning(false);
- }
- @Working
- public void setAvgFps(String avgFps)
- {
- this.avgFps = avgFps;
- }
- @Working
- public void render(Canvas canvas)
- {
- onDraw(canvas);
- }
- @Working
- @Override
- protected void onDraw(Canvas canvas)
- {
- Paint paint = new Paint();
- float defSize = paint.getTextSize();
- paint.setARGB(255, 255, 255, 255);
- if(remainingDmg > 0) remainingDmg--;
- if(remainingDmg == 0) player.resetDmg();
- if(remainingHeal > 0)
- {
- player.healInt(healAmount);
- remainingHeal--;
- }
- for(Spike spike : corners)
- {
- spike.draw(canvas);
- }
- canvas.drawColor(Color.BLACK);
- if(!gameOver)
- {
- for(Shield shield : shieldList)
- {
- if(player.getHitbox().isCircleIntersectingWithCircle(shield.getHitbox()))
- {
- player.addShield(shield.getDurability());
- shieldList.remove(shield);
- }
- else shield.draw(canvas);
- }
- for(DamageBoost dmg : dmgList)
- {
- if(player.getHitbox().isCircleIntersectingWithCircle(dmg.getHitbox()))
- {
- player.getWeapon().setDamage(dmg.getDamage());
- remainingDmg = dmg.getDuration();
- dmgList.remove(dmg);
- }
- else dmg.draw(canvas);
- }
- for(AdvHeal heal : advHealList)
- {
- if(player.getHitbox().isCircleIntersectingWithCircle(heal.getHitbox()))
- {
- remainingHeal = heal.getDuration();
- healAmount = heal.getHealValue();
- advHealList.remove(heal);
- }
- else heal.draw(canvas);
- }
- for(Heal heal : healList)
- {
- if(player.getHitbox().isCircleIntersectingWithCircle(heal.getHitbox()))
- {
- player.heal(heal.getHeal());
- healList.remove(heal);
- }
- else heal.draw(canvas);
- }
- for(Obstacle obstacle : obstacleList)
- {
- obstacle.draw(canvas, this);
- }
- for(Droid droid : droidList)
- {
- if(droid instanceof Boss)
- {
- droid = (Boss) droid;
- bossRound = true;
- }
- if(droid instanceof Boss1) droid = (Boss1) droid;
- if(!droid.getDead())
- {
- droid.draw(canvas);
- if(showRange)
- {
- int colorMode = 0;
- for(Obstacle obstacle : obstacleList)
- {
- if(droid.getAttackArea().isPointInCircle(obstacle.getPosition())) colorMode = 1;
- }
- for(Droid droid2 : droidList)
- {
- if(droid.getAttackArea().isCircleIntersectingWithCircle(droid2.getAttackArea()))
- {
- if(!(droid2.getId() == droid.getId())) colorMode = 3;
- }
- if(droid.getAttackArea().isPointInCircle(droid2.getPosition()))
- {
- if(!(droid2.getId() == droid.getId()))
- {
- colorMode = 2;
- droid.attack(canvas, droid2);
- }
- }
- }
- if(droid.getAttackArea().isCircleIntersectingWithCircle(player.getAttackArea())) colorMode = 3;
- if(droid.getAttackArea().isPointInCircle(player.getPosition()))
- {
- colorMode = 2;
- droid.attack(canvas, player);
- }
- droid.drawRange(canvas, this, colorMode);
- }
- else
- {
- for(Droid droid2 : droidList)
- {
- if(droid.getAttackArea().isPointInCircle(droid2.getPosition()))
- {
- if(!(droid2.getId() == droid.getId()))
- {
- droid.attack(canvas, droid2);
- }
- }
- }
- if(droid.getAttackArea().isPointInCircle(player.getPosition())) droid.attack(canvas, player);
- }
- }
- else
- {
- droidList.remove(droid);
- }
- }
- player.draw(canvas);
- paint.setTextSize(defSize * 3);
- if(remainingDmg > 0) canvas.drawText(remainingDmg + "", player.getPosition().getX() - (player.getBitmap().getWidth() / 2), player.getPosition().getY() + (player.getBitmap().getHeight() / 2), paint);
- if(showRange)
- {
- int colorMode = 0;
- for(Obstacle obstacle : obstacleList)
- {
- if(player.getAttackArea().isPointInCircle(obstacle.getPosition())) colorMode = 1;
- }
- for(Droid droid : droidList)
- {
- if(player.getAttackArea().isCircleIntersectingWithCircle(droid.getAttackArea())) colorMode = 3;
- if(player.getAttackArea().isPointInCircle(droid.getPosition()))
- {
- colorMode = 2;
- player.attack(canvas, droid);
- }
- }
- player.drawRange(canvas, this, colorMode);
- }
- else
- {
- for(Droid droid : droidList)
- {
- if(player.getAttackArea().isPointInCircle(droid.getPosition())) player.attack(canvas, droid);
- }
- }
- displayFps(canvas, avgFps);
- paint.setTextSize(defSize * 2.5f);
- canvas.drawText("Round " + rounds, 20, 25, paint);
- }
- else
- {
- String gameOverMsg = " ";
- switch(reasonGameOver)
- {
- case 1:
- main.onLose(rounds, difficulty);
- break;
- case 2:
- gameOverMsg = "Carregando round" + rounds ;
- if(numberDroids < 30) numberDroids++;
- if((int)(rounds) % 30 != 0)
- {
- droidList.clear();
- for(int i = 0; i < numberDroids; i++)
- {
- droidList.add(new Droid(droidBmp, rand.nextInt(width), rand.nextInt(height)));
- }
- for(Droid droid : droidList)
- {
- droid.changeSpeed((droidSpeedX * rand.nextInt(5)) / 3 + 1, (droidSpeedY * rand.nextInt(5)) / 3 + 1);
- droid.setHealth(healthDroids);
- droid.setRangeAndDamage(this.baseRange, this.toAddRange);
- }
- }
- else
- {
- droidList.clear();
- droidList.add(new Boss1(boss1Bmp, width / 2, height / 2));
- for(Droid droid : droidList)
- {
- droid.changeSpeed((droidSpeedX * rand.nextInt(5)) / 3 + 1, (droidSpeedY * rand.nextInt(5)) / 3 + 1);
- droid.setRangeAndDamage(this.baseRange, this.toAddRange);
- droid.setHealth((50000 * bossNumber) + 100000);
- }
- bossNumber++;
- }
- gameOver = false;
- break;
- default:
- gameOverMsg = "erro";
- }
- /*for(Explosion e : explosions)
- {
- for(Particle p : e.getParticles())
- {
- p.draw(canvas);
- }
- }*/
- paint.setTextSize(defSize * 8);
- canvas.drawText(gameOverMsg, 200, 500, paint);
- }
- }
- @Working
- public void drawCircle(Canvas canvas, Paint.Style style, int cx, int cy, float radius, int alpha, int red, int green, int blue)
- {
- Paint paint = new Paint();
- paint.setStyle(style);
- paint.setARGB(alpha, red, green, blue);
- canvas.drawCircle(cx, cy, radius, paint);
- }
- @Working
- public void displayFps(Canvas canvas, String fps)
- {
- try
- {
- if(canvas != null && fps != null)
- {
- drawText(canvas, fps, 2.5f, this.getWidth() - 100, 25, 255, 255, 255, 255);
- }
- }
- catch(Exception e) {}
- }
- @Working
- public void drawText(Canvas canvas, String text, float textSize, int x, int y, int alpha, int red, int green, int blue)
- {
- try
- {
- if(canvas != null && text != null)
- {
- Paint paint = new Paint();
- paint.setARGB(alpha, red, green, blue);
- paint.setTextSize(paint.getTextSize() * textSize);
- canvas.drawText(text, x, y, paint);
- }
- }
- catch(Exception e) {}
- }
- @Working
- @Override
- public boolean onTouchEvent(MotionEvent event)
- {
- if(event.getAction() == MotionEvent.ACTION_DOWN)
- {
- // delegating event handling to the droid
- /*int currentExplosion = 0;
- Explosion explosion = explosions[currentExplosion];
- while (explosion != null && explosion.isAlive() && currentExplosion < explosions.length - 1)
- {
- currentExplosion++;
- explosion = explosions[currentExplosion];
- }
- if (explosion == null || explosion.isDead())
- {
- explosion = new Explosion(100, (int)event.getX(), (int)event.getY());
- explosions[currentExplosion] = explosion;
- }*/
- player.handleActionDown((int) event.getX(), (int) event.getY());
- // check if in the lower part of the screen we exit
- if(debug)
- {
- Log.d(TAG, "Coords: {x = " + event.getX() + ", y = " + event.getY() + "}");
- }
- }
- if (event.getAction() == MotionEvent.ACTION_MOVE)
- {
- // the gestures
- if (player.isTouched())
- {
- // the droid was picked up and is being dragged
- player.setX((int)event.getX());
- player.setY((int)event.getY());
- }
- }
- if (event.getAction() == MotionEvent.ACTION_UP)
- {
- // touch was released
- if(player.isTouched())
- {
- player.setTouched(false);
- }
- }
- return true;
- }
- public static void droidDead()
- {
- //Log.i(MainGamePanel.class.getSimpleName(), "Adding 10 currency");
- player.addCurrency(10);
- }
- @Working
- public void update()
- {
- if(bossRound)
- {
- int y = rand.nextInt(healChance);
- if(y == 0) if(dmgList.size() < 1) dmgList.add(new DamageBoost(dmgBmp, rand.nextInt(width), rand.nextInt(height)));
- int z = rand.nextInt(healChance);
- if(z == 0) if(healList.size() < 1) healList.add(new Heal(healBmp, rand.nextInt(width), rand.nextInt(height)));
- int a = rand.nextInt(healChance);
- if(a == 0) if(shieldList.size() < 1) shieldList.add(new Shield(shieldBmp, rand.nextInt(width), rand.nextInt(height)));
- }
- int x = rand.nextInt(healChance);
- if(x == 0) if(advHealList.size() < maxheals) advHealList.add(new AdvHeal(advHealBmp, rand.nextInt(width), rand.nextInt(height)));
- for(Droid droid : droidList)
- {
- if(!droid.getDead())
- {
- if (droid.getSpeed().getxDirection() == Speed.DIRECTION_RIGHT && droid.getX() + droid.getBitmap().getWidth() / 2 >= getWidth())
- {
- droid.getSpeed().toggleXDirection();
- }
- // check collision with left wall if heading left
- if (droid.getSpeed().getxDirection() == Speed.DIRECTION_LEFT && droid.getX() - droid.getBitmap().getWidth() / 2 <= 0)
- {
- droid.getSpeed().toggleXDirection();
- }
- // check collision with bottom wall if heading down
- if (droid.getSpeed().getyDirection() == Speed.DIRECTION_DOWN && droid.getY() + droid.getBitmap().getHeight() / 2 >= getHeight())
- {
- droid.getSpeed().toggleYDirection();
- }
- // check collision with top wall if heading up
- if (droid.getSpeed().getyDirection() == Speed.DIRECTION_UP && droid.getY() - droid.getBitmap().getHeight() / 2 <= 0)
- {
- droid.getSpeed().toggleYDirection();
- }
- // Update the lone droid
- droid.update();
- }
- else
- {
- droidList.remove(droid);
- }
- }
- if(droidList.isEmpty())
- {
- main.saveCurrency(player.getCurrency());
- player.setCurrency(0);
- if(bossRound) player.heal(1);
- bossRound = false;
- rounds++;
- if(rounds == Long.MAX_VALUE) thread.setRunning(false);
- reasonGameOver = 2;
- gameOver = true;
- }
- if(!player.getDead())
- {
- player.update();
- }
- else
- {
- //thread.setGameRunning(false);
- reasonGameOver = 1;
- gameOver = true;
- player.die();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement