Розробка ігрової програми для мобільних пристроїв з сенсорним екраном студента 4 курсу

Вид материалаДокументы
Подобный материал:
1   2   3   4   5   6   7

// Tower.java

import javax.microedition.lcdui.Graphics;

import javax.microedition.lcdui.Image;

import javax.microedition.lcdui.game.Sprite;


public abstract class Tower {

abstract public boolean attack(Monster[] m);

abstract protected void upgrade();

Sprite sp;

int xp;

int dmg;

int range;

int gold;

int cooldown;

long lastactiontime = 0;

String info;

Image img;


public Tower(int xp, int dmg, int range, int gold, int cooldown,Image img,String info) {

this.xp = xp;

this.dmg = dmg;

this.range = range;

this.gold = gold;

this.cooldown = cooldown;

this.img = Image.createImage(img, 0, 0, 36, 36, Sprite.TRANS_NONE );

sp = new Sprite(img, 36, 36);

this.info = info;

}

public void setXY(int x,int y){

sp.setPosition(x, y);

}

public void paint(Graphics g){

sp.paint(g);

}

public void paintInfo(Graphics g,int x,int y){

g.drawImage(img, x, y, Graphics.TOP|Graphics.LEFT);

paint(g);

point tp = getCentPosition();

int color = g.getColor();

g.setColor(255, 0, 0);

g.drawRect(tp.x - range, tp.y - range, range*2 , range*2);

g.drawString("Dmg:\n"+ dmg, x+37, y, Graphics.TOP|Graphics.LEFT);

g.setColor(0, 30, 200);

g.drawString("Range:"+ range, x, y+36, Graphics.TOP|Graphics.LEFT);

g.setColor(255, 245, 48);

g.drawString("Gold:"+ gold, x, y+51, Graphics.TOP|Graphics.LEFT);

g.setColor(200, 200, 200);

g.drawString("Cldn:"+ cooldown, x, y+66, Graphics.TOP|Graphics.LEFT);

g.setColor(0, 0, 0);

g.drawString(info, x-1, y-28, Graphics.TOP|Graphics.LEFT);

g.setColor(color);

}

protected boolean isCooledDown(){

long time = System.currentTimeMillis();

return (time - lastactiontime > cooldown*100);

}

protected boolean isInRange(Monster m){

point p = m.getCentPosition();

point tp = getCentPosition();

return ((p.x <= tp.x + range && p.x >= tp.x - range) &&

(p.y <= tp.y + range) && (p.y >= tp.y - range));

}

public void act(Monster[] m){

if (isCooledDown()) {

if (attack(m)) {

xp ++;

upgrade();

xp = (xp > 1000)?xp%1000:xp;

}

lastactiontime = System.currentTimeMillis();

}

}

public point getCentPosition(){

return new point(sp.getX()+sp.getWidth()/2,sp.getY()+sp.getHeight()/2);

}

public point getPosition(){

return new point(sp.getX(),sp.getY());

}

public int getPrice(){

return gold;

}

}

// Towers.java

import javax.microedition.lcdui.Graphics;


public class Towers {

Tower[] arr;

Tower ntw = null;

boolean sell;


public Towers() {

arr = new Tower[148];

}

public void act(Monster[] mar){

for (int i=0; i
if (arr[i] != null){

arr[i].act(mar);

}

}

}

public void tryNew(Tower ntw){

this.ntw = ntw;

ntw.setXY(-100, -100);

}

public void cancelNew(){

ntw = null;

}

public void moveNew(int x,int y){

if (ntw != null) ntw.setXY(x, y);

}

public void paint(Graphics g){

for (int i=0; i
if (arr[i] != null) arr[i].paint(g);

}

if (!sell && ntw != null) ntw.paintInfo(g, 7,101);

}

public void paintInfo(Graphics g, int x,int y,int ind){

if (arr[ind] != null) arr[ind].paintInfo(g,x,y);

}

public int put(GameField gf,int gold){

if (ntw!= null){

if (gold < ntw.getPrice()) return -1;

point p = ntw.getCentPosition();

p = gf.getCellij(p.x, p.y);

if (!gf.isCellAvailable(p.x, p.y)) return -1;

p = gf.getCellXY(p);

if (!isFree(p)) return -1;

ntw.setXY(p.x, p.y);

for (int i=0; i
if (arr[i] == null){

arr[i]= ntw;

cancelNew();

return gold - arr[i].gold;

}

}

}

return -1;

}

private boolean isFree(point p){

for (int i=0; i
if (arr[i] != null){

if (p.equals(arr[i].getPosition())) return false;

}

}

return true;

}

public int check(point p){

for (int i=0; i
if (arr[i] != null){

point cur = arr[i].getPosition();

if (p.x > cur.x && p.x < cur.x + arr[i].sp.getWidth() &&

p.y > cur.y && p.y < cur.y + arr[i].sp.getHeight()) return i;

}

}

return -1;

}

public int remove(int i){

int gold = arr[i].gold;

arr[i]=null;

sell = false;

return gold;

}

public void changeState(boolean sell){

this.sell = sell;

}

}

