<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: matannahmani</title>
    <description>The latest articles on DEV Community by matannahmani (@matannahmani).</description>
    <link>https://dev.to/matannahmani</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F347729%2Fe1547421-8996-400d-9979-0f914548868b.jpeg</url>
      <title>DEV Community: matannahmani</title>
      <link>https://dev.to/matannahmani</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/matannahmani"/>
    <language>en</language>
    <item>
      <title>Phaser 3 - Saving and Loading dynamic maps (tilemaps)</title>
      <dc:creator>matannahmani</dc:creator>
      <pubDate>Tue, 10 Mar 2020 12:01:05 +0000</pubDate>
      <link>https://dev.to/matannahmani/phaser-3-saving-and-loading-dynamic-maps-tilemaps-1i6j</link>
      <guid>https://dev.to/matannahmani/phaser-3-saving-and-loading-dynamic-maps-tilemaps-1i6j</guid>
      <description>&lt;p&gt;&lt;strong&gt;About Me&lt;/strong&gt;: I've been programming for just over 3 years and I'm currently looking for a job in web development/game design.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;About the topic&lt;/strong&gt;: I have seen many phaser 3 developers attempting to create dynamic maps, which could be saved and exported but they couldn't find the solution, so I decided to attempt and solve this problem.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;About the Tools used&lt;/strong&gt;: SQL (Postgres), ruby on rails for back-end API, Phaser3 and Reg-ex for basic security.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;About the Level&lt;/strong&gt;: This tutorial is meant for a complete beginner so everybody could understand! but remember in this tutorial I don't show a way to secure user route or use and authorization methods.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Example of usage&lt;/strong&gt; : &lt;a href="https://lastresort.pw"&gt;LastResort.pw&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;iframe src="https://player.vimeo.com/video/396294350" width="710" height="399"&gt;
&lt;/iframe&gt;
&lt;/p&gt;


&lt;center&gt; End Result map saved to the current user &lt;center&gt;&lt;/center&gt;

&lt;p&gt;Our first step is creating a rails app and setting up a Relational database:&lt;/p&gt;

&lt;p&gt;I am using ruby on rails for this tutorial but this method could work on any other web framework.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Rails part starts here, setting new rails app with webpacker and Postgres DB&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;to create a new rails app with webpacker and Postgres DB&lt;/p&gt;

&lt;p&gt;&lt;code&gt;rails new $yourappname --webpack --database=postgresql&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--KQk93vvt--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://i.imgur.com/ISU8NHE.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--KQk93vvt--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://i.imgur.com/ISU8NHE.png" alt="Database photo"&gt;&lt;/a&gt;&lt;/p&gt;




&lt;center&gt;In this database each user has base_commands column which will be an array of commands, we will touch this later.&lt;br&gt;&lt;br&gt;
The structure table contains two columns, placed and amount both being integer, which will represent the number of structures owned and the number of structures placed on the tilemap.&lt;br&gt;&lt;br&gt;
User_structures is a join table containing user structures.

&lt;p&gt;We will start by generating all of our models :&lt;br&gt;
structure, user and user_structures&lt;/p&gt;

&lt;p&gt;&lt;code&gt;rails g model structure amount:integer placed:integer&lt;/code&gt;&lt;br&gt;
&lt;code&gt;rails g model user&lt;/code&gt;&lt;br&gt;
&lt;code&gt;rails g model user_structure structure:references user:references&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;inside User migration file&lt;br&gt;
&lt;code&gt;t.string :base_commands, array:true, default: []&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Then run the following commands&lt;br&gt;
&lt;code&gt;rails db:create&lt;/code&gt;&lt;br&gt;
&lt;code&gt;rails db:migrate&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Now after we finished setting up our database will start working on models&lt;/p&gt;

&lt;p&gt;In the User and Structure model, we add &lt;code&gt;has_many :user_structure&lt;/code&gt;&lt;br&gt;
In UserStructure model we add the following lines:&lt;br&gt;
&lt;code&gt;belongs_to :structure&lt;/code&gt;&lt;br&gt;
&lt;code&gt;belongs_to :user&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Our next step now is to create Get and Patch routes so we could access our base_commands column.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;rails g controller game&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;then in routes.rb (inside config folder) we add the following routes&lt;br&gt;
  &lt;code&gt;get 'base/:id', to: "game#showbase"&lt;/code&gt;&lt;br&gt;
  &lt;code&gt;patch 'base/:id', to: "game#updatebase"&lt;/code&gt;&lt;br&gt;
in our game controller we add basic checking , and responding with JSON containing user base_commands.&lt;br&gt;
see code in &lt;a href="https://github.com/matannahmani/phaser3tutorial"&gt;github&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;after creating all of our API endpoints we can finally start working inside our Phaser javascript file!&lt;/p&gt;


