Functionality using fun
Posted: 13 Sep 2017, 12:26
Hi,
in this topic I'll try to give a step-by-step guide into fun functionality. Dependent on your current knowledge it might need some time getting used to it. However, fun is a real powerful tool to create buildings that react to their environment or to user interaction. By now (version 343) the system is for from completion. Nevertheless, functionality as it's described here should still work with future updates as new functionality will be added primarily.
1. Getting started
To keep it simple I'll just focus on the fun part of a sample plugin. For this reason I'll use frames which are built right into the game so you don't have to bother with them. However, you can make them as complex as usual for your projects. Also building size isn't limited by fun, but a size of width:1, height:1 makes it much easier.
Let this be our starting point of a simple plugin:
You don't have to worry about the frames definition, it just means that we load two frames of this image:
So, now let's add some fun to it. To do so, we use the fun attribute:
Try to guess what this code does.
Solution: If the building was placed with frame 0, it will eventually switch to frame 1.
Let's analyze "fun" a bit further. It takes an array of objects which we refer to as transitions (as they are similar to the transitions in a state machine). A transition has the form (simplified)
The ability to provide multiple transitions allows us to perform different actions on different conditions. Details about when and which transition will be taken will follow later.
What does the condition
exactly to? It checks whether there's a building at the current position (that is the position of the building in which we define fun) which also has set frame 0 as current frame. If this is true, the condition evaluates to true so the actions can be performed.
We provide a single action
which tries to set the frame of the building at the current position (so of this building) to 1.
2. Multiple transitions
For now, it's boring if our plugin just switched frame 0 to 1. We want to switch back from 1 to 0 eventually. To do so, we can use the ability to provide multiple transitions:
Simple, isn't it? But wait, you might say what if the frame is 0 the first transition's condition will be true so the first action will be performed. But after that, the condition of the second transition becomes true so that the frame will be set back to 0 immediately.
True, but that's not how transitions and their actions are executed. Instead, all conditions of all transitions are evaluated at the same time. After that, only one transition whose condition was true is selected to perform it's actions. If no condition was true, no actions will be performed. How often does this happen? For "fun" this process is performed daily. So the building will switch it's frame each day except you pause the game.
3. Tap sensitivity
You might say that we can achieve the exact same behavior by using an animation. That's true, and for this functionality I would also recommend to use animations instead as it's much easier. But what if we want our building to change it's frame only if the user taps on it? We cannot achieve this with animations alone, but with fun it's fairly easy: We just have to replace "fun" with "on click fun". This fun will be performed only if the user taps on the building.
So our final code for this looks like that:
Note that you can define "fun" and "on click fun" both in a single plugin. So you can do both, daily transitions as well as touch sensitive transitions. This is used for example in these sample plugins:
Wandering Animals
Game of Life
in this topic I'll try to give a step-by-step guide into fun functionality. Dependent on your current knowledge it might need some time getting used to it. However, fun is a real powerful tool to create buildings that react to their environment or to user interaction. By now (version 343) the system is for from completion. Nevertheless, functionality as it's described here should still work with future updates as new functionality will be added primarily.
1. Getting started
To keep it simple I'll just focus on the fun part of a sample plugin. For this reason I'll use frames which are built right into the game so you don't have to bother with them. However, you can make them as complex as usual for your projects. Also building size isn't limited by fun, but a size of width:1, height:1 makes it much easier.
Let this be our starting point of a simple plugin:
Code: Select all
[
{
"id":"$deco_fun_sample00",
"type":"decoration",
"width":1,
"height":1,
"frames":[{"x":288,"y":0,"w":32,"h":32,"count":2,"offset x":1024,"offset y":1024}]
}
]
Code: Select all
[
{
... // Just as before
"fun":[
{
"condition":{"type":"building","frame":0},
"actions":[
{"type":"frame","frame":1}
]
}
]
}
]
Solution: If the building was placed with frame 0, it will eventually switch to frame 1.
Let's analyze "fun" a bit further. It takes an array of objects which we refer to as transitions (as they are similar to the transitions in a state machine). A transition has the form (simplified)
Code: Select all
if (condition) then do actions
What does the condition
Code: Select all
{type":"building","frame":0}
We provide a single action
Code: Select all
{"type":"frame","frame":1}
2. Multiple transitions
For now, it's boring if our plugin just switched frame 0 to 1. We want to switch back from 1 to 0 eventually. To do so, we can use the ability to provide multiple transitions:
Code: Select all
[
{
... // As before
"fun":[
{
"condition":{"type":"building","frame":0},
"actions":[
{"type":"frame","frame":1}
]
},
{
"condition":{"type":"building","frame":1},
"actions":[
{"type":"frame","frame":0}
]
}
]
}
]
True, but that's not how transitions and their actions are executed. Instead, all conditions of all transitions are evaluated at the same time. After that, only one transition whose condition was true is selected to perform it's actions. If no condition was true, no actions will be performed. How often does this happen? For "fun" this process is performed daily. So the building will switch it's frame each day except you pause the game.
3. Tap sensitivity
You might say that we can achieve the exact same behavior by using an animation. That's true, and for this functionality I would also recommend to use animations instead as it's much easier. But what if we want our building to change it's frame only if the user taps on it? We cannot achieve this with animations alone, but with fun it's fairly easy: We just have to replace "fun" with "on click fun". This fun will be performed only if the user taps on the building.
So our final code for this looks like that:
Code: Select all
[
{
"id":"$deco_fun_sample00",
"type":"decoration",
"width":1,
"height":1,
"frames":[{"x":288,"y":0,"w":32,"h":32,"count":2,"offset x":1024,"offset y":1024}],
"on click fun":[
{
"condition":{"type":"building","frame":0},
"actions":[
{"type":"frame","frame":1}
]
},
{
"condition":{"type":"building","frame":1},
"actions":[
{"type":"frame","frame":0}
]
}
]
}
]
Wandering Animals
Game of Life