// Trek.java

import java.util.Stack;


public class Trek {

Stack st;

public Trek(Stack st){

this.st = st;

}

public point getNext(){

return (point)st.peek();

}

public boolean isEmty(){

return st.isEmpty();

}

public void delNext(){

st.pop();

}

public Trek copy(){

Stack st1 = new Stack();

st1.setSize(st.size());

for (int i=0; i
st1.setElementAt(st.elementAt(i), i);

return new Trek(st1);

}

}

// TrekBuilder.java

import java.util.Random;

import java.util.Stack;

import java.util.Vector;


public class TrekBuilder {

Trek[] treks = null;

int[][] map = null;

int h,w,x0,y0;


public TrekBuilder(int h, int w,int x0,int y0) {

this.h = h;

this.w = w;

this.x0 = x0;

this.y0 = y0;

}

public Stack convertToXY(Stack st){

for (int i=0; i
int x = ((point)st.elementAt(i)).x;

int y = ((point)st.elementAt(i)).y;

((point)st.elementAt(i)).x = y*w + x0;

((point)st.elementAt(i)).y = x*h + y0;

}

System.out.println();

return st;

}

public boolean buildTreks(int[][] map, int si,int sj){

this.map = map;

Stack from = new Stack();

from.push(new point(si, sj));

Vector v = findTrek(from);

if (v.isEmpty()) return false;

treks = new Trek[v.size()];

for (int i =0; i
Stack s = (Stack)v.elementAt(i);

s.addElement(new point(si,sj));

treks[i] = new Trek(convertToXY(s));

}

return true;

}

private boolean ok(int i,int j){

return (map[i][j] == GameField.ROAD);

}

private boolean finish(int i,int j){

return (map[i][j] == GameField.HOME);

}

// повертає вектор стеків шляху (шлях в клітинках, а не координатах)

private Vector findTrek(Stack from){

Vector ret = new Vector();

point[] neib = new point[4];

point f = (point) from.peek();

neib[0] = new point(f.x,f.y-1);

neib[1] = new point(f.x,f.y+1);

neib[2] = new point(f.x-1,f.y);

neib[3] = new point(f.x+1,f.y);

for (int i=0; i<4; i++){

if (neib[i].x < map.length && neib[i].y < map[0].length && neib[i].x>=0 && neib[i].y>=0) {

if (finish(neib[i].x,neib[i].y)){

Stack st = new Stack();

st.push(neib[i].copy());

ret.addElement(st);

}

if (ok(neib[i].x,neib[i].y) && !from.contains(neib[i])){

from.push(neib[i]);

Vector v = findTrek(from);

from.pop();

for (int j = 0; j
Stack cur = (Stack) v.elementAt(j);

cur.push(neib[i].copy());

ret.addElement(cur);

}

}

}

}

return ret;

}

public Trek randTrek(){

if (treks == null) return null;

else{

Random r = new Random();

int i = r.nextInt(treks.length);

return treks[i].copy();

}

}

public Trek[] getTreks() {

return treks;

}

}

// point.java

public class point {

public int x;

public int y;

public point(int x, int y) {

this.x = x;

this.y = y;

}

public boolean equals(Object obj) {

if (obj instanceof point) {

point p = (point)obj;

return (x == p.x && y == p.y);

}

return super.equals(obj);

}

public point copy(){

return new point(x,y);

}

}