platjs
Version:
Simple engine for platformer physics and rendering
80 lines (74 loc) • 15.2 kB
HTML
<html class="default" lang="en"><head><meta charSet="utf-8"/><meta http-equiv="x-ua-compatible" content="IE=edge"/><title>platjs - v1.2.5</title><meta name="description" content="Documentation for platjs - v1.2.5"/><meta name="viewport" content="width=device-width, initial-scale=1"/><link rel="stylesheet" href="assets/style.css"/><link rel="stylesheet" href="assets/highlight.css"/><script async src="assets/search.js" id="search-script"></script></head><body><script>document.documentElement.dataset.theme = localStorage.getItem("tsd-theme") || "os"</script><header class="tsd-page-toolbar">
<div class="tsd-toolbar-contents container">
<div class="table-cell" id="tsd-search" data-base=".">
<div class="field"><label for="tsd-search-field" class="tsd-widget tsd-toolbar-icon search no-caption"><svg width="16" height="16" viewBox="0 0 16 16" fill="none"><path d="M15.7824 13.833L12.6666 10.7177C12.5259 10.5771 12.3353 10.499 12.1353 10.499H11.6259C12.4884 9.39596 13.001 8.00859 13.001 6.49937C13.001 2.90909 10.0914 0 6.50048 0C2.90959 0 0 2.90909 0 6.49937C0 10.0896 2.90959 12.9987 6.50048 12.9987C8.00996 12.9987 9.39756 12.4863 10.5008 11.6239V12.1332C10.5008 12.3332 10.5789 12.5238 10.7195 12.6644L13.8354 15.7797C14.1292 16.0734 14.6042 16.0734 14.8948 15.7797L15.7793 14.8954C16.0731 14.6017 16.0731 14.1267 15.7824 13.833ZM6.50048 10.499C4.29094 10.499 2.50018 8.71165 2.50018 6.49937C2.50018 4.29021 4.28781 2.49976 6.50048 2.49976C8.71001 2.49976 10.5008 4.28708 10.5008 6.49937C10.5008 8.70852 8.71314 10.499 6.50048 10.499Z" fill="var(--color-text)"></path></svg></label><input type="text" id="tsd-search-field" aria-label="Search"/></div>
<div class="field">
<div id="tsd-toolbar-links"></div></div>
<ul class="results">
<li class="state loading">Preparing search index...</li>
<li class="state failure">The search index is not available</li></ul><a href="index.html" class="title">platjs - v1.2.5</a></div>
<div class="table-cell" id="tsd-widgets"><a href="#" class="tsd-widget tsd-toolbar-icon menu no-caption" data-toggle="menu" aria-label="Menu"><svg width="16" height="16" viewBox="0 0 16 16" fill="none"><rect x="1" y="3" width="14" height="2" fill="var(--color-text)"></rect><rect x="1" y="7" width="14" height="2" fill="var(--color-text)"></rect><rect x="1" y="11" width="14" height="2" fill="var(--color-text)"></rect></svg></a></div></div></header>
<div class="container container-main">
<div class="col-8 col-content">
<div class="tsd-page-title">
<h2>platjs - v1.2.5</h2></div>
<div class="tsd-panel tsd-typography">
<a href="#platjs" id="platjs" style="color: inherit; text-decoration: none;">
<h1>platjs</h1>
</a>
<a href="#a-simple-engine-for-platformer-games-and-rendering" id="a-simple-engine-for-platformer-games-and-rendering" style="color: inherit; text-decoration: none;">
<h3>A simple engine for platformer games and rendering</h3>
</a>
<a href="#demo" id="demo" style="color: inherit; text-decoration: none;">
<h3>Demo</h3>
</a>
<p><a href="https://platjs.vercel.app">platjs demo</a></p>
<hr>
<a href="#installation" id="installation" style="color: inherit; text-decoration: none;">
<h1>Installation:</h1>
</a>
<a href="#npm-install" id="npm-install" style="color: inherit; text-decoration: none;">
<h3>npm install</h3>
</a>
<pre><code><span class="hl-0">npm</span><span class="hl-1"> </span><span class="hl-0">install</span><span class="hl-1"> </span><span class="hl-0">platjs</span>
</code></pre>
<pre><code class="language-js"><span class="hl-2">import</span><span class="hl-1"> </span><span class="hl-3">*</span><span class="hl-1"> </span><span class="hl-2">as</span><span class="hl-1"> </span><span class="hl-0">plat</span><span class="hl-1"> </span><span class="hl-2">from</span><span class="hl-1"> </span><span class="hl-4">"platjs"</span><span class="hl-1">;</span>
</code></pre>
<a href="#script-tag" id="script-tag" style="color: inherit; text-decoration: none;">
<h3>script tag</h3>
</a>
<pre><code class="language-html"><span class="hl-5"><</span><span class="hl-6">script</span><span class="hl-7"> </span><span class="hl-8">src</span><span class="hl-7">=</span><span class="hl-9">"https://cdn.jsdelivr.net/npm/platjs"</span><span class="hl-5">></</span><span class="hl-6">script</span><span class="hl-5">></span>
</code></pre>
<a href="#use" id="use" style="color: inherit; text-decoration: none;">
<h1>Use:</h1>
</a>
<pre><code class="language-js"><span class="hl-3">const</span><span class="hl-1"> { </span><span class="hl-10">Renderer</span><span class="hl-1">, </span><span class="hl-10">ControlledBody</span><span class="hl-1">, </span><span class="hl-10">StaticBody</span><span class="hl-1"> } = </span><span class="hl-0">plat</span><span class="hl-1">;</span><br/><br/><span class="hl-11">// Create a renderer</span><br/><span class="hl-11">// This handles physics and rendering for you.</span><br/><span class="hl-3">const</span><span class="hl-1"> </span><span class="hl-10">renderer</span><span class="hl-1"> = </span><span class="hl-3">new</span><span class="hl-1"> </span><span class="hl-12">Renderer</span><span class="hl-1">()</span><br/><span class="hl-1"> .</span><span class="hl-12">mount</span><span class="hl-1">(</span><span class="hl-0">document</span><span class="hl-1">.</span><span class="hl-0">body</span><span class="hl-1">)</span><br/><span class="hl-1"> .</span><span class="hl-12">enableFixedPosition</span><span class="hl-1">()</span><br/><span class="hl-1"> .</span><span class="hl-12">enablePhysics</span><span class="hl-1">({});</span><br/><br/><span class="hl-11">// Create a player</span><br/><span class="hl-11">// Giving it a "color" property will make it render as that color.</span><br/><span class="hl-3">const</span><span class="hl-1"> </span><span class="hl-10">player</span><span class="hl-1"> = </span><span class="hl-3">new</span><span class="hl-1"> </span><span class="hl-12">ControlledBody</span><span class="hl-1">({</span><br/><span class="hl-1"> </span><span class="hl-0">x:</span><span class="hl-1"> </span><span class="hl-13">30</span><span class="hl-1">,</span><br/><span class="hl-1"> </span><span class="hl-0">y:</span><span class="hl-1"> </span><span class="hl-13">30</span><span class="hl-1">,</span><br/><span class="hl-1"> </span><span class="hl-0">width:</span><span class="hl-1"> </span><span class="hl-13">30</span><span class="hl-1">,</span><br/><span class="hl-1"> </span><span class="hl-0">height:</span><span class="hl-1"> </span><span class="hl-13">30</span><span class="hl-1">,</span><br/><span class="hl-1"> </span><span class="hl-0">layer:</span><span class="hl-1"> </span><span class="hl-13">1</span><span class="hl-1">,</span><br/><span class="hl-1"> </span><span class="hl-0">color:</span><span class="hl-1"> </span><span class="hl-4">"blue"</span><span class="hl-1">,</span><br/><span class="hl-1">});</span><br/><br/><span class="hl-11">// Add the player to the renderer's list of objects to draw / update</span><br/><span class="hl-0">renderer</span><span class="hl-1">.</span><span class="hl-12">add</span><span class="hl-1">(</span><span class="hl-0">player</span><span class="hl-1">);</span><br/><br/><span class="hl-11">// enable keyboard controls</span><br/><span class="hl-0">player</span><span class="hl-1">.</span><span class="hl-12">bindKeyboardControls</span><span class="hl-1">({});</span><br/><br/><span class="hl-11">// lock the camera to the player (player stays at center of the screen)</span><br/><span class="hl-0">renderer</span><span class="hl-1">.</span><span class="hl-0">camera</span><span class="hl-1">.</span><span class="hl-12">lock</span><span class="hl-1">(</span><span class="hl-0">player</span><span class="hl-1">);</span><br/><br/><span class="hl-11">// create a body for the player to land / jump on</span><br/><span class="hl-0">renderer</span><span class="hl-1">.</span><span class="hl-12">add</span><span class="hl-1">(</span><br/><span class="hl-1"> </span><span class="hl-3">new</span><span class="hl-1"> </span><span class="hl-12">StaticBody</span><span class="hl-1">({ </span><span class="hl-0">x:</span><span class="hl-1"> </span><span class="hl-13">0</span><span class="hl-1">, </span><span class="hl-0">y:</span><span class="hl-1"> </span><span class="hl-13">500</span><span class="hl-1">, </span><span class="hl-0">width:</span><span class="hl-1"> </span><span class="hl-13">300</span><span class="hl-1">, </span><span class="hl-0">height:</span><span class="hl-1"> </span><span class="hl-13">100</span><span class="hl-1">, </span><span class="hl-0">color:</span><span class="hl-1"> </span><span class="hl-4">"black"</span><span class="hl-1"> })</span><br/><span class="hl-1">);</span><br/><br/><span class="hl-11">// rendering loop</span><br/><span class="hl-3">const</span><span class="hl-1"> </span><span class="hl-12">animationLoop</span><span class="hl-1"> = () </span><span class="hl-3">=></span><span class="hl-1"> {</span><br/><span class="hl-1"> </span><span class="hl-11">// update physics</span><br/><span class="hl-1"> </span><span class="hl-0">renderer</span><span class="hl-1">.</span><span class="hl-12">update</span><span class="hl-1">();</span><br/><br/><span class="hl-1"> </span><span class="hl-11">// respawn player if needed</span><br/><span class="hl-1"> </span><span class="hl-2">if</span><span class="hl-1"> (</span><span class="hl-0">player</span><span class="hl-1">.</span><span class="hl-0">y</span><span class="hl-1"> - </span><span class="hl-0">player</span><span class="hl-1">.</span><span class="hl-0">height</span><span class="hl-1"> / </span><span class="hl-13">2</span><span class="hl-1"> > </span><span class="hl-0">renderer</span><span class="hl-1">.</span><span class="hl-0">height</span><span class="hl-1">) {</span><br/><span class="hl-1"> </span><span class="hl-0">player</span><span class="hl-1">.</span><span class="hl-0">v</span><span class="hl-1">.</span><span class="hl-0">y</span><span class="hl-1"> = </span><span class="hl-13">0</span><span class="hl-1">;</span><br/><span class="hl-1"> </span><span class="hl-0">player</span><span class="hl-1">.</span><span class="hl-0">v</span><span class="hl-1">.</span><span class="hl-0">x</span><span class="hl-1"> = </span><span class="hl-13">0</span><span class="hl-1">;</span><br/><span class="hl-1"> </span><span class="hl-0">player</span><span class="hl-1">.</span><span class="hl-0">x</span><span class="hl-1"> = </span><span class="hl-13">30</span><span class="hl-1">;</span><br/><span class="hl-1"> </span><span class="hl-0">player</span><span class="hl-1">.</span><span class="hl-0">y</span><span class="hl-1"> = </span><span class="hl-13">30</span><span class="hl-1">;</span><br/><span class="hl-1"> }</span><br/><br/><span class="hl-1"> </span><span class="hl-11">// draw everything</span><br/><span class="hl-1"> </span><span class="hl-0">renderer</span><span class="hl-1">.</span><span class="hl-12">render</span><span class="hl-1">();</span><br/><br/><span class="hl-1"> </span><span class="hl-12">requestAnimationFrame</span><span class="hl-1">(</span><span class="hl-0">animationLoop</span><span class="hl-1">);</span><br/><span class="hl-1">};</span><br/><br/><span class="hl-12">requestAnimationFrame</span><span class="hl-1">(</span><span class="hl-0">animationLoop</span><span class="hl-1">);</span>
</code></pre>
</div></div>
<div class="col-4 col-menu menu-sticky-wrap menu-highlight">
<div class="tsd-navigation settings">
<details class="tsd-index-accordion"><summary class="tsd-accordion-summary">
<h3><svg width="20" height="20" viewBox="0 0 24 24" fill="none"><path d="M4.93896 8.531L12 15.591L19.061 8.531L16.939 6.409L12 11.349L7.06098 6.409L4.93896 8.531Z" fill="var(--color-text)"></path></svg> Settings</h3></summary>
<div class="tsd-accordion-details">
<div class="tsd-filter-visibility">
<h4 class="uppercase">Member Visibility</h4><form>
<ul id="tsd-filter-options">
<li class="tsd-filter-item"><label class="tsd-filter-input"><input type="checkbox" id="tsd-filter-protected" name="protected"/><svg width="32" height="32" viewBox="0 0 32 32" aria-hidden="true"><rect class="tsd-checkbox-background" width="30" height="30" x="1" y="1" rx="6" fill="none"></rect><path class="tsd-checkbox-checkmark" d="M8.35422 16.8214L13.2143 21.75L24.6458 10.25" stroke="none" stroke-width="3.5" stroke-linejoin="round" fill="none"></path></svg><span>Protected</span></label></li>
<li class="tsd-filter-item"><label class="tsd-filter-input"><input type="checkbox" id="tsd-filter-private" name="private"/><svg width="32" height="32" viewBox="0 0 32 32" aria-hidden="true"><rect class="tsd-checkbox-background" width="30" height="30" x="1" y="1" rx="6" fill="none"></rect><path class="tsd-checkbox-checkmark" d="M8.35422 16.8214L13.2143 21.75L24.6458 10.25" stroke="none" stroke-width="3.5" stroke-linejoin="round" fill="none"></path></svg><span>Private</span></label></li>
<li class="tsd-filter-item"><label class="tsd-filter-input"><input type="checkbox" id="tsd-filter-inherited" name="inherited" checked/><svg width="32" height="32" viewBox="0 0 32 32" aria-hidden="true"><rect class="tsd-checkbox-background" width="30" height="30" x="1" y="1" rx="6" fill="none"></rect><path class="tsd-checkbox-checkmark" d="M8.35422 16.8214L13.2143 21.75L24.6458 10.25" stroke="none" stroke-width="3.5" stroke-linejoin="round" fill="none"></path></svg><span>Inherited</span></label></li>
<li class="tsd-filter-item"><label class="tsd-filter-input"><input type="checkbox" id="tsd-filter-external" name="external"/><svg width="32" height="32" viewBox="0 0 32 32" aria-hidden="true"><rect class="tsd-checkbox-background" width="30" height="30" x="1" y="1" rx="6" fill="none"></rect><path class="tsd-checkbox-checkmark" d="M8.35422 16.8214L13.2143 21.75L24.6458 10.25" stroke="none" stroke-width="3.5" stroke-linejoin="round" fill="none"></path></svg><span>External</span></label></li></ul></form></div>
<div class="tsd-theme-toggle">
<h4 class="uppercase">Theme</h4><select id="theme"><option value="os">OS</option><option value="light">Light</option><option value="dark">Dark</option></select></div></div></details></div>
<nav class="tsd-navigation primary">
<details class="tsd-index-accordion" open><summary class="tsd-accordion-summary">
<h3><svg width="20" height="20" viewBox="0 0 24 24" fill="none"><path d="M4.93896 8.531L12 15.591L19.061 8.531L16.939 6.409L12 11.349L7.06098 6.409L4.93896 8.531Z" fill="var(--color-text)"></path></svg> Modules</h3></summary>
<div class="tsd-accordion-details">
<ul>
<li class="current selected"><a href="modules.html">platjs -<wbr/> v1.2.5</a>
<ul>
<li class="tsd-kind-module"><a href="modules/camera.html">camera</a></li>
<li class="tsd-kind-module"><a href="modules/index.html">index</a></li>
<li class="tsd-kind-module"><a href="modules/object.html">object</a></li></ul></li></ul></div></details></nav></div></div>
<div class="container tsd-generator">
<p>Generated using <a href="https://typedoc.org/" target="_blank">TypeDoc</a></p></div>
<div class="overlay"></div><script src="assets/main.js"></script></body></html>