Skip to content

Commit c81e0c0

Browse files
committed
veryveryvery ugly stuff happening here
1 parent 05b1b3b commit c81e0c0

File tree

6 files changed

+138
-17
lines changed

6 files changed

+138
-17
lines changed

Basketball/AndroidManifest.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
<application android:label="@string/app_name"
99
android:icon="@drawable/ic_launcher"
1010
android:theme="@style/AppTheme">
11-
<activity android:name="AndroidEx24">
11+
<activity android:name="AndroidEx18">
1212
<intent-filter>
1313
<action android:name="android.intent.action.MAIN"/>
1414
<category android:name="android.intent.category.LAUNCHER"/>

Basketball/src/AndroidEx24.java

Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
// AndroidEx24.java
2+
3+
4+
5+
import ch.aplu.android.*;
6+
import android.graphics.Point;
7+
8+
public class AndroidEx24 extends GameGrid implements GGFlingListener,
9+
GGActorCollisionListener {
10+
private final double vFactor = 1 / 15.0;
11+
private int screenW;
12+
private int screenH;
13+
private Basket basket;
14+
private int startLine;
15+
16+
private GGStatusBar status;
17+
18+
public AndroidEx24() {
19+
super(WHITE, false, true, windowZoom(600));
20+
setScreenOrientation(LANDSCAPE);
21+
status = addStatusBar(20);
22+
}
23+
24+
public void main() {
25+
addFlingListener(this);
26+
getBg().setPaintColor(GREEN);
27+
screenW = getNbHorzCells();
28+
screenH = getNbVertCells();
29+
startLine = screenW * 3 / 4;
30+
getBg().setLineWidth(4);
31+
getBg().drawLine(startLine, screenH / 8, startLine,
32+
screenH * 7 / 8);
33+
basket = new Basket();
34+
basket.setCollisionRectangle(new Point(0, -30), 45, 6);
35+
addActor(basket, new Location(screenH/2, 250));
36+
setSimulationPeriod(50);
37+
doRun();
38+
status.setText("Fling the ball!");
39+
}
40+
41+
public boolean flingEvent(Point start, Point end, GGVector velocity) {
42+
if (end.x < startLine || start.x < startLine) {
43+
showToast("Stay behind the line!");
44+
return true;
45+
}
46+
Ball ball = new Ball(vFactor * velocity.x, vFactor * velocity.y);
47+
L.d("" + velocity);
48+
addActorNoRefresh(ball, new Location(end.x, end.y));
49+
ball.addCollisionActor(basket);
50+
ball.addActorCollisionListener(this);
51+
return true;
52+
}
53+
54+
public int collide(Actor actor1, Actor actor2) {
55+
Ball b = (Ball) actor1;
56+
if (b.isFalling()) {
57+
b.drop();
58+
playTone(1200, 20);
59+
}
60+
return 100;
61+
}
62+
}
63+
64+
class Ball extends Actor {
65+
private final double g = 9.81; // in m/s^2
66+
private double x, y; // in m
67+
private double vx, vy; // in m/s
68+
private double dt = 0.4; // in s
69+
int factor = 10;
70+
71+
public Ball(double vx, double vy) {
72+
super("ball");
73+
this.vx = vx;
74+
this.vy = vy;
75+
76+
}
77+
78+
public boolean isFalling() {
79+
//careful, the left upper corner is (0,0), so a positive vy means falling
80+
return vy > 0;
81+
}
82+
83+
public void drop() {
84+
vy += Math.abs(vx)/5;
85+
vx = vx/10;
86+
}
87+
88+
public void reset() {
89+
x = getX();
90+
y = getY();
91+
}
92+
93+
public void act() {
94+
95+
vy = vy + g * dt;
96+
x = x + vx * dt;
97+
y = y + vy * dt;
98+
setLocation(new Location(x, y));
99+
//reflect at wall:
100+
if (x < this.getWidth(0))
101+
vx = -vx;
102+
if (!isInGrid())
103+
removeSelf();
104+
}
105+
}
106+
107+
class Basket extends Actor {
108+
public Basket() {
109+
super("basket");
110+
}
111+
}

Basketball/src/sm/phbern/basketball/AndroidEx18.java

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// AndroidEx18.java
22

3-
package app.ex18;
3+
package sm.phbern.basketball;
44

55
import ch.aplu.android.*;
66
import android.graphics.Point;
@@ -19,21 +19,21 @@ public class AndroidEx18 extends GameGrid implements GGFlingListener,
1919
public AndroidEx18() {
2020
super(WHITE, false, true, windowZoom(600));
2121
setScreenOrientation(LANDSCAPE);
22-
status = addStatusBar(20);
22+
status = addStatusBar(40);
2323
}
2424

