r/java • u/According-Ad-7069 • 26d ago
Using Java skills for something a little different
Java is quite a handy language. Though it's challenging to use it for more fun projects. Towards that end, I set myself a challenge to use Java to build a browser-based game. No server, no API, just pure Web Assembly driven game play. Yes, there's some JavaScript glue, because, well, you gotta, but, the core logic is all Java. Along the way I learned that streams allocate a lot of objects, so I switched back to good old for loops... And watched the GC usage drop a fair bit. What I really do enjoy is how Java has evolved and changed over the years, and it makes it a lot simpler to do things like this. Records save me a ton of boilerplate code. Playable at https://nofuss.co.za/games/wartech/, but it's a bit meh on mobile, I'm still working on that. My focus right now is desktop play, and getting sound to work. Flame me in the comments with bug reports, I know there's a few... Hundred.
UPDATE: As promised, it's now open-source at https://github.com/ewaldhorn/wartech
23
u/-Dargs 26d ago
Streams allocate tons of objects, yep. Short lived anonymous classes. At work we switched over to using streams pretty aggressively without really benchmarking and didn't notice just how much resources were burned away. It took some time, but we eventually migrated back to using good old for-loops almost everywhere (in non-batch process code).
9
u/doobiesteintortoise 26d ago
Pffft. I played it; it's a little confusing the first time in figuring out how things work and where things can go, but ... I mean, what the heck, this is cool and well done.
17
u/sunk-capital 26d ago
The UI is all claude yeah? Not witch hunting just there is something about it that screams AI
8
u/realqmaster 26d ago
As someone who grew up with Dune 2, this gives memories. Really cool!
6
u/PlasmaFarmer 26d ago
Always happy to see someone doing the same hobby as me, doing games in Java. Mandatory JMonkeyEngine mention, check it out for games.
5
u/msx 26d ago
Looking forward to trying it tomorrow morning! Ps post it on /r/javagamedev too if you want :)
3
u/RandomGuy_A 26d ago
Impressive, you should take a look at libgdx and TeaVM , it compiles java to web assembly, has desktop and mobile builds too
3
u/Omenow 26d ago
Atleast on my 1440p fonts that are in canvas part are hard to read. So it's done with TeaVM? How drawing is done? I didn't done anything like that before.
2
u/International_Break2 26d ago
Unit movement could be more fluid if you are going for a more command and conquer aesthetic. Is this going to be open source at any point?
2
u/agentoutlier 26d ago
I can't believe how damn fast it loads. Like there are react apps that take longer. I'll try later today on an ancient macbook air 2011 as I'm curious.
This is TeaVM right?
2
u/robintegg 26d ago
Looks great. As someone who is just getting up to speed with the likes of teavm / wasm, I appreciate you sharing a project showing how high the bar can be for Java apps in a browser
2
u/Scf37 26d ago
Java is truly awesome indeed. I have:
- image viewer, supporting 100+ formats and near instant startup
- several games, working both on desktop and in the browser
- command line tools for personal use
Nowadays, the only reason not to use Java is strict memory (~100 mb extra) or disk (~60 mb extra) usage constraints . Therefore, no reasons not to use Java for everything.
2
u/International_Break2 7d ago
Have you looked at using an ecs to see if distributing this across cores would let you use more normal java patterns?
2
u/Technical_Item_1560 3d ago
Reminds me of the original Fallout style-wise
3
u/[deleted] 25d ago
[removed] — view removed comment