Skip to content

Commit

Permalink
wrzutka
Browse files Browse the repository at this point in the history
  • Loading branch information
KaMyLuS committed Dec 8, 2012
1 parent c89adb1 commit 229e74b
Show file tree
Hide file tree
Showing 4 changed files with 135 additions and 20 deletions.
1 change: 0 additions & 1 deletion src/com/test/nanowar/model/Tower.java
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,6 @@ public Troops sendTroops(Integer percentageShare, Tower destination) {
public void troopsArrived(Troops bubble) {
if (owner == bubble.getOwner()) {
troopsCount += bubble.count();
// TODO! a co jeśli przekroczy capacity?
}
else {
troopsCount -= bubble.count();
Expand Down
36 changes: 28 additions & 8 deletions src/com/test/nanowar/model/Troops.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,17 @@
*/
public class Troops extends GameObject {
public static final Integer RADIUS = 20;
public static final Integer BASE = 50;

protected Integer count;
protected Tower source, destination;
protected RealPoint realCenter, step;

protected Point relativeCenter;
protected com.test.nanowar.view.Troops view;

public Troops(Player owner, Integer count, Point center) {
super(owner, center, RADIUS);
this.realCenter = new RealPoint(center.x, center.y);
this.relativeCenter = center;
this.owner = owner;
this.count = count;
}
Expand All @@ -32,8 +35,8 @@ public void sendBetween(Tower source, Tower destination) {
this.source = source;
this.destination = destination;

step = new RealPoint(destination.center.x - realCenter.x, destination.center.y - realCenter.y);
step.normalise();
//step = new RealPoint(destination.center.x - realCenter.x, destination.center.y - realCenter.y);
//step.normalise();
// TODO! obliczenie odleglosci, wyznaczenie sciezki, narysowanie linii, uruchomienie animacji
// w animacji sprawdzamy, czy sie nie przecielismy z docelowa wieza (jesli tak,
// wywolaj destination.troopsArrived(this)
Expand All @@ -48,14 +51,13 @@ public Integer count() {
}

public void update() {
realCenter.add(step);
center.x = (int)floor(realCenter.getX());
center.y = (int)floor(realCenter.getY());
view.update();
}

public boolean destinationReached() {
if(destination != null) {
return this.location.intersect(destination.getLocation());
return true;
//return this.location.intersect(destination.getLocation());
}
else {
// nie powinno sie wydarzyc, ale jakby co...
Expand All @@ -66,4 +68,22 @@ public boolean destinationReached() {
public Tower getDestination() {
return this.destination;
}

public Point getRelativeCenter() {
return relativeCenter;
}

public void setRelativeCenter(Point relativeCenter) {
this.relativeCenter = relativeCenter;
}

public com.test.nanowar.view.Troops getView() {
return view;
}

public void setView(com.test.nanowar.view.Troops view) {
this.view = view;
}


}
2 changes: 1 addition & 1 deletion src/com/test/nanowar/view/Tower.java
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ public double computeExternalRadius() {

// w procentach
public double computeInternalRadius() {
return Math.min(Tower.MAX_RADIUS_PERCENTAGE, Math.max(Math.sqrt(model.getTroopsCount() / model.getCapacity()) * Tower.MAX_RADIUS_PERCENTAGE, Tower.MIN_RADIUS_PERCENTAGE));
return Math.min(Tower.MAX_RADIUS_PERCENTAGE, Math.max(Math.sqrt(model.getTroopsCount() / model.getCapacity()) * computeExternalRadius(), Tower.MIN_RADIUS_PERCENTAGE));
}

public void update() {
Expand Down
116 changes: 106 additions & 10 deletions src/com/test/nanowar/view/Troops.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,15 @@

import android.content.Context;
import android.graphics.Color;
import android.graphics.Point;
import android.graphics.Rect;
import android.view.Gravity;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import com.larvalabs.svgandroid.SVG;
import com.larvalabs.svgandroid.SVGParser;
import com.test.nanowar.MainLayout;
import com.test.nanowar.R;
import com.test.nanowar.model.Player;

Expand All @@ -19,17 +23,44 @@
* @author artur
*/
public class Troops extends RelativeLayout {

public static final int MIN_RADIUS_PERCENTAGE = 3;
public static final int MAX_RADIUS_PERCENTAGE = 10;

protected SVG resource;
protected ImageView background;
protected TextView count;
protected com.test.nanowar.model.Troops model;

protected Point center;
protected Rect position;
protected MainLayout layout;

private Troops(Context context) {
super(context);
}

private Troops(MainLayout layout) {
super(layout.getContext());
this.layout = layout;
}

public static Troops createForPlayer(com.test.nanowar.model.Troops model, MainLayout layout) {
Troops troops = new Troops(layout.getContext());
troops.setModel(model);
troops.setOwner(model.getOwner());
//troops.init(layout);

troops.setCenter(layout.convertToPx(model.getRelativeCenter()));
troops.setPosition(troops.computePosition());
troops.buildView();

public static Troops createForPlayer(com.test.nanowar.model.Troops model, Context context, RelativeLayout layout) {
return troops;
}

/* old
*
* public static Troops createForPlayer(com.test.nanowar.model.Troops model, Context context, RelativeLayout layout) {
Troops troops = new Troops(context);
troops.setModel(model);
//troops.init(layout);
Expand All @@ -38,7 +69,7 @@ public static Troops createForPlayer(com.test.nanowar.model.Troops model, Contex
troops.initAnimation();
return troops;
}
}*/

public void setOwner(Player owner) {
if (owner != null) {
Expand All @@ -65,19 +96,84 @@ protected SVG setResource(int resourceId) {
return resource = SVGParser.getSVGFromResource(getResources(), resourceId);
}

protected void init(Context context, RelativeLayout layout) {
// RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams((int) (1.5*radius), (int) (1.5*radius));
// params.alignWithParent = true;
// params.leftMargin = center.x - radius;
// params.topMargin = center.y - radius;
// layout.addView(this, params);
public Point getCenter() {
return center;
}

protected void initAnimation() {
// utworzenie animacji, uruchomienie i sprawdzanie czy się dotarło, jeśli tak, to wywołaj metodę z modelu
public void setCenter(Point center) {
this.center = center;
}

public Rect getPosition() {
return position;
}

public void setPosition(Rect position) {
this.position = position;
}

protected void buildView() {
buildContainer();
buildBackground();
buildCountElement();
}

protected void buildContainer() {
// tower container
int actualRadius = layout.getRadius(computeRadius());
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(actualRadius, actualRadius);
params.alignWithParent = true;
params.leftMargin = center.x - actualRadius;
params.topMargin = center.y - actualRadius;
layout.addView(this, params);
}

protected void buildBackground() {
/*background = new ImageView(layout.getContext());
background.setImageDrawable(outerResource.createPictureDrawable());
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.MATCH_PARENT);
this.addView(outerBackground, params);*/
}

protected void buildCountElement() {
/*count = new TextView(layout.getContext());
count.setText(Integer.toString(model.getTroopsCount()) + " ");
count.setGravity(Gravity.CENTER_VERTICAL | Gravity.CENTER_HORIZONTAL);
count.setTextColor(textColor);
count.setTextSize(10);
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
params.addRule(RelativeLayout.CENTER_IN_PARENT);
this.addView(count, params);*/
}

/*protected void init(Context context, MainLayout layout) {
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams((int) (1.5*radius), (int) (1.5*radius));
params.alignWithParent = true;
params.leftMargin = center.x - radius;
params.topMargin = center.y - radius;
layout.addView(this, params);
}*/

/*protected void initAnimation() {
// utworzenie animacji, uruchomienie i sprawdzanie czy się dotarło, jeśli tak, to wywołaj metodę z modelu
}*/

protected void setTextColor(int color) {
count.setTextColor(color);
}

public void update() {

}

private Rect computePosition() {
int actualRadius = layout.getRadius(computeRadius());
return new Rect(center.x - actualRadius, center.y - actualRadius, center.x + actualRadius, center.y + actualRadius);
}

public double computeRadius() {
return Math.max( Math.min(1, model.count()/com.test.nanowar.model.Troops.BASE) * Troops.MAX_RADIUS_PERCENTAGE, Troops.MIN_RADIUS_PERCENTAGE);
}
}

0 comments on commit 229e74b

Please sign in to comment.