2525
public void main() {
2626
dimFactor = 600.0 / roomHeight;
2727
z = getZoomFactor();
2828
roomWidth = toPhysical(getNbHorzCells());
2929
addFlingListener(this);
30-
setSimulationPeriod(50);
30+
setSimulationPeriod(30);
3131
basket = new Basket();
3232
addActor(basket, new Location(toPix(0.5), toPix(1.8)));
3333
basket.setCollisionRectangle(new Point(0, -20), 40, 20);
3434
drawBg();
3535
doRun();
36-
status.setText("Fling the ball!");
36+
//status.setText("Fling the ball!");
3737
}
3838

3939
public boolean flingEvent(Point start, Point end, GGVector velocity) {
@@ -80,7 +80,7 @@ class Ball extends Actor {
8080
private final double g = 9.81; // in m/s^2
8181
private double x, y; // in m
8282
private double vx, vy; // in m/s
83-
private double dt = 0.05; // in s
83+
private double dt = 0.025; // in s
8484
private AndroidEx18 app;
8585

8686
public Ball(AndroidEx18 app, double vx, double vy) {
@@ -101,7 +101,8 @@ public void act() {
101101
x = x + vx * dt;
102102
y = y + vy * dt;
103103
setLocation(new Location(app.toPix(x), app.toPix(y)));
104-
104+
if (app.toPix(x) < getWidth(0))
105+
vx = -vx;
105106
if (!isInGrid())
106107
removeSelf();
107108
}

TCP/.classpath

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
<classpathentry kind="src" path="src"/>
44
<classpathentry kind="src" path="gen"/>
55
<classpathentry kind="con" path="com.android.ide.eclipse.adt.ANDROID_FRAMEWORK"/>
6+
<classpathentry kind="con" path="org.eclipse.jdt.USER_LIBRARY/Jdroid"/>
67
<classpathentry kind="con" path="com.android.ide.eclipse.adt.LIBRARIES"/>
78
<classpathentry kind="output" path="bin/classes"/>
89
</classpath>

TCP/AndroidManifest.xml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@
33
package="ph.sm.ship"
44
android:versionCode="1"
55
android:versionName="1.0">
6-
<uses-sdk android:minSdkVersion="8" />
6+
<uses-sdk android:minSdkVersion="8" android:targetSdkVersion="12"/>
7+
<uses-permission android:name="android.permission.INTERNET" />
8+
<uses-permission android:name="android.permission.VIBRATE"/>
79
<application android:label="TCPShip" android:icon="@drawable/jdroid_gglogo">
810
<activity android:name=".TcpShip"
911
android:configChanges="orientation">
@@ -16,6 +18,4 @@
1618
android:configChanges="keyboardHidden|orientation">
1719
</activity>
1820
</application>
19-
<uses-permission android:name="android.permission.INTERNET" />
20-
<uses-permission android:name="android.permission.VIBRATE"/>
2121
</manifest>

TCP/src/ph/sm/ship/TcpShip.java

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,13 @@
22

33
package ph.sm.ship;
44

5+
import android.graphics.Color;
56
import ch.aplu.android.*;
67
import ch.aplu.tcp.*;
78
import ch.aplu.util.Monitor;
89

910

10-
public class TcpShip extends GameGrid implements GGTouchListener,TcpNodeListener
11+
public class TcpShip extends GameGrid implements GGTouchListener, TcpNodeListener
1112
{
1213
private String roomID = "";
1314
private String sessionID = "awq";
@@ -29,14 +30,14 @@ private interface Command
2930

3031
public TcpShip()
3132
{
32-
super(6, 6, cellZoom(60), RED, false);
33+
super(6, 6, 60, RED);
34+
status = addStatusBar(50);
3335
// setScreenOrientation(GGNavigationListener.ScreenOrientation.FIXED);
3436
}
3537

3638
public void main()
3739
{
3840
setTitle("TCP BattleShip");
39-
4041
init();
4142
}
4243

@@ -47,23 +48,30 @@ public void init()
4748
addTouchListener(this, GGTouch.click);
4849
refresh();
4950
node.addTcpNodeListener(this);
50-
showToast("Connecting to relay...");
51-
connect();
52-
Monitor.putSleep(4000);
51+
status.setText("Connecting to relay...");
52+
refresh();
53+
connect(10);
5354
if (node.getNodeState() == TcpNodeState.CONNECTED)
5455
{
5556
status.setText("Connection established.");
5657
}
5758
else
5859
status.setText("Connection failed");
60+
refresh();
5961
}
6062

61-
private void connect()
63+
private void connect(int timeout)
6264
{
6365
while (roomID.length() < 3)
6466
roomID = requestEntry("Enter unique game room name (more than 2 characters):");
6567
sessionID = sessionID + roomID;
6668
node.connect(sessionID, myNodeName);
69+
70+
int connectingTime = 0;
71+
while (node.getNodeState() != TcpNodeState.CONNECTED && connectingTime < timeout) {
72+
connectingTime++;
73+
Monitor.putSleep(1000);
74+
}
6775
}
6876

6977
private String requestEntry(String prompt)

0 commit comments

Comments
 (0)