&lt;/center&gt;

&lt;p&gt;we will start by using the basic Phaser3 template:&lt;br&gt;
&lt;code&gt;const config = {&lt;br&gt;
  type: Phaser.WEBGL,&lt;br&gt;
  width: 375,&lt;br&gt;
  height: 812,&lt;br&gt;
  scale: {&lt;br&gt;
    mode: Phaser.Scale.RESIZE,&lt;br&gt;
  },&lt;br&gt;
  // parent  : 'phaser-app',&lt;br&gt;
  scene: {&lt;br&gt;
    preload: preload,&lt;br&gt;
    create: create,&lt;br&gt;
    update: update,&lt;br&gt;
  }&lt;br&gt;
}&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;after finishing our basic template we need to load our tileset to the phaser3 canvas.&lt;br&gt;
we will create preload function:&lt;br&gt;
&lt;code&gt;function preload() {&lt;br&gt;
      this.load.image('tiles', 'url/tileimg.png');&lt;br&gt;
      this.load.tilemapTiledJSON('tilemap', 'url/tilemapjson.json');&lt;br&gt;
&lt;/code&gt;&lt;br&gt;
tiles is the key for the image and tilemap is the key for the json&lt;/p&gt;

&lt;p&gt;now will move to the create and update function:&lt;br&gt;
&lt;code&gt;function create() {&lt;br&gt;
      const map = this.make.tilemap({ key: 'tilemap' });&lt;br&gt;
      const tiles = map.addTilesetImage('16x16s', 'tiles');&lt;br&gt;
      const layer = map.createStaticLayer(1, tiles);&lt;br&gt;
            layer2 = map.createDynamicLayer(2, tiles);&lt;br&gt;
&lt;/code&gt;&lt;br&gt;
&lt;code&gt;function update() {}&lt;/code&gt;&lt;br&gt;
Nice ! now will should have our map running on the canvas&lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--mXNclH4C--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://i.imgur.com/yj936sf.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--mXNclH4C--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://i.imgur.com/yj936sf.png" alt="Base Photo"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Our next step is to create our Fetch calls, We will create two functions, first the loadBase that will give us the current user draw commands,&lt;br&gt;
Second, the updateBase which will add our draw command to the user database.&lt;br&gt;
you could also find improved version of functions in &lt;a href="https://github.com/matannahmani/phaser3tutorial/blob/master/app/javascript/plugins/game.js"&gt;github&lt;/a&gt;&lt;a&gt;&lt;/a&gt;&lt;br&gt;
&lt;code&gt;&lt;br&gt;
function loadBase() {&lt;br&gt;
  fetch('../base/1') // will get first user base&lt;br&gt;
    .then((response) =&amp;gt; {&lt;br&gt;
      return response.json();&lt;br&gt;
    })&lt;br&gt;
    .then((commands) =&amp;gt; {&lt;br&gt;
      commands.msg.forEach((command) =&amp;gt;{ // iterate over commands&lt;br&gt;
        eval(command); }) // execute each draw command&lt;br&gt;
    });&lt;br&gt;
}&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;function updateBase(data) {&lt;br&gt;
    // Default options are marked with *&lt;br&gt;
    fetch('../base', {&lt;br&gt;
      method: 'PATCH', // *GET, POST, PUT, DELETE, etc.&lt;br&gt;
      mode: 'cors', // no-cors, *cors, same-origin&lt;br&gt;
      cache: 'no-cache', // *default, no-cache, reload, force-cache, only-if-cached&lt;br&gt;
      credentials: 'same-origin', // include, *same-origin, omit&lt;br&gt;
      headers: {&lt;br&gt;
        'Content-Type': 'application/json'&lt;br&gt;
        // 'Content-Type': 'application/x-www-form-urlencoded',&lt;br&gt;
      },&lt;br&gt;
      redirect: 'follow', // manual, *follow, error&lt;br&gt;
      referrerPolicy: 'no-referrer', // no-referrer, *client&lt;br&gt;
      body: JSON.stringify(data) // body data type must match "Content-Type" header&lt;br&gt;
    }).then((response) =&amp;gt; {&lt;br&gt;
    return response.json().then((data) =&amp;gt; {&lt;br&gt;
      if(data['response'] == '500')&lt;br&gt;
        location.reload(); // reload page if failed placing&lt;br&gt;
    }) // parses JSON response into native JavaScript objects&lt;br&gt;
  });&lt;br&gt;
}&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;You could see full example &lt;a href="https://lastresort.pw"&gt;game&lt;/a&gt;&lt;br&gt;
Or here at Lastresort.pw&lt;/p&gt;


&lt;/center&gt;

</description>
      <category>javascript</category>
      <category>beginners</category>
      <category>postgres</category>
    </item>
  </channel>
</rss>
