Added:
trunk/src/android/com/abb/Weapon.java
Modified:
trunk/src/android/com/abb/Avatar.java
trunk/src/android/com/abb/GameState.java
Log:
The weapons are ugly, and still need work for parameters, but they display.
Modified: trunk/src/android/com/abb/Avatar.java
==============================================================================
--- trunk/src/android/com/abb/Avatar.java (original)
+++ trunk/src/android/com/abb/Avatar.java Thu Dec 18 20:50:17 2008
@@ -17,6 +17,7 @@
import android.com.abb.ArticulatedEntity;
import android.com.abb.GameState;
+import android.com.abb.Weapon;
public class Avatar extends ArticulatedEntity {
@@ -29,6 +30,8 @@
public void step(float time_step) {
super.step(time_step);
+ mWeapon.x = x;
+ mWeapon.y = y;
ddy = kGravity;
// Update the horizontal acceleration acceleration according to the
current
@@ -75,6 +78,7 @@
setSprite(sprite_index, facing_left_);
*/
+ mWeapon.setSprite(facing_left_);
// Update the shooting mechanism. The choices for shot direction are
// specialized for each animation case: in the air, facing left,
right, and
@@ -131,12 +135,20 @@
shooting_ = (state == 1);
}
+ private void setSprite(int index, boolean facing_left) {
+ // Set up the sprite drawing parameters within our *parent* Entity
class.
+ sprite_rect.top = kSpriteSize * index;
+ sprite_rect.bottom = kSpriteSize * index + kSpriteSize;
+ sprite_flipped_horizontal = facing_left;
+ }
+
private float animation_phase_ = 0.0f;
private boolean facing_left_;
private GameState game_state_;
private boolean shooting_;
private float shot_delay_;
private float shot_phase_;
+ public Weapon mWeapon = new Weapon(game_state_);
private static final float kAirAcceleration = 40.0f;
private static final float kAirAnimationSpeed = 3.0f;
Modified: trunk/src/android/com/abb/GameState.java
==============================================================================
--- trunk/src/android/com/abb/GameState.java (original)
+++ trunk/src/android/com/abb/GameState.java Thu Dec 18 20:50:17 2008
@@ -1,4 +1,4 @@
-// Copyright 2008 and onwards Matthew Burkhart.
+// Copyright 2008 and onwards Matthew Burkhart and Matt Barnes.
//
// This program is free software; you can redistribute it and/or modify it
under
// the terms of the GNU General Public License as published by the Free
Software
@@ -33,7 +33,7 @@
import android.com.abb.Game;
import android.com.abb.Graphics;
import android.com.abb.Map;
-
+import android.com.abb.Weapon;
public class GameState implements Game {
public Avatar avatar = new Avatar(this);
@@ -112,6 +112,7 @@
// Step the avatar.
if (avatar.alive) {
avatar.step(time_step);
+ avatar.mWeapon.step(time_step);
map.collideEntity(avatar);
if (Map.tileIsGoal(map.tileAt(avatar.x, avatar.y))) {
map.advanceLevel();
@@ -177,10 +178,11 @@
for (Iterator it = enemies.iterator(); it.hasNext();)
((Entity)it.next()).draw(graphics, view_x_, view_y_, zoom_);
- // Draw the avatar.
+ // Draw the avatar and weapon.
if (avatar.alive)
avatar.draw(graphics, view_x_, view_y_, zoom_);
-
+ avatar.mWeapon.setImage(graphics);
+ avatar.mWeapon.draw(graphics, view_x_, view_y_, zoom_);
// Draw the projectiles.
for (Iterator it = projectiles.iterator(); it.hasNext();)
((Entity)it.next()).draw(graphics, view_x_, view_y_, zoom_);
Added: trunk/src/android/com/abb/Weapon.java
==============================================================================
--- (empty file)
+++ trunk/src/android/com/abb/Weapon.java Thu Dec 18 20:50:17 2008
@@ -0,0 +1,105 @@
+// Copyright 2008 and onwards Matthew Barnes.
+//
+// This program is free software; you can redistribute it and/or modify it
under
+// the terms of the GNU General Public License as published by the Free
Software
+// Foundation; version 3 of the License.
+//
+// This program is distributed in the hope that it will be useful, but
WITHOUT
+// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
FITNESS
+// FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
+// details.
+
+package android.com.abb;
+
+import android.graphics.Bitmap;
+import android.graphics.BitmapFactory;
+import android.graphics.Rect;
+import android.net.Uri;
+import android.util.Log;
+import java.lang.Math;
+import java.io.BufferedReader;
+import java.io.FileReader;
+import java.io.IOException;
+import java.util.Random;
+import junit.framework.Assert;
+
+import android.com.abb.Avatar;
+import android.com.abb.Content;
+import android.com.abb.Entity;
+import android.com.abb.GameState;
+
+public class Weapon extends Entity {
+ public Weapon(GameState game_state) {
+ super();
+ //loadWeapon();
+ loadWeaponImages();
+ game_state_ = game_state;
+ sprite_rect = new Rect(0, 0, kSpriteSize, kSpriteSize);
+ }
+
+ public void loadWeapon() {
+ //Load weapons.txt
+ Uri weapons_uri = Uri.parse("content:///weapons.txt");
+ String weapons_path = Content.getTemporaryFilePath(weapons_uri);
+ if (weapons_path == null)
+ Log.e("Weapon::loadWeaponsFromFile", "Invalid null argument.");
+ try {
+ StringBuffer file_data = new StringBuffer(1000);
+ BufferedReader reader = new BufferedReader(new
FileReader(weapons_path));
+ char[] buf = new char[1024];
+ int num_read = 0;
+ while((num_read = reader.read(buf)) != -1){
+ file_data.append(buf, 0, num_read);
+ }
+ reader.close();
+ mWeaponString = file_data.toString();
+ Log.d("Weapon String", mWeaponString);
+ } catch (IOException ex) {
+ Assert.fail("Weapon::loadWeaponFromFile. Cannot find: " +
weapons_path + ".");
+ }
+ }
+
+ public void loadWeaponImages() {
+ //Load weapons.png from URI
+ Uri weapon_images_uri = Uri.parse("content:///weapons.png");
+ String weapon_images_path =
Content.getTemporaryFilePath(weapon_images_uri);
+ Log.d("Weapons Path", weapon_images_path);
+
+ //Load the weapons.png into Bitmap
+ if (weapon_images_path == null)
+ Assert.fail("Weapon::loadWeaponImagesFromFile. Invalid argument.");
+ mWeaponsBitmap = BitmapFactory.decodeFile(weapon_images_path);
+ if (mWeaponsBitmap == null)
+ Assert.fail("Weapon::loadWeaponImagesFromFile. Cannot find: " +
weapon_images_path + ".");
+ }
+
+ public void setImage(Graphics graphics) {
+ // Load the image if it hasn't been done already.
+ if (mWeaponsBitmap != null) {
+ graphics.freeImage(sprite_image);
+ sprite_image = graphics.loadImageFromBitmap(mWeaponsBitmap);
+ mWeaponsBitmap = null;
+ }
+ }
+
+ public void setSprite(boolean facing_left) {
+ sprite_rect.top = kSpriteSize * mWeaponIndex;
+ sprite_rect.bottom = kSpriteSize * mWeaponIndex + kSpriteSize;
+ sprite_flipped_horizontal = facing_left;
+ }
+
+ private GameState game_state_;
+
+ private String mWeaponString;
+ private Bitmap mWeaponsBitmap;
+
+ private int mWeaponIndex = 0;
+ private float shotDelay = 0.2f; // Seconds between shots.
+ private float shotDistance = 25.0f;
+ private float shotOffsetX = 23.0f;
+ private float shotOffsetY = -8.0f;
+ private float shotSpread = 15.0f * (float)Math.PI / 180.0f;
+ private float shotVelocity = 60.0f;
+ private static final int kSpriteSize = 64;
+
+}