<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Alexander Saltykov on Medium]]></title>
        <description><![CDATA[Stories by Alexander Saltykov on Medium]]></description>
        <link>https://medium.com/@alex.saltykov?source=rss-c403d0cd5604------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*qJQaqDQ1l9Loa7a36F_1aQ.png</url>
            <title>Stories by Alexander Saltykov on Medium</title>
            <link>https://medium.com/@alex.saltykov?source=rss-c403d0cd5604------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sun, 22 Nov 2026 13:52:41 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@alex.saltykov/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[The hell is wrong with you guys?]]></title>
            <link>https://medium.com/@alex.saltykov/the-hell-is-wrong-with-you-guys-91e2136ee480?source=rss-c403d0cd5604------2</link>
            <guid isPermaLink="false">https://medium.com/p/91e2136ee480</guid>
            <dc:creator><![CDATA[Alexander Saltykov]]></dc:creator>
            <pubDate>Thu, 10 Apr 2025 09:44:27 GMT</pubDate>
            <atom:updated>2025-04-10T09:44:27.033Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*vupxlLimvxFyspHMrHBuUA.png" /></figure><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=91e2136ee480" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Long journey to perfect notes]]></title>
            <link>https://medium.com/@alex.saltykov/long-journey-to-perfect-notes-be0e9deda4b7?source=rss-c403d0cd5604------2</link>
            <guid isPermaLink="false">https://medium.com/p/be0e9deda4b7</guid>
            <category><![CDATA[open-source]]></category>
            <category><![CDATA[notetaking]]></category>
            <category><![CDATA[journalling]]></category>
            <dc:creator><![CDATA[Alexander Saltykov]]></dc:creator>
            <pubDate>Tue, 18 Mar 2025 07:52:36 GMT</pubDate>
            <atom:updated>2025-03-19T07:46:54.937Z</atom:updated>
            <content:encoded><![CDATA[<p>TL;DR it’s still not finished.</p><p>Two years ago I decided to start journalling. Not to maintain mental health but rather to keep every aspect of my work in place. There’s a ton of things I need to keep track on and remember. And you know what? I’m not satisfied with note taking apps. Majority of them are freemium and hide the most wanted features under paywall, others do not allow you to own your notes. There are, of course, some great apps like Joplin (and it almost perfect, tbh) but their design makes me sad panda. I even attempted to fix this by forking Joplin code and tweaking styles but my oh my, reading it’s source code is a pain.</p><p>It seemed obvious to me that the only way to have good-looking tool that is also comfortable and functional is to make my own note taking app.</p><p>Yeah…</p><p>Due to lack of time and all the AI hype around, I decided to play with bolt.new and see if I could build one this way.</p><p>So what is a perfect notes app? It should:</p><ol><li>Be free</li><li>Be open source</li><li>Be self-hosted</li><li>Have customizable typography (because fonts are the most underestimated thing in interfaces, in my opinion)</li><li>Be minimalistic</li><li>Allow to hide certain information</li><li>Have zen mode that hides everything except the text you’re writing</li><li>Have synchronization feature</li><li>Provide E2E encryption</li><li>Allow you to write down text by hand (and to draw too)</li></ol><p>There should also be possible to use tags for notes (<strong>top requirement</strong>) and flexible search system.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*wL6z7aqmfuMi4qg0QNBbEg.jpeg" /></figure><p>In further blog posts I’ll reveal the process of creating a perfect solution and right now you can play with the early version <a href="https://comforting-starlight-b74676.netlify.app/">here</a>. Wish me luck and join me in this journey.</p><p>Oh, and don’t hesitate filing issues in the <a href="https://github.com/johnsamilin/solo">repo</a> (or drop me a line in telegram: <a href="http://twitter.com/WatasheeBaka">@WatasheeBaka</a>)!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=be0e9deda4b7" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Philosophic questions of webapps internationalization]]></title>
            <link>https://medium.com/@alex.saltykov/philosophic-questions-of-webapps-internationalization-7738a964152e?source=rss-c403d0cd5604------2</link>
            <guid isPermaLink="false">https://medium.com/p/7738a964152e</guid>
            <category><![CDATA[community]]></category>
            <category><![CDATA[challenge]]></category>
            <category><![CDATA[web-apps]]></category>
            <category><![CDATA[frontend]]></category>
            <category><![CDATA[i18n]]></category>
            <dc:creator><![CDATA[Alexander Saltykov]]></dc:creator>
            <pubDate>Mon, 17 Apr 2023 07:40:40 GMT</pubDate>
            <atom:updated>2023-04-17T07:40:40.464Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*NhKLOs1a2dlpI0vk1BYEqg.jpeg" /></figure><p>When dealing with translations, you’re dealing with ton of tricky questions, like what languages to support, how do you get messages translated and how do you detect locale. What seemed like a quick easy task becomes a burden. Here are some things to keep in mind before you start translating your app.</p><p><strong>Languages</strong></p><p>If you support two or three languages, it’s not a big deal. But if you want to support a dozen of variants, you have to decide somehow which ones. Good thing to start is to google top languages by popularity, but this option is not precise at all. Better look at statistics or analytical reports, that will give you an insight on which languages your users speak. In <a href="https://github.com/johnSamilin/rosplata">Rosplata</a>, I don’t collect that kind of data, so I needed another start point. I had to be proactive and just asked potential users: “Hey guys, what languages do you expect to see in settings page?”. Honestly, the answer was unexpected. Arabic, Hebrew, Hindi and Bengali!</p><p><strong>Detecting</strong></p><p>When new user lands in the app, he may not to speak english, which is default language. So there must be some way to guess. And there is one: use <a href="https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Accept-Language">Accept-Language header</a> at server side. This header not just tells you which locale to use, but also gives you a hint, like, this user speaks the language A and languages B (I’m 90% sure) and C (I’m 50% sure). With this information you can be much more confident: if you don’t support user’s primary language, you can probably use additional ones.</p><p><strong>Selecting</strong></p><p>When user logs in and changes locale, you have to remember this choice. For example I <a href="https://github.com/johnSamilin/rosplata-back/blob/feature/translations/src/api/users/users.controller.ts#L50">set http-only cookie</a> indicating user’s preference. Next time he requests index.html from server, he’ll get one based on this cookie value.</p><p>At the same time, user most likely has several devices. If you change language on the smartphone, you’d expect the app to be translated to that language wherever you open it. That adds one layer more. In the end, here’s decision algorithm:</p><ol><li>Check cookie</li><li>If there’s no preferred language in cookie, check what’s in DB</li><li>If DB doesn’t know what language user selected, check Accept-Language header</li><li>In any other case, use default locale</li></ol><p><strong>Services vs community. Static files vs downloading</strong></p><p>There is a lot of i18n services around, like <a href="https://weblate.org/">https://weblate.org/</a> or <a href="https://contentech.com/the-rise-of-using-crowdsourcing-and-community-based-translation/">Contentech</a>. Using them is a good way to get fine quality translations. And it’s probably the best solution. But in my case, I don’t have the ability to pay for such services. And I decided to ask community for help. It seemed like a great way to involve potential users in the process of building the app they are using!</p><p>One more thing to think about is whether you are ready to download fresh translations from some service each time someone’s requesting HTML page or ‘bake’ pre-translated files and serve them as static? For me the answer was pretty obvious:</p><p><em>If you can save 10ms of request handling time, you must do it.</em></p><p><strong>Conclusion</strong></p><p>I hope this article helped you plan your next steps in this hard (but important) way.</p><p>By the way, if you’re speaking language other than english, I encourage you to help my opensource project <a href="https://github.com/johnSamilin/rosplata">Rosplata</a> by either <a href="https://github.com/johnSamilin/rosplata/wiki/Translations">submit new locale</a> or review existing ones (because there might be inaccuracies).</p><p>And subscribe to my <a href="https://t.me/rosplata">telegram channel</a> with all the announcements and cool stuff!</p><p>Love y’all!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=7738a964152e" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Hidden beauty of Chrome]]></title>
            <link>https://medium.com/@alex.saltykov/hidden-beauty-of-chrome-8cbd3196a218?source=rss-c403d0cd5604------2</link>
            <guid isPermaLink="false">https://medium.com/p/8cbd3196a218</guid>
            <category><![CDATA[web-development]]></category>
            <dc:creator><![CDATA[Alexander Saltykov]]></dc:creator>
            <pubDate>Fri, 17 Feb 2023 09:04:37 GMT</pubDate>
            <atom:updated>2023-02-17T09:04:37.680Z</atom:updated>
            <content:encoded><![CDATA[<p>Without further hesitations, watch this:</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fwww.youtube.com%2Fembed%2FOIls0eCwmjA%3Ffeature%3Doembed&amp;display_name=YouTube&amp;url=https%3A%2F%2Fwww.youtube.com%2Fwatch%3Fv%3DOIls0eCwmjA&amp;image=https%3A%2F%2Fi.ytimg.com%2Fvi%2FOIls0eCwmjA%2Fhqdefault.jpg&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=youtube" width="854" height="480" frameborder="0" scrolling="no"><a href="https://medium.com/media/c712f6af41da2cb6f1a539403f5e86b4/href">https://medium.com/media/c712f6af41da2cb6f1a539403f5e86b4/href</a></iframe><p>Noticed?</p><p>I changed content of the module, then clicked the button again and the new code worked without reloading.</p><p>There’s no webpack and websockets, just plain ES6 modules. More than that, there was no additional module request. You can see that in the Network section of DevTools. Looks like magic for me.</p><p>It’s the setting called <a href="https://developer.chrome.com/docs/devtools/workspaces/">Workspaces</a> (<a href="https://www.bitdegree.org/learn/chrome-workspaces">addintional info</a>) and it’s been around since 2018. It even works both ways: changes to JS in Sources tab or CSS in Elements tab can be persisted to corresponding files in the filesystem.</p><p>How cool is that?</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=8cbd3196a218" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[How do I make web apps nowadays?]]></title>
            <link>https://medium.com/@alex.saltykov/how-do-they-make-web-apps-nowadays-pt-1-c1a36acc7dd8?source=rss-c403d0cd5604------2</link>
            <guid isPermaLink="false">https://medium.com/p/c1a36acc7dd8</guid>
            <category><![CDATA[modern-frontend]]></category>
            <category><![CDATA[js]]></category>
            <category><![CDATA[modern-browser-apis]]></category>
            <dc:creator><![CDATA[Alexander Saltykov]]></dc:creator>
            <pubDate>Fri, 06 Jan 2023 16:24:02 GMT</pubDate>
            <atom:updated>2023-02-13T10:09:25.878Z</atom:updated>
            <content:encoded><![CDATA[<p>Hey guys, merry christmas! I work as a web developer for 13 years already. I remember the days when jQuery was so popular that it influenced web platform itself, making <em>document.querySelector</em> real. Last year was time of reflection for me, so I was surprized how much comfortabler browser APIs became: it’s like when you use all those bundlers, transpilers, autoprefixers etc you use not web platform but something different, something that lives and evolves by it’s own rules. Modern browsers can load js modules asyncronously, cache page’s state, track device’s performance, react on user’s preferences and much more; there’s number of APIs from NFC to gamepads.</p><p>But there’s one little tricky question bothers me: how do we make web apps when UFO lands and steals all of our tools? I’m currently trying to make webapp with such limitations (for fun) — <a href="https://github.com/johnSamilin/rosplata">ros-plata.ru</a></p><h4>1. Use HTML templates</h4><p>Typical web app constructs it’s UI dynamically, constantly calling web page reflow and style recalculation events. By the time, <a href="https://caniuse.com/template">html temlates</a> for the long time already. They are being parsed by the browser, but not being rendered immediately. It’s much easier for the browser to clone part of the document rather than parse it once again.</p><blockquote>Why you won’t like it?<br>After cloning you’ll have to fill the template with data and attach event listeners using DOM APIs.</blockquote><h4>2. Write modular js</h4><p><a href="https://caniuse.com/es6-module">ES6 modules</a> — is a reality for as long as 5 years already. It’s not just the way to split you app and load it piece by piece when you need it. It’s the way to free the main thread and keep UI interactive. Browser engine won’t stop parsing process and the user will get page as soon as possible. More than that: once loaded, the module won’t be fetched again. It looks like deduplication out of the box! Plus tree shaking, because undemanded modules won’t be loaded at all.</p><p>Cool bonus — for local development saving the file will deliver new version of the module to the page next time browser tries to execute code that it exports (hot reloading out of the box!).</p><blockquote>Why you won’t like it?<br>You’ll have to think about script priorities and the way they influence total load time. It’s also a possibility to fine tune your app though.</blockquote><h4>3. Consider the circumstances</h4><p>Low bandwidth, no connection, low battery level — these are the things browsers can react to.</p><p><a href="https://developer.mozilla.org/en-US/docs/Web/API/NetworkInformation"><em>Network information API</em></a><em> &amp; </em><a href="https://developer.mozilla.org/en-US/docs/Web/API/Window/matchMedia"><em>matchMedia</em></a><em>(‘prefers-reduced-data’) gives you possibility to know if there is a connection, what’s it’s speed and to to reduce the amount of data that you transfer through the network. For example, you can sacrifice some things like images or video and make your app work faster.</em></p><p>With <a href="https://developer.mozilla.org/en-US/docs/Web/API/Battery_Status_API"><em>Battery status API</em></a><em> &amp; matchMedia(‘prefers-reduced-motion’) you can turn off things that spend battery charge (first of all, animations).</em></p><h4>4. Be one step ahead of the browser</h4><p>With modular js you’ll face the problem of too long dependency chains. Root module loads another, it loads number of child modules and… you got it. Possible solution is to use <a href="https://developer.chrome.com/blog/early-hints/">early hints</a>. It’s the way to tell the browser which resources it will need in near future. They will be loaded in parallel. In the end it looks like bunding but without bundling.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*62vGM1mO3I-VHdekpcMKwA.png" /><figcaption>Without early hints</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*DdU-lzf5GZA0VIMAS0mRGg.png" /><figcaption>With early hints</figcaption></figure><blockquote>Why you won’t like it?<br>You will need an http/2 server. And it works only for navigation requests.</blockquote><h4>5. Use URL pattern API</h4><p>In fact, you don’t need react router (or any other router) for routing. <a href="https://caniuse.com/mdn-api_urlpattern">URL pattern API</a> lets you parse urls in the way you do it with popular rounting libs (declare variables in URL parts and extract them easily). It’s not widely supported yet but looks very promising.</p><h4>6. Use import assertions</h4><p>Modular javascript is cool, but how to import css files from inside a module as you do it with webpack loaders? <a href="https://caniuse.com/mdn-javascript_statements_import_import_assertions">Import assertions</a> may come handy. It’s an API that lets you write something like import Style from &#39;./styles/style.css&#39; assert { type: &#39;css&#39; } and browser will load it just as it would load js module. Keep in mind that this feature is non-standard yet.</p><p>Thank you for reading this article! Let’s discuss it in comments! Stay tuned for next chapters.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=c1a36acc7dd8" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Making economic RTS with React and Babylon. Part 2]]></title>
            <link>https://medium.com/@alex.saltykov/making-economic-rts-with-react-and-babylon-part-2-8e92ef25ab73?source=rss-c403d0cd5604------2</link>
            <guid isPermaLink="false">https://medium.com/p/8e92ef25ab73</guid>
            <category><![CDATA[procedural-generation]]></category>
            <category><![CDATA[gamedev]]></category>
            <category><![CDATA[react]]></category>
            <category><![CDATA[babylonjs]]></category>
            <category><![CDATA[webgl]]></category>
            <dc:creator><![CDATA[Alexander Saltykov]]></dc:creator>
            <pubDate>Wed, 01 Dec 2021 14:13:32 GMT</pubDate>
            <atom:updated>2021-12-01T14:16:55.175Z</atom:updated>
            <content:encoded><![CDATA[<p>Hi everyone, welcome back to my WebGL gamedev blog. Today we’ll talk about terrain: how to create one for your game and how to let units find their way across it.</p><p><strong>Step 3. Searching for proper terrain</strong></p><p>When it comes to RTS, as a player you don’t usually pay much attention to the landscape. But. It doesn’t mean that it’s something unnecessary. I think there’s always hard decision: either you do fine-grained handcrafted predictable landscape or unique procedural generated experience. In the first case you get confidence that every corner of the map is reachable, but it’s hard to create and it will weight a lot of MB. The second one is (in my opinion) much easier to create but there is a chance that there will be dead ends or unreachable areas.</p><p>If you just want to see the result, you may skip this part and go straight to the next chapter. But remember: here you may find an overview of the concepts you may need in the future.</p><p><em>Procedural way</em></p><p>Core idea of the procedural terrain generation is noise. This idea is so widely-used there are already helper components: <a href="https://brianzinn.github.io/react-babylonjs/?path=/story/babylon-basic--dynamic-terrain">Dynamic terrain</a> (react-babylonjs).</p><p>There are, of course, more complex examples. Let me give you brief overview.</p><p>1.<em> Height map</em></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/567/1*urnr2yAya9Vp8RhZjJYSPw.jpeg" /></figure><p>Actually, each map has at least two dimensions: length and width. So you can think of it as of matrix where each cell stores the information about height and then present this matrix in the form of 3d landscape. There is a nice read about how to generate source data for this approach: <a href="https://www.wesley-kerr.com/caves/">procedural cave generation</a> or even <a href="https://mewo2.com/notes/terrain/">random heightmap</a>. Babylon.js has built-in tool for this: <a href="https://doc.babylonjs.com/divingDeeper/mesh/creation/set/ground_hmap">Ground from heightmap</a>.</p><p>2. <em>Hill algorith</em></p><p>This algorithm allows you to create pretty good-looking landscape with mountains and valleys:</p><ul><li>Create matrix and initialize it with zeros</li><li>Get random point and random radius within certain extents. Those extents control whether landscape will be flatter or rocky</li><li>Lift the hill in this point</li><li>Go to second step and repeat it several times</li><li>Normalize landscape</li><li>Make valleys</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/440/1*BXwpsuLIzAQ9ijFWSpMkIg.jpeg" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/500/1*T931y3suD4jTzixlTeScOA.jpeg" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/500/1*75LUbujlO-wysfFtSZNQrw.jpeg" /></figure><p>You may find the details in the Resources section below.</p><p>3. <em>FBM in SDF</em></p><p><a href="https://iquilezles.org/www/articles/fbmsdf/fbmsdf.htm">Fractional Brownian Motion in Signed Distance Fields</a>. Whatever it means :)</p><p>Hardcore alternative to default noise method for generating realistic rocky landscape.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/960/1*iR0o0xcjn8Al-Z08plz4hw.jpeg" /></figure><p>4. <em>Fractal planets</em></p><p>For some reasons you may need to represent spherical or lumpy heavenly bodies from asteroids to suns with realistic looking topography and features, which are viewable across all scales from surface to space at flight-simulator levels of detail, rendered with convincing atmosphere, water, clouds, haze.</p><p>In this case, check out this marvelous read about <a href="https://acko.net/blog/making-worlds-1-of-spheres-and-cubes/">making planets and asteroids</a>. Also, watch this: <a href="https://www.youtube.com/playlist?list=PLRL3Z3lpLmH3PNGZuDNf2WXnLTHpN9hXy">3d world generation</a> (JavaScript) playlist.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/959/1*14lhlpWzWgwlDOXwUJLxDQ.jpeg" /></figure><p>5. <em>Voronoi tesselations</em></p><p>Finally, my favorite link: <a href="http://www-cs-students.stanford.edu/~amitp/game-programming/polygon-map-generation/">Polygonal map generation</a> allows you to generate worlds that consist of hexagonal tiles and spread different biomes and rivers across it. I promise you won’t regret reading this.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/600/1*-8Sw3sxacU8RVRWoIFC5vg.png" /></figure><p><em>Manual way</em></p><p>If you prefer more control on the final result, there are a couple of tools available.</p><p>1.<em> World Machine</em></p><p><a href="https://www.world-machine.com/">World machine</a> is a powerful tool for simulating geographical data even including erosion. It allows you to mark areas in declarative way and control their appearance.</p><p>2. <em>World Creator</em></p><p><a href="https://www.world-creator.com/features/">World Creator</a> is the first multi-platform, fully GPU powered terrain and landscape generator available on this planet.</p><p>3. <em>Elevation API</em></p><p><a href="https://elevationapi.com/">Elevation API</a> is needed when you want to use a piece of real world geographical data in your game. Just mark area on the map, export it in sketchfab and then download it as GLTF file. That’s it.</p><p>After some thinking I came out to the fact that Elevation API is the most convenient, cool and easy method for this series of articles.</p><p><strong>Step 4. Model loading</strong></p><p>GLTF models, especially with geographical data, will be too heavy to include them into the bundle, so with the help of parcel-plugin-static-files-copy (more info <a href="https://medium.com/@alex.saltykov/how-do-they-make-brower-games-nowadays-part-1-3a18b9b04a4b">here</a>) we will be loading them asynchronously. To use them in the scene we’ll need <em>Model</em> component from react-babylonjs. Just create <em>Ground</em> component with the following lines:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/d5ca36d810011e266e7188b3b87df730/href">https://medium.com/media/d5ca36d810011e266e7188b3b87df730/href</a></iframe><p>And add it to <em>index.tsx</em>.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*IDjJNCQpfzOkxx8r51mOsw.png" /></figure><p><strong>Step 5. Pathfinding</strong></p><p>Last thing I’d like to talk about today is pathfinding. Given that your map has passable and unpassable areas (circumstances, like buildings) you’ll need a way to navigate your units. And that’s not just “go there by straight line”. It’s “find a way and if you <em>can</em> go there, do so”. A* algorithm is the thing that’s gonna solve this problem. There is a number of implementations of this algorithm, like Pathfinding.js; I even implemented it myself and compiled to WebAssembly (using <a href="https://github.com/johnSamilin/assemblyscript-pathfinding">AssemblyScript</a> and <a href="https://github.com/johnSamilin/go-pathfinding">Go</a>). But Babylon.js offers more rich and convenient alternative —<a href="https://doc.babylonjs.com/typedoc/classes/babylon.recastjsplugin"> Recast</a>.</p><p>To make it work we’ll need to add such components as <em>Pickable</em>, <em>Selectable </em>and <em>Navigatable</em> in order to have possibility to select units and give them commands to go to certain point. For now take a look at this demo from Babylon.js team to get the idea of how it will look like.</p><p><a href="https://playground.babylonjs.com/#6AE0RP">Babylon.js Playground</a></p><p>Thanks for reading the article! I hope you liked it. Here are some links to stay in touch: <a href="https://twitter.com/low_dencity">twitter</a>, <a href="https://www.linkedin.com/in/asaltykov">linkedin</a>, <a href="https://github.com/johnSamilin">github</a>.</p><p><strong>Index</strong></p><ol><li><a href="https://medium.com/@alex.saltykov/making-economic-rts-with-react-and-babylon-770e85c96e9d">Introduction</a></li><li><a href="https://medium.com/@alex.saltykov/making-economic-rts-with-react-and-babylon-770e85c96e9d">Part 1. Setup and Camera controls</a></li><li>Part 2. Procedural terrain and Pathfinding</li></ol><p><strong>Resources</strong></p><ol><li><a href="https://techblog.geekyants.com/recreating-real-world-terrain-with-react-threejs-and-webgl-shaders-1">Recreating World Terrain with React, Three.js and Shaders</a>, <a href="https://hashnode.com/@RishabhKarnad">Rishabh Karnad</a> (2021)</li><li><a href="http://www-cs-students.stanford.edu/~amitp/game-programming/polygon-map-generation/">Polygonal Map Generation for Games,</a> Amit Patel (2010)</li><li><a href="https://www.wesley-kerr.com/caves/">Procedural Caves Generation,</a> Wesley Kerr (2014)</li><li><a href="https://acko.net/blog/making-worlds-1-of-spheres-and-cubes/">Procedural Planets</a>, Steven Wittens (2009)</li><li><a href="https://web.archive.org/web/20150317105255/http://graphics.tudelft.nl/~ruben/RMSmelik3AMIGAS09.pdf">A Survey of Procedural Methods for Terrain Modelling</a>, Ruben M. Smelik, Klaas Jan de Kraker and Saskia A. Groenewegen, Tim Tutenel and Rafael Bidarra (2015)</li><li><a href="https://mewo2.com/notes/terrain/">Generating Fantasy Maps</a>, Martin O’Leary</li><li><a href="https://www.world-creator.com/features/">World Creator</a></li><li><a href="https://elevationapi.com/">Elevation API</a></li><li><a href="https://www.world-machine.com/">World Machine</a></li><li><a href="https://www.ixbt.com/video/3dterrains-generation.shtml">3D Terrain Generation</a>, Denis Kozhuhov (2001) — twenty-years-old goodness</li><li><a href="https://www.youtube.com/playlist?list=PLRL3Z3lpLmH3PNGZuDNf2WXnLTHpN9hXy">3D World Generation youtube playlist</a></li><li><a href="https://iquilezles.org/www/articles/fbmsdf/fbmsdf.htm">FBM Details in SDFs</a>, Inigo Quilez (2019)</li><li><a href="https://github.com/johnSamilin/assemblyscript-pathfinding">AssemblyScript A* implementation</a></li><li><a href="https://github.com/johnSamilin/go-pathfinding">Go A* implementation</a></li></ol><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=8e92ef25ab73" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Making economic RTS with React and Babylon. Part 1]]></title>
            <link>https://medium.com/@alex.saltykov/making-economic-rts-with-react-and-babylon-part-1-f63c2a0dc60e?source=rss-c403d0cd5604------2</link>
            <guid isPermaLink="false">https://medium.com/p/f63c2a0dc60e</guid>
            <category><![CDATA[react]]></category>
            <category><![CDATA[indie-game]]></category>
            <category><![CDATA[gamedev]]></category>
            <category><![CDATA[webgl]]></category>
            <category><![CDATA[babylonjs]]></category>
            <dc:creator><![CDATA[Alexander Saltykov]]></dc:creator>
            <pubDate>Thu, 25 Nov 2021 12:22:36 GMT</pubDate>
            <atom:updated>2021-12-01T14:17:16.245Z</atom:updated>
            <content:encoded><![CDATA[<p>So as I promised, here’s the first part of development process.</p><p><strong>Step 1. Setup</strong></p><p>Luckily, <a href="https://medium.com/@alex.saltykov/how-do-they-make-brower-games-nowadays-part-1-3a18b9b04a4b">previous experience</a> with Parcel was good, there was absolutely no need in configuration, so I’d be more than happy to use it this time too. But instead of using Three.js and react-three-fiber, I suggest to use Babylon.js and react-babylonjs. As always, create a folder and run these commands:</p><pre>npm init -y<br>npm i --save-dev @babel/core @babel/preset-react @types/babylonjs @types/lodash @types/react @types/react-dom parcel-bundler typescript<br>npm i --save @babylonjs/core@5.0.0-alpha.62 @babylonjs/gui @babylonjs/loaders@5.0.0-alpha.62 lodash react react-babylonjs react-dom recast-detour xstate zustand</pre><p>Note that we’re gonna use alpha version of Babylon.js because it has one particular feature that will help us a lot: <a href="https://github.com/BabylonJS/Babylon.js/pull/10789">navigation observer</a>.</p><h3>Alexander Saltykov on Twitter: &quot;dear @babylonjs thanks for your coolinness, but I have a claim to your nav system: why there is no way to know when an agent reached destination? / Twitter&quot;</h3><p>dear @babylonjs thanks for your coolinness, but I have a claim to your nav system: why there is no way to know when an agent reached destination?</p><p>Also don’t forget to add <em>start</em> script in <em>package.json</em>:</p><pre>&quot;start&quot;: &quot;parcel ./index.html&quot;</pre><p>And of course <em>index.html</em> itself:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/85c451101045464f99d6e8a5e9735c19/href">https://medium.com/media/85c451101045464f99d6e8a5e9735c19/href</a></iframe><p>The whole project that is ready to explore you will find <a href="https://codesandbox.io/s/twilight-snowflake-sc098?file=/src/index.tsx">here</a>.</p><p>Take a look at <em>index.tsx</em>, our starting point for now:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/9669b3fd0f1fe16223cadbc4dc7f6b39/href">https://medium.com/media/9669b3fd0f1fe16223cadbc4dc7f6b39/href</a></iframe><p>Here we declare our root component, Game. Mainly it will be constantly checking whether you won or lost (<em>checkWin/checkLose</em>) and set starting conditions (<em>startBlds/startResources/startUnits</em>) once mounted. The idea is that you could change those conditions and get absolutely another mission while core rules remain the same.</p><p><strong>arcRotateCamera</strong> component behaves just like <strong>orbitControls</strong> from three.js: hold left button and move the mouse to rotate camera around certain point; hold right button to move that point and camera in XZ plane; use wheel to zoom in and out.</p><p>Now npm start command will start the server and if you navigate to localhost:1234, you will see the following scene:</p><p><a href="https://sc098.csb.app/">Sandbox - CodeSandbox</a></p><p><strong>Step 2. Camera movement</strong></p><p>Ok, I think if you played any strategy, you already know the rules. You should be able to move camera freely by moving cursor near the edges of the screen. How’d you do that? I already described my <a href="https://medium.com/@alex.saltykov/how-do-they-make-browser-games-nowadays-part-2-8e7dcd50d6c0">pretty simple solution</a> to this. Good thing is that it’s almost independent to engine.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/255885b99e84652dba7e9867c1daefc4/href">https://medium.com/media/255885b99e84652dba7e9867c1daefc4/href</a></iframe><p>And you know what? This component works with cameras too. If you wrap <em>arcRotateCamera </em>component with <em>Movable</em> and implement local state that increases or decreases <em>velocity</em> prop and takes care of <em>heading</em> prop when your mouse cursor reaches certain area on the screen, you may expect the behavior I just described.</p><p><a href="https://9o02k.csb.app/">Sandbox - CodeSandbox</a></p><p>Check out the code for this step: <a href="https://codesandbox.io/s/economic-rts-2-9o02k?file=/src/Camera.tsx">https://codesandbox.io/s/economic-rts-2-9o02k?file=/src/Camera.tsx</a></p><p>Next important step contains pretty big topic —models loading, procedural terrain generation and basic pathfinding. I guess it deserves a separate article, so see you later! Don’t be mad :)</p><p>Thanks for reading the article! I hope you liked it. Here are some links to stay in touch: <a href="https://twitter.com/low_dencity">twitter</a>, <a href="https://www.linkedin.com/in/asaltykov">linkedin</a>, <a href="https://github.com/johnSamilin">github</a>.</p><p><strong>Index</strong></p><ol><li><a href="https://medium.com/@alex.saltykov/making-economic-rts-with-react-and-babylon-770e85c96e9d">Introduction</a></li><li>Part 1. Setup and Camera controls</li><li><a href="https://medium.com/@alex.saltykov/making-economic-rts-with-react-and-babylon-part-2-8e92ef25ab73">Part 2. Procedural terrain and Pathfinding</a></li></ol><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=f63c2a0dc60e" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Making economic RTS with React and Babylon]]></title>
            <link>https://medium.com/@alex.saltykov/making-economic-rts-with-react-and-babylon-770e85c96e9d?source=rss-c403d0cd5604------2</link>
            <guid isPermaLink="false">https://medium.com/p/770e85c96e9d</guid>
            <category><![CDATA[indie-game]]></category>
            <category><![CDATA[gamedev]]></category>
            <category><![CDATA[experiment]]></category>
            <category><![CDATA[react]]></category>
            <category><![CDATA[webgl]]></category>
            <dc:creator><![CDATA[Alexander Saltykov]]></dc:creator>
            <pubDate>Mon, 22 Nov 2021 09:30:58 GMT</pubDate>
            <atom:updated>2021-12-01T14:17:38.085Z</atom:updated>
            <content:encoded><![CDATA[<p>When I was much younger, I used to play <em>a lot</em>. My number one preference was <a href="https://store.steampowered.com/app/253900/Knights_and_Merchants/">Knights &amp; Merchants</a>. While all my friends liked battle scenes most, I was fond of economical aspects of this game. For me it was always cool to look after the villagers who run their own business, become hungry sometimes and work together in the name of their country’s welfare. Recently I bought it in Steam store and thought that I could even do a <em>webmake </em>of this game.</p><p>As a first step of this dangerous journey it would be cool to take one of the core mechanics and try to re-implement it with the tools I’m already comfortable with: React and WebGL. It’s also a great possibility to test drive <a href="https://www.babylonjs.com/">Babylon.js</a>, which is in my to-do list.</p><p><strong>Design</strong></p><p><em>So what can player do in this newly born game?</em></p><ol><li>Get the information about available resources</li><li>Construct buildings that consume one type of resource and produce another</li><li>Organize supply chains</li></ol><p><em>What should player do to win?</em></p><p>Collect certain amount of resource.</p><p><em>What should player do to lose?</em></p><p>Spend all the resource reserve.</p><p>Also NPCs won’t be controlled by the user, they will make their own decisions based on the current economical situation.</p><p>In the next couple of articles I’ll try to cover every aspect of the development process. Stay tuned and here is the glance of the final result:</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fwww.youtube.com%2Fembed%2FiEEuwIOx69E%3Ffeature%3Doembed&amp;display_name=YouTube&amp;url=https%3A%2F%2Fwww.youtube.com%2Fwatch%3Fv%3DiEEuwIOx69E&amp;image=https%3A%2F%2Fi.ytimg.com%2Fvi%2FiEEuwIOx69E%2Fhqdefault.jpg&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=youtube" width="640" height="480" frameborder="0" scrolling="no"><a href="https://medium.com/media/bda97fe9efcc88cc180c11aa63bd16ef/href">https://medium.com/media/bda97fe9efcc88cc180c11aa63bd16ef/href</a></iframe><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fwww.youtube.com%2Fembed%2F2AurW1UacHU%3Ffeature%3Doembed&amp;display_name=YouTube&amp;url=https%3A%2F%2Fwww.youtube.com%2Fwatch%3Fv%3D2AurW1UacHU&amp;image=https%3A%2F%2Fi.ytimg.com%2Fvi%2F2AurW1UacHU%2Fhqdefault.jpg&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=youtube" width="640" height="480" frameborder="0" scrolling="no"><a href="https://medium.com/media/e058ce6d0672900654af51673bb36f9c/href">https://medium.com/media/e058ce6d0672900654af51673bb36f9c/href</a></iframe><p>Thanks for reading the article! I hope you liked it. Here are some links to stay in touch: <a href="https://twitter.com/low_dencity">twitter</a>, <a href="https://www.linkedin.com/in/asaltykov">linkedin</a>, <a href="https://github.com/johnSamilin">github</a>.</p><p><strong>Index</strong></p><ol><li>Introduction</li><li><a href="https://medium.com/@alex.saltykov/making-economic-rts-with-react-and-babylon-part-1-f63c2a0dc60e">Part 1. Setup and Camera controls</a></li><li><a href="https://medium.com/@alex.saltykov/making-economic-rts-with-react-and-babylon-part-2-8e92ef25ab73">Part 2. Procedural terrain and Pathfinding</a></li></ol><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=770e85c96e9d" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[How do they make browser games nowadays? Part 3]]></title>
            <link>https://medium.com/@alex.saltykov/how-do-they-make-browser-games-nowadays-part-3-cd7bb0474ce4?source=rss-c403d0cd5604------2</link>
            <guid isPermaLink="false">https://medium.com/p/cd7bb0474ce4</guid>
            <category><![CDATA[indie-game]]></category>
            <category><![CDATA[webgl]]></category>
            <category><![CDATA[threejs]]></category>
            <category><![CDATA[gamedev]]></category>
            <dc:creator><![CDATA[Alexander Saltykov]]></dc:creator>
            <pubDate>Fri, 12 Nov 2021 14:02:05 GMT</pubDate>
            <atom:updated>2021-11-12T14:02:05.525Z</atom:updated>
            <content:encoded><![CDATA[<p>Two previous articles (<a href="https://medium.com/@alex.saltykov/how-do-they-make-brower-games-nowadays-part-1-3a18b9b04a4b">part 1</a> and <a href="https://medium.com/@alex.saltykov/how-do-they-make-browser-games-nowadays-part-2-8e7dcd50d6c0">part 2</a>) were manuals on how move load models and move things around. This time we’ll add more interactivity. Realtime statistics and collision detection, to be specific.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/882/1*0qLkuDv5X61Rc4ypXaET-w.png" /></figure><p><strong>Step 9. Collision detection</strong></p><p>This is probably the most challenging part of the journey. The thing is, there are <em>a lot</em> of ways you can check if two objects are colliding right now. Each one has it’s own pros and cons, and it’s your responsibility to choose, depending on your needs. If you’re curious, check Resources section below to find a list of hand-picked links to materials about this.</p><p>Here they are:</p><ol><li><strong>Raycasting</strong>. This is the method that comes to mind when you say ‘Collisions’. When you cast the spell, you already know the direction of the fireball. So you can draw a line in this direction (ray) and check if it intersects any other objects. This method is usually used for bullets (so called projectiles), but our fireball is not so fast, so this method does not fit.</li><li><strong>Spatial grid</strong>. Here you divide the world to cells of certain size and check which objects currently are in the same cell fireball is or in one of the nearby cells.</li><li><strong>Boid-like</strong>. This method is similar to raycasting. In fact, you <em>do</em> use rays here too, but multiple times in different directions.</li><li><strong>AABB method</strong>. Each object in your scene has it’s own bounding box, so there are ways to detect their intersection (using Three.js’s <a href="https://threejs.org/docs/?q=geometry#api/en/helpers/BoxHelper">BoxHelper</a>).</li><li><strong>Ammo.js</strong> With physics engine you are able to simulate the world, move objects there that represent your NPCs around and ask the engine whether or not they are currently colliding. This is probably the most accurate method but in the same time it’s the hardest one.</li><li><strong>Naive. </strong>Finally you can just iterate over all entities in the scene and check if on of the fireballs position is within some rectangle around enemy. This is the slowest and easiest way.</li></ol><p>I’d say, our game is 2.5D (Diablo-like), no object changes it’s Y coordinate. So the last method sounds like an easy solution that still fits us. <em>I promise to implement all of the other methods some day. Stay tuned.</em></p><p>So let’s go ahead and create this new <strong>Collidable </strong>component which will do the job:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/768/1*3FJxl6XHJ2MEiOEoLcHObQ.png" /><figcaption><a href="https://codesandbox.io/s/distracted-bird-56kyy?file=/src/hoc/Collidable.jsx">https://codesandbox.io/s/distracted-bird-56kyy?file=/src/hoc/Collidable.jsx</a></figcaption></figure><p>For that purpose it stores the reference to the object that is subject for collisions in global store.</p><p>Of course you have to wrap fireballs, enemies and the player in that component and create callbacks:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/768/1*YPpMFcRyS1kAm5HhwLGhoQ.png" /><figcaption><a href="https://codesandbox.io/s/distracted-bird-56kyy?file=/src/fireball/fireball.jsx:3490-4121">https://codesandbox.io/s/distracted-bird-56kyy?file=/src/fireball/fireball.jsx:3490-4121</a></figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/768/1*x1e0SIUL8bTn_3N5EA9X9A.png" /><figcaption><a href="https://codesandbox.io/s/distracted-bird-56kyy?file=/src/Zombie.jsx:622-689">https://codesandbox.io/s/distracted-bird-56kyy?file=/src/Zombie.jsx:622-689</a></figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/768/1*8bbX_V95UsfwvM2kOjrAGg.png" /><figcaption><a href="https://codesandbox.io/s/distracted-bird-56kyy?file=/src/Player.jsx:1123-1512">https://codesandbox.io/s/distracted-bird-56kyy?file=/src/Player.jsx:1123-1512</a></figcaption></figure><p><a href="https://56kyy.csb.app/">The Game</a></p><p><strong>Step 10. Stats</strong></p><p>Now, all of the code that is relative to webGL must be placed inside Canvas component. But what if we place some HTML <em>outside</em> it? Exactly. It will behave just as usual. So this step is primitive: select some data from storage, display it. Nothing new.</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fwww.youtube.com%2Fembed%2F0Nl7Zjbvuak%3Ffeature%3Doembed&amp;display_name=YouTube&amp;url=https%3A%2F%2Fwww.youtube.com%2Fwatch%3Fv%3D0Nl7Zjbvuak&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=youtube" width="640" height="480" frameborder="0" scrolling="no"><a href="https://medium.com/media/df17909133a488a0ab72e5c981b50680/href">https://medium.com/media/df17909133a488a0ab72e5c981b50680/href</a></iframe><p><a href="https://56kyy.csb.app/">The Game</a></p><p>Try and play it! Drop a line in comments if liked it. Or, if you want me to publish this project on github.</p><p><strong>Conclusion</strong></p><p>Of course I don’t think of myself as of <em>the great game developer</em>. There is huge amount of cool projects, I often see such posts it Twitter. It’s great that people share knowledge and games they did. It was fun, I hope it was not boring ;) Btw, here are some links to stay in touch: <a href="https://twitter.com/low_dencity">twitter</a>, <a href="https://www.linkedin.com/in/asaltykov">linkedin</a>, <a href="https://github.com/johnSamilin">github</a>.</p><p><strong>Resources</strong></p><ol><li><a href="https://stemkoski.github.io/Three.js/Collision-Detection.html">https://stemkoski.github.io/Three.js/Collision-Detection.html</a> — collision detection based on raycasting</li><li><a href="https://www.youtube.com/watch?v=sx4IIQL0x7c">https://www.youtube.com/watch?v=sx4IIQL0x7c</a> — cool video about spatial grids</li><li><a href="https://www.youtube.com/watch?v=bqtqltqcQhw">https://www.youtube.com/watch?v=bqtqltqcQhw</a> — another video about boids</li><li><a href="https://developer.mozilla.org/en-US/docs/Games/Techniques/3D_collision_detection/Bounding_volume_collision_detection_with_THREE.js">https://developer.mozilla.org/en-US/docs/Games/Techniques/3D_collision_detection/Bounding_volume_collision_detection_with_THREE.js</a> — explanatory article about bounding volume collision detection</li><li><a href="https://mozdevs.github.io/gamedev-js-3d-aabb/physics.html">https://mozdevs.github.io/gamedev-js-3d-aabb/physics.html</a> — demo of physics based (ammo.js) collision detection</li><li><a href="https://medium.com/@bluemagnificent/intro-to-javascript-3d-physics-using-ammo-js-and-three-js-dd48df81f591">https://medium.com/@bluemagnificent/intro-to-javascript-3d-physics-using-ammo-js-and-three-js-dd48df81f591</a> — intro to three.js and ammo.js</li></ol><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=cd7bb0474ce4" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[How do they make browser games nowadays? Part 2]]></title>
            <link>https://medium.com/@alex.saltykov/how-do-they-make-browser-games-nowadays-part-2-8e7dcd50d6c0?source=rss-c403d0cd5604------2</link>
            <guid isPermaLink="false">https://medium.com/p/8e7dcd50d6c0</guid>
            <category><![CDATA[webgl]]></category>
            <category><![CDATA[gamedev]]></category>
            <category><![CDATA[indie]]></category>
            <category><![CDATA[react]]></category>
            <category><![CDATA[threejs]]></category>
            <dc:creator><![CDATA[Alexander Saltykov]]></dc:creator>
            <pubDate>Wed, 10 Nov 2021 07:03:45 GMT</pubDate>
            <atom:updated>2021-11-29T09:36:11.039Z</atom:updated>
            <content:encoded><![CDATA[<p>The <a href="https://medium.com/@alex.saltykov/how-do-they-make-brower-games-nowadays-part-1-3a18b9b04a4b">previous article</a> described basic steps that you need to do to make your own browser game. Read it first, if you haven’t already.</p><p>This time we’ll move to more complex stuff, such as state management, higher order components and gamepad API.</p><p><strong>Step 6. Movement</strong></p><p>How do you actually move a character in the direction you need? A model can’t move itself by it’s own. I’ve already mentioned earlier in step 4 the pros of wrapping a collection of objects in group. Once again: you can transition group of meshes (i.e. the model) with linear speed; in conjunction with proper animation it will look like running or walking, depending on your needs.</p><p>So if you want your NPC to move in some direction, let’s say northward, you’d have to perform 2 simple operations:</p><ol><li>rotate it in the desired direction;</li><li>start transitioning it’s position little by little.</li></ol><p>The most convenient way to do so (imho) is to use composition. So create a file called /hoc/Movable.jsx with the following content:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/768/1*d_BmohZnqoqZjXu6518x2A.png" /><figcaption><a href="https://codesandbox.io/s/exciting-marco-iyy6h?file=/src/hoc/Movable.jsx:0-787">https://codesandbox.io/s/exciting-marco-iyy6h?file=/src/hoc/Movable.jsx:0-787</a></figcaption></figure><p>Here, this component simply takes all of it’s child nodes, then slightly moves them towards <em>heading </em>vector in each frame. <em>Velocity </em>prop is responsible for how far this transition will be. Note <em>onMove</em> callback: we’ll talk about it later.</p><p>Additionally, it uses /helpers/getRotation.js helper which does <a href="https://codesandbox.io/s/exciting-marco-iyy6h?file=/src/helpers/getRotation.js">some quaternion magic</a> to actually rotate the group.</p><p>Ok now, if you wrap something in &lt;Movable /&gt; component it will, well, move. Take a look at this little demo:</p><p><a href="https://iyy6h.csb.app/">The Game</a></p><p><strong>Step 7. Gamepad support</strong></p><p>I didn’t event have gamepad until quarantine; I bought one just to try. And it turned out that it’s pretty comfortable way to play games (by the way, Diablo II: Resurrected with gamepad feels like a charm. Never thought it would be so). Chrome has Gamepad API for a long time already. So why not to try?</p><p>Gamepad API is pretty straightforward; it’s not terribly hard to use, but to make things even easier we’ll use <a href="https://www.npmjs.com/package/gamepads">Gamepads</a> library. By using React’s context we can write all of the callbacks somewhere in the top level and just pass the information about how player moves it’s stick (and when he presses attack button) down to Movable component we created earlier. Check out the files: /GamepadControls.jsx — callbacks for gamepad and fallback for WASD keys (<a href="https://codesandbox.io/s/pensive-rubin-stl9f?file=/src/GamepadControls.jsx">link</a>); /Player.jsx — small wrapper around player model that responds to our actions (<a href="https://codesandbox.io/s/pensive-rubin-stl9f?file=/src/Player.jsx">link</a>).</p><p>Now replace all of the NPCs in index.js with this:</p><pre>&lt;GamepadControls&gt;</pre><pre>  &lt;Player /&gt;</pre><pre>&lt;/GamepadControls&gt;</pre><p>And that’s it!</p><p><a href="https://stl9f.csb.app/">The Game</a></p><p>Next important question to ask is “How do you tie together all those zombies, the player and fireballs? How do enemies know where the player is?”. And the answer is — you say it — state management.</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fwww.youtube.com%2Fembed%2FTRQECpN4QrY%3Ffeature%3Doembed&amp;display_name=YouTube&amp;url=https%3A%2F%2Fwww.youtube.com%2Fwatch%3Fv%3DTRQECpN4QrY&amp;image=https%3A%2F%2Fi.ytimg.com%2Fvi%2FTRQECpN4QrY%2Fhqdefault.jpg&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=youtube" width="854" height="480" frameborder="0" scrolling="no"><a href="https://medium.com/media/d4fa8dc25a6b7602374ebfc273c3109b/href">https://medium.com/media/d4fa8dc25a6b7602374ebfc273c3109b/href</a></iframe><p><strong>Step 8. State management</strong></p><p>Every single front-end project in 2021 must have state management layer. This is unarguable. And our game is no exception. You can of course use your favorite one, whether it’s Redux, MobX, effector or whatever. It makes absolutely no difference. I chose Zustand.</p><p>Think of all the entities in the game as of collections. Add an entity (zombie or fireball) to corresponding collection in global store, render it, then just remove an entity when you need (fireball hits zombie or flies too far; zombie dies of spell). I bet you did this a lot of times but you rendered collection of items in e-commerce shop instead.</p><p>Let’s add some stores in /store folder:</p><ul><li><a href="https://codesandbox.io/s/distracted-bird-56kyy?file=/src/store/player.js">player</a></li><li><a href="https://codesandbox.io/s/distracted-bird-56kyy?file=/src/store/zombies.js">zombies</a></li><li><a href="https://codesandbox.io/s/distracted-bird-56kyy?file=/src/store/fireballs.js">fireballs</a></li></ul><p>I hope all of the methods there are self-explanatory. If not, sorry. Player store holds position (it should be available for everyone); zombies store holds id for each enemy, his position and HP. Finally, fireballs store is used to control amount of fireballs and their directions.</p><p>Ok, now things start to get complicated a little. Don’t worry, it’s not that bad.</p><p>Modify Player component as such:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/768/1*jfRwRD-jqSoJAkmjeUb-fA.png" /></figure><p>Also add this code to Fireball component:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/768/1*ypfRHg4KcLbq6qCarVwm0Q.png" /></figure><p>And take a look at <a href="https://codesandbox.io/s/distracted-bird-56kyy?file=/src/Zombie.jsx:566-734">Zombie</a> component, especially at these lines:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/768/1*v5CZ_BIfYIooixXBrh9fWw.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/660/1*CCR67T4_gCetZp56zb-4bA.png" /><figcaption>Vector operations behind enemy orientation</figcaption></figure><p>Now all enemies will pursuit you wherever you go.</p><p><a href="https://codesandbox.io/s/distracted-bird-56kyy?file=/src/index.js">Complete code for this step is available here</a>, except one thing: since codesandbox has some problems with parsing .fbx, I replaced all the models with colorful cubes.</p><p><a href="https://56kyy.csb.app/">The Game</a></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/800/1*2rjkWhSn-pHLwr3L1N13GA.gif" /></figure><p>In the <a href="https://medium.com/@alex.saltykov/how-do-they-make-browser-games-nowadays-part-3-cd7bb0474ce4">next chapter</a> I’ll review techniques for collision detection and UI.</p><p>Thanks for reading the article! I hope you liked it. Here are some links to stay in touch: <a href="https://twitter.com/low_dencity">twitter</a>, <a href="https://www.linkedin.com/in/asaltykov">linkedin</a>, <a href="https://github.com/johnSamilin">github</a>.</p><p><strong>Note</strong></p><p>There is a chance that your gamepad won’t have the same buttons as mine, if so, please see <a href="https://www.npmjs.com/package/gamepads">Gamepads</a> library docs and the standard mapping.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*QERPvlI0BpEBAM6yje7arg.png" /><figcaption>Standard gamepad mapping</figcaption></figure><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=8e7dcd50d6c0" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>