It wasn't as easy as I thought it would be; I faced some challenges before finally achieving a basic functional game. My approach may not have been the most efficient, but it did the job for me (I was using Google Chrome on a Mac OS). If there are better ways to do this that someone could suggest (as I'm also new to this library), I am open to learning. Since I couldn't find a straightforward beginner tutorial online, I decided to share my experience in the hopes of helping others.
To start, create an HTML file and include the necessary libraries inside the <head>
tag:
<script src="canvasengine-X.Y.Z.all.min.js"></script>
<script src="rpgjs-X.Y.Z.min.js"></script>
Then, add the canvas within the <body>
tag:
<canvas id="canvas_id" width="640" height="480"></canvas>
There is a 'sample' folder within the GitHub project (refer to https://github.com/RSamaium/RPG-JS), containing a playable game which can be launched in the browser through the file quick.html
. The idea is that if the sample game functions, then your game can too. Therefore, I started by mimicking the graphics and data from the sample game.
Firstly, you need to create the following folders and subfolders at the root of your project:
core
core\scene
Data
Data\Maps
Graphics
For a basic functional game, you require 1) a map for your character to navigate, and 2) a character for control. Hence, you must have graphics for tiles to construct your map and a model for your character's movement. Create the subfolders below:
Graphics\Characters
Graphics\Tilesets
Graphics\Windowskins
Next, copy the following files from the respective sample subfolder into the newly created folders:
Graphics\Characters\event1.png
Graphics\Tilesets\tileset.png
Graphics\Windowskins\window.png #this file might not be used in all scripts, but is essential due to dependencies; removing this necessity requires editing multiple files - a task best avoided initially.
The 'core/scene' folder contains JavaScript files for loading scenes (e.g., map navigation, game over screen). I had to transfer all JS files from the 'sample' project's corresponding folder to the 'core/scene' folder of my game. This folder is located at the root of the GitHub project, not within the 'sample' directory. To get my game running properly, I included all 7 JS files (you can eliminate unwanted scenes without code modifications, but for now, let's copy them):
Scene_Gameover.js
Scene_Generated.js
Scene_Load.js
Scene_Map.js
Scene_Menu.js
Scene_Title.js
Scene_Window.js
Finally, I inserted the following code within a <script>
tag in the HTML. It closely resembles what was mentioned in the documentation. These snippets will generate an "actor" (character), a "map", and the tiles intended for the map. Further details on maps or tiles can be found in the documentation linked in your question.
RPGJS.Materials = {
"characters": {
"1": "event1.png"
},
"tilesets": {
"1": "tileset.png"
}
};
RPGJS.Database = {
"actors": {
"1": {
"graphic": "1"
}
},
"tilesets": {
"1": {
"graphic": "1"
}
},
"map_infos": {
"1": {
"tileset_id": "1"
}
}
};
RPGJS.defines({
canvas: "canvas_id",
autoload: false
}).ready(function() {
RPGJS.Player.init({
actor: 1,
start: {x: 10, y: 10, id: 1} // Here, map id doesn't exist
});
RPGJS.Scene.map();
Lastly, create a JSON file defining every tile in your map. I simply copied the MAP-1.json file from the 'sample' folder into the Data\Maps
folder.
This should enable you to move your character around an empty map! Open the HTML file in your browser and give it a try!
Screenshot of the game running in a browser
Of course, developing a full-fledged game will involve significant changes (like creating a database.json file and a materials.json file to store much of the script present in the <script>
tag); however, with these basics, you should be able to make progress!