Item probability javaScript example
This javaScript example post will be on a module that has to do with setting what the probability should be for various classes of items for a game of one kind or another, mostly RPG style games such as my turn based RPG game prototype that is another one of my javaScript examples. The source code of this example started out as a little source code project for my post on the Math.random method in native javaScript as there are a lot of little details about the use of the method beyond just the method itself and a few simple expressions using the Math.random method.
When it comes to such game projects they often contain item systems, and when it comes to such item systems one aspect of such systems is to have a class of item. That is that certain items are of low quality and as such they belong to a class often called common or something to that effect, and then there are items that are of the highest quality in the game and as such they belong to a class such as Epic. So then there is creating a system that can be used to set what the probability of a certain class of item occurring.
This item class module will then not be a whole item system then, let alone a full game prototype of any kind. However it is at least one little component of an item system that has to do with the probability of a certain class of item dropping. There is also the idea of having another set of probabilities for each item of a class also, but that might be a topic for another post that has to do with a more comprehensive over all system for dealing with items in a game.
What to know first
This is a javaScript project example that involves a single javaScript module and a few simple demo projects that make use of that module. This is then not any kind of getting started type post on javaScript in general and I assume that you have at least some background with client side javaScript. If not you might want to check out my main getting started with javaScript post first if you are still very new to javaScript. Also in some of the demos I am making use of canvas elements as a way to render graphics with jaavScript code. Again this is something that you should know a thing or two about before hand as getting into all the various little details about canvas elements is outside the scope of this post.
The source code examples in this post are on Guthub
The source code example for this post can be fount in my test vjs repository on Github. This test vjs repository is where I also have all the source code examples for my many other blog posts on various native javaScript examples, and features in which I am just working with the javaScript language itself.
1 - The Item class module
Here is the source code of the item class module as it currently stands as a javaScript module that follows an IIFE pattern. There just needs to be two public methods for what I had in mind for this module one of which is used to create a main object that will be used for the other public methods of this module, and the other is a method that is used to get a random item class object from a pool of such objects. When it comes to using this module in an over all larger project I might want to add a few more, but the general idea is to just have a collection of objects for each class of object, and each item class object will have a description property that is the name of that item class, as well as a range property that is used to store point values that in turn can be used to set the probability of that class of item happening.
|
|
2 - Demo of the item class system
To make sure that this item class system is working the way that I would want it to I will want to work out at least one if not more demos of the module. For this demo I have made just a very simple canvas project that creates a collection of bars that gives a graphical idea of how often certain item classes will happen over a given amount of item drops when I set the range values for each class in various different kinds of ways.
For example this example I just came up with some fixed static values for the various options that I give to the create method of the item probability module. For example I am setting the levelPer value to 1 with a number literal, but in an actual use case example of this in a game this levelPer value will be set by a players charterer level, or an upgrade option of some kind. I am also setting static values for the range and levelPer values for each item class also. In an actual project these values also might end up being static, or they might be generated by some component of the greater system that uses this. However all of that is a matter for another demo, or maybe a while other post for that matter. For now I will just be dealing with static values for these options and just play around with those to get a sense of what kinds of items to expect for various settings.
So then once I create the item classes object that is created and returned by calling the itemClass create method I will then want to create a bar object for each class. Each bar object will then have a description key, color, and a count variable that will be stepped in a loop where each index value will be a single call of the get random class method of this item probability module.
|
|
With this example up and running so far it would seem that this item probability module is working as expected. It might however be called for to work out at least maybe one or two additional demos at some point though as a way to just test out some more things that would be need to work on top of this. However much of that will end up having to do with the nature of the game that would use this, so maybe what really needs to happen is to make one or more simple game projects actually on top of this rather than just a simple demo.
3 - Simple mine game example
So then for this demo of the module I worked out a very simple game where I just click a mine button and numbers for various types of ores will go up. This is the the first step forward to some kind of actual use case example of this module by making some additional components outside of the module. Some of the code that to do with additional logic that will compose an over all item system.
For this example I just have a collection of arrays for each item class, where each array contains a name of a resource and an additional value that has to do with setting the probability of that resource being the resource that is obtained on each manual click of a mine.
|
|
4 - Conclusion
This javaScript example worked out pretty well for me thus far, and I can not say that there is much more that I would what to add to it for what it is. When it comes to making additional changes and expanding on this example I think that will need to happen in terms of additional modules that make use of this item class module. With that said when it comes to relevant additional reading with this module you might want to check out my post on my turn based RPG javaScript example as that is a full game project that I aim to make use of this module in when it comes to a later revision number of the example.