Skip to content
This repository was archived by the owner on Mar 3, 2025. It is now read-only.

Commit 9e971b5

Browse files
committed
html support
I love gwt!
1 parent 0d4087e commit 9e971b5

File tree

13 files changed

+269
-54
lines changed

13 files changed

+269
-54
lines changed

core/src/com/zzzyt/rs/Controller.java

Lines changed: 0 additions & 7 deletions
This file was deleted.

core/src/com/zzzyt/rs/RocketSimulator.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
import com.badlogic.gdx.utils.viewport.FitViewport;
1212
import com.zzzyt.rs.draw.Drawer;
1313
import com.zzzyt.rs.type.Rocket;
14+
import com.zzzyt.rs.util.StringUtil;
1415
import com.zzzyt.rs.phy.Phy;
1516
import com.zzzyt.rs.phy.Simulator;
1617
import com.zzzyt.rs.rocket.RocketManager;
@@ -85,7 +86,7 @@ public void render() {
8586
}
8687

8788
if(fcd<=0) {
88-
Gdx.app.debug("Rocket", String.format(Locale.getDefault(),"t=%.1f x=%.2f y=%.2f vx=%.2f vy=%.2f m=%.0f", r.t,r.x,r.y,r.vx,r.vy,r.getMass())+"\n\t"+String.format(Locale.getDefault(),"sp=%.2f zm=%.2f tr=%.2f f=%.2f h=%.1f drag=%.2f th=%.2f",sim.speed,cam.zoom,r.throttle,r.stages.get(r.stage).f,Phy.tri(r.x,r.y)-Phy.R,Phy.tri(r.x,r.y)>=Phy.R+Phy.up?0:r.getDrag()*(Phy.R+Phy.up-Phy.tri(r.x,r.y))/Phy.up,r.getTheta()/Math.PI));
89+
Gdx.app.debug("Rocket", StringUtil.format("t=%.1f x=%.3f y=%.2f vx=%.2f vy=%.2f m=%.0f", r.t,r.x,r.y,r.vx,r.vy,r.getMass())+"\n\t"+StringUtil.format("sp=%.2f zm=%.2f tr=%.2f f=%.2f h=%.1f drag=%.2f th=%.2f",sim.speed,cam.zoom,r.throttle,r.stages.get(r.stage).f,Phy.tri(r.x,r.y)-Phy.R,Phy.tri(r.x,r.y)>=Phy.R+Phy.up?0:r.getDrag()*(Phy.R+Phy.up-Phy.tri(r.x,r.y))/Phy.up,r.getTheta()/Math.PI));
8990
fcd=30;
9091
}
9192
else {

core/src/com/zzzyt/rs/draw/Drawer.java

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
import com.zzzyt.rs.RocketSimulator;
1212
import com.zzzyt.rs.phy.Phy;
1313
import com.zzzyt.rs.type.Rocket;
14+
import com.zzzyt.rs.util.StringUtil;
1415

1516
import java.util.Locale;
1617

@@ -153,14 +154,14 @@ else if(zoom<3E5f) {
153154
batch.setProjectionMatrix(defaultMat);
154155
batch.begin();
155156
font.setColor(1,1,1,1);
156-
font.draw(batch, String.format(Locale.getDefault(),"Version:%s Rocket Name: %s","1.1.2",r.name),10,rs.h-10);
157-
font.draw(batch, String.format(Locale.getDefault(),"t=%.1f x=%.2f y=%.2f vx=%.2f vy=%.2f m=%.0f", r.t,r.x,r.y,r.vx,r.vy,r.getMass()), 10,rs.h-30);
158-
font.draw(batch,String.format(Locale.getDefault(),"sp=%.2f zm=%.2f tr=%.2f f=%.2f h=%.1f drag=%.2f th=%.2f",rs.sim.speed,rs.cam.zoom,r.throttle,r.stages.get(r.stage).f,Phy.tri(r.x,r.y)-Phy.R,Phy.tri(r.x,r.y)>=Phy.R+Phy.up?0:r.getDrag()*(Phy.R+Phy.up-Phy.tri(r.x,r.y))/Phy.up,r.getTheta()/Math.PI),10,rs.h-50);
157+
font.draw(batch, StringUtil.format("Version:%s Rocket Name: %s","1.1.2",r.name),10,rs.h-10);
158+
font.draw(batch, StringUtil.format("t=%.1f x=%.2f y=%.2f vx=%.2f vy=%.2f m=%.0f", r.t,r.x,r.y,r.vx,r.vy,r.getMass()), 10,rs.h-30);
159+
font.draw(batch,StringUtil.format("sp=%.2f zm=%.2f tr=%.2f f=%.2f h=%.1f drag=%.2f th=%.2f",rs.sim.speed,rs.cam.zoom,r.throttle,r.stages.get(r.stage).f,Phy.tri(r.x,r.y)-Phy.R,Phy.tri(r.x,r.y)>=Phy.R+Phy.up?0:r.getDrag()*(Phy.R+Phy.up-Phy.tri(r.x,r.y))/Phy.up,r.getTheta()/Math.PI),10,rs.h-50);
159160
if(len>=1000) {
160-
font.draw(batch,String.format(Locale.getDefault(),"%.0fkm", len/1000),20,RocketSimulator.rs.h-70);
161+
font.draw(batch,StringUtil.format("%.0fkm", len/1000),20,RocketSimulator.rs.h-70);
161162
}
162163
else {
163-
font.draw(batch,String.format(Locale.getDefault(),"%.0fm", len),20,RocketSimulator.rs.h-70);
164+
font.draw(batch,StringUtil.format("%.0fm", len),20,RocketSimulator.rs.h-70);
164165
}
165166
batch.end();
166167
}

core/src/com/zzzyt/rs/phy/Phy.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import com.zzzyt.rs.RocketSimulator;
55
import com.zzzyt.rs.type.Rocket;
66
import com.zzzyt.rs.type.Stage;
7+
import com.zzzyt.rs.util.StringUtil;
78

89
public class Phy {
910
public final static double G = 6.67428E-11d;
@@ -78,7 +79,7 @@ public static boolean sim(Rocket r) {
7879

7980
if (Phy.tri(r.x, r.y)-Phy.R<1) {
8081
if(Phy.tri(r.vx, r.vy)>10) {
81-
Gdx.app.log("Rocket", String.format("Crash! t=%.2f", r.t));
82+
Gdx.app.log("Rocket", StringUtil.format("Crash! t=%.2f", r.t));
8283
return false;
8384
}
8485
else {

core/src/com/zzzyt/rs/type/Rocket.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
import com.badlogic.gdx.Gdx;
77
import com.zzzyt.rs.phy.Phy;
8+
import com.zzzyt.rs.util.StringUtil;
89

910
public class Rocket {
1011
public String name;
@@ -46,7 +47,7 @@ public double getDrag() {
4647
}
4748

4849
public void stage() {
49-
Gdx.app.log("Rocket",String.format("Stage: %d -> %d",stage,stage+1));
50+
Gdx.app.log("Rocket",StringUtil.format("Stage: %d -> %d",stage,stage+1));
5051
stage++;
5152
tmpmass=0;
5253
for(int i=stage+1;i<stages.size();i++) {
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
package com.zzzyt.rs.util;
2+
3+
public class StringUtil {
4+
//thanks to StackOverflow!
5+
//https://stackoverflow.com/questions/15452994/what-is-the-alternative-for-string-format-in-gwt
6+
public static String format(final String format, final Object... args) {
7+
final StringBuffer buffer= new StringBuffer();
8+
int cnt=0;
9+
for (int i= 0; i< format.length(); i++) {
10+
if(format.charAt(i)!='%') {
11+
buffer.append(format.charAt(i));
12+
}
13+
else {
14+
char nxt=format.charAt(i+1);
15+
if(nxt=='d'||nxt=='s'||nxt=='c') {
16+
buffer.append(args[cnt].toString());
17+
i++;
18+
}
19+
else if(nxt=='%') {
20+
buffer.append('%');
21+
i++;
22+
}
23+
else if(nxt=='.') {
24+
int idd=format.indexOf('f', i);
25+
int dig=Integer.parseInt(format.substring(i+2,idd));
26+
try {
27+
double num=Double.parseDouble(args[cnt].toString());
28+
String tmp;
29+
if(dig==0) {
30+
tmp=Integer.toString((int)Math.round(num));
31+
}
32+
else {
33+
tmp=Double.toString((Math.round(num*Math.pow(10, dig))/Math.pow(10, dig)));
34+
int dot=tmp.lastIndexOf('.');
35+
if(dot!=-1) {
36+
int les=tmp.length()-dot-1;
37+
if(les<dig) {
38+
for(int j=0;j<dig-les;j++) {
39+
tmp+='0';
40+
}
41+
}
42+
}
43+
}
44+
45+
buffer.append(tmp);
46+
}
47+
catch(Exception e) {
48+
e.printStackTrace();
49+
}
50+
i=idd;
51+
}
52+
cnt++;
53+
}
54+
}
55+
return buffer.toString();
56+
}
57+
}
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
package com.zzzyt.rs.client;
2+
3+
import com.badlogic.gdx.Input;
4+
import com.badlogic.gdx.InputAdapter;
5+
import com.badlogic.gdx.math.Vector3;
6+
import com.zzzyt.rs.RocketSimulator;
7+
8+
public class HtmlController extends InputAdapter{
9+
static final double[] speeds= {0,0.1,0.2,0.5,1,1.5,2,5,10,50,100,500,1000,10000,100000,1000000};
10+
int spd;
11+
12+
RocketSimulator rs;
13+
Vector3 tp,tp2;
14+
15+
public boolean touchDown (int screenX, int screenY, int pointer, int button) {
16+
if(button!=Input.Buttons.RIGHT)return false;
17+
tp=new Vector3(screenX,screenY,0);
18+
tp2=new Vector3(0,0,0);
19+
return true;
20+
}
21+
22+
public boolean touchUp (int screenX, int screenY, int pointer, int button) {
23+
tp=null;
24+
return true;
25+
}
26+
27+
public boolean touchDragged (int screenX, int screenY, int pointer) {
28+
if(tp!=null) {
29+
tp2=new Vector3(screenX,screenY,0);
30+
rs.cam.translate(rs.cam.unproject(tp).sub(rs.cam.unproject(tp2)));
31+
tp.x=screenX;
32+
tp.y=screenY;
33+
return true;
34+
}
35+
return false;
36+
}
37+
38+
public boolean keyDown(int keycode) {
39+
switch (keycode) {
40+
case Input.Keys.SHIFT_RIGHT:
41+
rs.focusOn=!rs.focusOn;
42+
return true;
43+
case Input.Keys.SPACE:
44+
if(rs.r.stage<rs.r.stages.size()-1)rs.r.stage();
45+
return true;
46+
case Input.Keys.Z:
47+
rs.r.throttle=0;
48+
return true;
49+
case Input.Keys.X:
50+
rs.r.throttle=1;
51+
return true;
52+
case Input.Keys.LEFT:
53+
if(speeds[spd]!=rs.sim.speed) {
54+
for(int i=speeds.length-1;i>=0;i--) {
55+
if(speeds[i]<=rs.sim.speed) {
56+
spd=i;
57+
}
58+
}
59+
}
60+
if(spd>0)spd--;
61+
rs.sim.speed=speeds[spd];
62+
return true;
63+
case Input.Keys.RIGHT:
64+
if(speeds[spd]!=rs.sim.speed) {
65+
for(int i=speeds.length-1;i>=0;i--) {
66+
if(speeds[i]<=rs.sim.speed) {
67+
spd=i;
68+
}
69+
}
70+
}
71+
if(spd<speeds.length-1)spd++;
72+
rs.sim.speed=speeds[spd];
73+
default:
74+
return false;
75+
}
76+
}
77+
78+
public HtmlController(){
79+
super();
80+
this.rs=RocketSimulator.rs;
81+
this.spd=4;
82+
}
83+
}
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
package com.zzzyt.rs.client;
2+
3+
import com.badlogic.gdx.Gdx;
4+
import com.badlogic.gdx.Input.Keys;
5+
import com.zzzyt.rs.Handler;
6+
import com.zzzyt.rs.RocketSimulator;
7+
8+
public class HtmlHandler implements Handler {
9+
RocketSimulator rs;
10+
11+
public void handle() {
12+
double zdif=1;
13+
if(rs.cam.zoom<1.5) {
14+
zdif=0.01;
15+
}
16+
else if(rs.cam.zoom<20) {
17+
zdif=0.5;
18+
}
19+
else if(rs.cam.zoom<100) {
20+
zdif=2;
21+
}
22+
else if(rs.cam.zoom<1000) {
23+
zdif=20;
24+
}
25+
else if(rs.cam.zoom<10000){
26+
zdif=200;
27+
}
28+
else if(rs.cam.zoom<100000){
29+
zdif=1000;
30+
}
31+
else {
32+
zdif=10000;
33+
}
34+
if(Gdx.input.isKeyPressed(Keys.UP)) {
35+
rs.cam.zoom-=zdif;
36+
if(rs.cam.zoom<0.01)rs.cam.zoom=0.01f;
37+
}
38+
if(Gdx.input.isKeyPressed(Keys.DOWN)) {
39+
rs.cam.zoom+=zdif;
40+
}
41+
42+
if(Gdx.input.isKeyPressed(Keys.A)) {
43+
rs.r.gimbal+=0.01;
44+
if(rs.r.gimbal>0.2)rs.r.gimbal=0.2;
45+
}
46+
if(Gdx.input.isKeyPressed(Keys.D)) {
47+
rs.r.gimbal-=0.01;
48+
if(rs.r.gimbal<-0.2)rs.r.gimbal=-0.2;
49+
}
50+
if(Gdx.input.isKeyPressed(Keys.Q)) {
51+
rs.r.dir+=0.05;
52+
}
53+
if(Gdx.input.isKeyPressed(Keys.E)) {
54+
rs.r.dir-=0.05;
55+
}
56+
57+
if(Gdx.input.isKeyPressed(Keys.SHIFT_LEFT)) {
58+
rs.r.throttle+=0.01;
59+
if(rs.r.throttle<0)rs.r.throttle=0;
60+
}
61+
if(Gdx.input.isKeyPressed(Keys.CONTROL_LEFT)) {
62+
rs.r.throttle-=0.01;
63+
if(rs.r.throttle>1)rs.r.throttle=1;
64+
}
65+
}
66+
67+
public HtmlHandler() {
68+
super();
69+
this.rs=RocketSimulator.rs;
70+
}
71+
}
Lines changed: 46 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,51 +1,58 @@
11
package com.zzzyt.rs.client;
22

33
import com.badlogic.gdx.ApplicationListener;
4+
import com.badlogic.gdx.Gdx;
45
import com.badlogic.gdx.backends.gwt.GwtApplication;
56
import com.badlogic.gdx.backends.gwt.GwtApplicationConfiguration;
7+
import com.google.gwt.event.logical.shared.ResizeEvent;
8+
import com.google.gwt.event.logical.shared.ResizeHandler;
9+
import com.google.gwt.user.client.Window;
610
import com.zzzyt.rs.RocketSimulator;
711

812
public class HtmlLauncher extends GwtApplication {
913

10-
// USE THIS CODE FOR A FIXED SIZE APPLICATION
11-
@Override
12-
public GwtApplicationConfiguration getConfig () {
13-
return new GwtApplicationConfiguration(480, 320);
14-
}
15-
// END CODE FOR FIXED SIZE APPLICATION
14+
// USE THIS CODE FOR A FIXED SIZE APPLICATION
15+
// @Override
16+
// public GwtApplicationConfiguration getConfig() {
17+
// return new GwtApplicationConfiguration(480*3, 320*3);
18+
// }
19+
// END CODE FOR FIXED SIZE APPLICATION
1620

17-
// UNCOMMENT THIS CODE FOR A RESIZABLE APPLICATION
18-
// PADDING is to avoid scrolling in iframes, set to 20 if you have problems
19-
// private static final int PADDING = 0;
20-
// private GwtApplicationConfiguration cfg;
21-
//
22-
// @Override
23-
// public GwtApplicationConfiguration getConfig() {
24-
// int w = Window.getClientWidth() - PADDING;
25-
// int h = Window.getClientHeight() - PADDING;
26-
// cfg = new GwtApplicationConfiguration(w, h);
27-
// Window.enableScrolling(false);
28-
// Window.setMargin("0");
29-
// Window.addResizeHandler(new ResizeListener());
30-
// cfg.preferFlash = false;
31-
// return cfg;
32-
// }
33-
//
34-
// class ResizeListener implements ResizeHandler {
35-
// @Override
36-
// public void onResize(ResizeEvent event) {
37-
// int width = event.getWidth() - PADDING;
38-
// int height = event.getHeight() - PADDING;
39-
// getRootPanel().setWidth("" + width + "px");
40-
// getRootPanel().setHeight("" + height + "px");
41-
// getApplicationListener().resize(width, height);
42-
// Gdx.graphics.setWindowedMode(width, height);
43-
// }
44-
// }
45-
// END OF CODE FOR RESIZABLE APPLICATION
21+
// UNCOMMENT THIS CODE FOR A RESIZABLE APPLICATION
22+
// PADDING is to avoid scrolling in iframes, set to 20 if you have problems
23+
private static final int PADDING = 0;
24+
private GwtApplicationConfiguration cfg;
4625

47-
@Override
48-
public ApplicationListener createApplicationListener () {
49-
return new RocketSimulator();
50-
}
26+
@Override
27+
public GwtApplicationConfiguration getConfig() {
28+
int w = Window.getClientWidth() - PADDING;
29+
int h = Window.getClientHeight() - PADDING;
30+
cfg = new GwtApplicationConfiguration(w, h);
31+
Window.enableScrolling(false);
32+
Window.setMargin("0");
33+
Window.addResizeHandler(new ResizeListener());
34+
cfg.preferFlash = false;
35+
return cfg;
36+
}
37+
38+
class ResizeListener implements ResizeHandler {
39+
@Override
40+
public void onResize(ResizeEvent event) {
41+
int width = event.getWidth() - PADDING;
42+
int height = event.getHeight() - PADDING;
43+
getRootPanel().setWidth("" + width + "px");
44+
getRootPanel().setHeight("" + height + "px");
45+
getApplicationListener().resize(width, height);
46+
Gdx.graphics.setWindowedMode(width, height);
47+
}
48+
}
49+
// END OF CODE FOR RESIZABLE APPLICATION
50+
51+
@Override
52+
public ApplicationListener createApplicationListener() {
53+
RocketSimulator game = new RocketSimulator();
54+
game.control = new HtmlController();
55+
game.handler = new HtmlHandler();
56+
return game;
57+
}
5158
}
1.93 MB
Binary file not shown.

0 commit comments

Comments
 (0)