DEV Community

Cover image for Writing a Game in Typescript

Writing a Game in Typescript

Daniel Schulz on June 14, 2022

I know people make all kinds of stuff with <canvas> but that's all black magic to me. Time to change that. This is how I built Attacke!, a si...
Collapse
 
talenttinaapi profile image
talent

This is awesome Daniel, I will surely contribute a few features soon.

Collapse
 
iamschulz profile image
Daniel Schulz

Awesome! Looking forward to it :)

Collapse
 
jzombie profile image
jzombie

This is cool. I skimmed the article and noticed one thing about the requestAnimationFrame. I think that the fps is contingent upon the refresh rate of the monitor - whatever load you're putting behind it.

So, without a load at all, on a 120Hz monitor, I believe you would get 120 fps.

I'd be interested in knowing if this is incorrect.

Collapse
 
iamschulz profile image
Daniel Schulz

It is. My external display maxes out at 75Hz, my Macbook does 120Hz. Browsers won't go higher than 60fps either way (30 on MacOS with energy saver). That is also true for all CSS animations, GPU-acceleratet or not.

Collapse
 
jzombie profile image
jzombie • Edited

Seems that Chrome is an exception to this rule:

As a test, I opened this site testufo.com/framerates-versus using Chrome on my Macbook M1 Pro, in a dual-screen configuration, the other monitor running at an apparent 60 fps.

The tests were measured by taking the window and moving from one screen to the other w/o refresh, while the M1 was plugged in (I didn't measure on battery).

[120 fps] dev-to-uploads.s3.amazonaws.com/up...
[60 fps] dev-to-uploads.s3.amazonaws.com/up...

Safari and Firefox maintained 60 fps for both, even if doing a hard refresh.


I skimmed this post to find the link to begin with: bugs.webkit.org/show_bug.cgi?id=17...

Thread Thread
 
jidayyy profile image
Julien hey

agree !

Thread Thread
 
parzavil3 profile image
Parzavil3

Freaking brilliant thanks for the great inspiration you actually gave me. I grew m uo to feel alone and youreminded me with this post why i love it

Collapse
 
drsensor profile image
૮༼⚆︿⚆༽つ • Edited

Congratulations and well done!
You deserve a medal 🥇
Also nice diagram 👍

💯 Lighthouse!?
When someone bring me that metrics I've always question how Lighthouse calculate those scores 😆

For the next step maybe you want try using ECS. Here some list

Any ECS library should be fine (except ECSY). Speaking of WebRTC, here an example combining ECS with WebRTC

Collapse
 
iamschulz profile image
Daniel Schulz

Lihhthouse scores are easier to get to 100 when you try to not mess up things instead of adding performance features :)

I haven't heard of ECS yet, but I'm no real game dev either. It sounds like I'm already halfway there. It would mean e.g. to put all positioning data into its own structure, right? I can see how that would be beneficial in games with a high number of players, but how would that pattern be batter than what I have now with only a handful of players?

Collapse
 
drsensor profile image
૮༼⚆︿⚆༽つ

Bullet-hell is one type of game that work best when using ECS even though it's single player. Every type of bullets and obstacles are modeled as ECS components. For example modeling them based on the effect and damage then we can model it as component fire, ice, poison, and physical. Ice will have parameter slow down in percent and ice damage per hit. Poison will have random move chance and poison damage per second. And so on. After that you just need to put each logic/implementation in different ECS systems.

Even if the game only has small amount of entities, it can still benefit from how easy the data can be serialized. Make it suitable in multiplayer game for states synchronization.

Thread Thread
 
jidayyy profile image
Julien hey

agree !

Collapse
 
andrewbaisden profile image
Andrew Baisden

Wow thats so cool! TypeScript and JavaScript can be used to make serious games. Like Danger Crew on Steam which was made with a JavaScript tech stack.

Collapse
 
andrerodrigues profile image
andrerodrigues

Cool. I will go read with more calm it tutorial.
Congratulations!

Collapse
 
cthulhudev profile image
CthulhuDev

Why setInterval and not RAF? Nice article btw!

Collapse
 
iamschulz profile image
Daniel Schulz

I did use requestAnimationFrame, which is the whole reason behind the frame skip mechanic.

Collapse
 
suchintan profile image
SUCHINTAN DAS

Awesome post Daniel. I can't deny this fact that this post actually made me amaze. Keep going ! You earned a new follower 🤗.

Collapse
 
gabrielepiaggesi profile image
Gabri

One of the best article on this platform, really well done, congrats!

Collapse
 
retr0ville profile image
Retroville

Cool stuff

Collapse
 
lostigeros profile image
Patryk L.

That was a really nice to read, have fun with WebRTC ;)

Collapse
 
metafox0114 profile image
MetaFox

Well Done Daniel.
Awesome Article.
Keep on going. I will wait your article.
Best regards.

Collapse
 
mupin profile image
Ahmad Muwaffaq

Awesome work, Daniel! I'm thinking the same to build a game to learn everything about Web API's and logic. Probably will use your repo as a reference 😁