I have wrote a few posts on all of the basics when it comes to vuejs, so now I find myself starting to make some actual full vuejs examples as that just seems like the net step when it comes to learning a language, or in this case a framework. Today I thought I would start another vuejs example that is a basic simple little game of sorts that has to do with creating web assets.
The general idea is to create a game where the object is to start making websites, and then the websites generate money over time. So just another kind of idle game as it where which seems to be a kind of game that I like to make, but have never really ran with just yet. In any case this example has proved to be another good exercise of using vue components as a way to keep things modular.
1 - Libraries for the Web Asset vuejs example
First off I have a library folder for this vuejs example Where I have parked a few vanilla javaScript modules that I will be using in my components, and the main vue instance. I often start a major project with a general utility library that might end up containing methods that I will use in other vanilla javaScript libraries. In addition I have a web asset library that I am using as a separate file for creating and updating a web asset object for the game.
1.1 - A utils library for the vue example
Here I have my general utility library, so far for this vue example at least I just have a format money helper and a mathematical modulo helper. If I do keep working on this vue example I am sure this library will grow but for now that is all there is to cover with this one.
// These options are needed to round to whole numbers if that's what you want.
minimumFractionDigits: 0, // (this suffices for whole numbers, but will print 2500.10 as $2,500.1)
maximumFractionDigits: 0// (causes 2500.99 to be printed as $2,501)
});
return formatter.format(number); /* $2,500.00 */
};
// mathematical modulo
utils.mod = function(x, m) {
return (x % m + m) % m;
};
1.2 - Web Assets library
I then also have my main web assets library that I will be using to create web asset objects that will be used in the main vue data object of the main vuejs instance that I will be getting to later in this post. This module will return a main function that I can use to create a web asset object when it comes to the working out the main vue instance where I will be creating such objects. In addition I have worked out a number of additional public methods for updating a web asset file in a loop, and also what to do when a write event happens.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
var WebAsset = (function(){
var setAssetWorth = function(asset){
var d = Math.log(1 + (asset.avgWordsPerPost / 2400)) / Math.log(2);
This is a module that I will be expanding on when it comes to certain parts of the game logic, having at least one or two files like this helps to make my vue components less complex by pulling it out of components and into a vanilla javaScript files such as this. Also I think that it is a good idea to make much of the game logic in a vanilla javaScript form such as this so it is easier for me to take this code to another project in which I might not use vuejs.
2 - The components folder
I then have a components folder with a few vue components for various features of the user interface of the game. Some of these just display info about the main game state object. Others can be used as a way to create new sites, buy sites, and sell as a well as improve sites that the player own all ready.
2.1 - The display component
I have a simple display component that is used to just display how much money the player has. In time I am sure that I will expand this component to display much more about the state of the game as a general info display type thing.
1
2
3
4
5
6
7
8
// just display some basic info
Vue.component('disp', {
props: ['state'],
template: '<div class="ui">' +
'<h3>Web Assets Game: </h3>' +
'<p>Money: {{ format_money(state.money) }}</p>'+
'</div>'
});
2.2 - webasset-ui-create
I wanted to start a simple components that can be used to create a new website asset for free in the event that the player has no money to buy one with.
I have the libraries that I want to use, and I have a number of components, now I just need a main vue instance. In the main.js file I have just a single vue mixin that is just a reference to my utils format money method. I did this just so I can use the format money method in the templates.
This game is starting to come together all ready, but I am not sure if I will keep working on it or not. There are so many other vuejs examples, and additional projects outside of vuejs all together that I also want to put more time into. In any case this vuejs example was fun for a little while, and working on it helped me to get a better idea of how to break a project down into many little parts by making use of vue components.