Arcade Village Blog

The Island, the providers

For lovers of programming, let's continue our journey to The Island.
As a reminder, a plot (class Plot) is a stack of blocks. But the parcel can also contain a provider. The provider draws above the plot and can only access the resources of the highest block.
I also remind you that you can see the progress of the game here:
The Island

The first thing you will probably see is the behavior of the creatures. But I'll find the bug and we'll talk about creatures later.

A provider, as the name suggests, provides resources. Resources allow the player to get food or craft various items.
Why did I not retain, as in Minecraft, the block/resource pair?
Quite simply because the block is for me only a support for the resources. On a block of dirt, you can collect dirt, but grass will also grow there. This herb can be useful to a player or consumed by a herbivore.
I also found that the supplier allowed more flexibility.
On a block of sand by the sea, the player could harvest seaweed. But on a block of desert sand, cacti will grow.
In the Minecraft game, each block provides a material. When you realize that this game offers almost infinite terrain, you can believe in magic.
As part of my javascript game, I think I'm limited on the display speed and the ability to save the playing field. The Block/Supplier separation also allows me to display the game screen in two superimposed parts:
- The first part contains the terrain itself (therefore the blocks). It is drawn only once.
- The second part contains the player, the creatures and the suppliers. This is because providers change based on the amount of resources they can offer. It is an important element for the player.

You can see an example provider on the game screen, it is ProviderHerb. It is usually found on a block of land, but can attempt to survive in lakeside areas.
Depending on the amount of blades of grass (and therefore of food present for herbivores, for example), you will see that the design is different.
You can also notice that a supplier can colonize the squares adjacent to the one where he is.
Now let's see the Provider class which is the ancestor of all providers.
From tie to time, you will meet french commentary in the code, sorry, it is my mother tongue.

The Provider class



class Provider
{
constructor( gaia, p, ftype, rone )
{
this.terrain = gaia;
this.parcelle = p;
this.ftype = ftype;
this.rtype = RESSOURCE_RIEN;
this.rmax = 0;
this.rone = rone;
this.ramount = 0;
this.ramount_ex = -1;
this.rw = this.rone;
this.dcout = 0;
}

getType()
{
return this.ftype;
}

getCoutDeplacement()
{
return this.dcout;
}

onBiomeCreation()
{

}

process()
{
this.ramount_ex = this.ramount;

if ( this.rw++ < this.rone ) return;
this.rw = 0;
this.grow();
}

grow()
{
}

getRessource( rask )
{
if ( rask != this.rtype ) return 0;
return Math.trunc(this.ramount);
}

decRessource( amount )
{
if ( amount > this.ramount )
{
return false;
}
this.ramount -= amount;
return true;
}


draw( g )
{
}
}

The constructor function tells the provider the land (gaia), the plot (p) it is on, the type of provider (ftype) and its grow time (rone)
You see two functions:
- getRessource( rask ) which allows to know if a resource is provided
- decRessource( amount ) which decreases the number of resources.
As you have subtly understood (you are subtle, I know!), to simplify the game, each supplier produces only one resource currently. It's a choice because I want to see my game evolve as I program.
Finally, two functions, grow and draw, are called by the game engine. draw for the drawing part and grow for the increase in resources.
I was talking to you earlier about ProviderHerb, here is the code.

The ProviderHerb class


class ProviderHerb extends Provider
{
constructor(gaia, p)
{
super(gaia, p, PROVIDER_HERB, 10);
this.rtype = RESSOURCE_HERB;
this.rmax = 20;
this.acote = this.parcelle.width / this.rmax;
this.pcote = 0;
this.pdecale = 0;
this.ramount = 1;
this.brins_x = new Array(this.rmax);
this.brins_y = new Array(this.rmax);
this.ibrin = 0;
for (this.ibrin = 0 ; this.ibrin < this.rmax ; this.ibrin++ )
{
this.brins_x[this.ibrin] = Math.trunc(Math.random() * WCASE );
this.brins_y[this.ibrin] = Math.trunc(Math.random() * HCASE );
}
}

grow()
{
if ( this.ramount == 0 && Math.random() > .6 ) return; // Quand le champs est vide, il est plus difficile de repousser
this.ramount_ex = this.ramount;
if ( this.parcelle.getWater() > 10 && this.parcelle.getNutriment() > 10 )
{
if ( this.ramount >= this.rmax )
{
if ( Math.random() > .7)
{
if ( this.parcelle.essaime() ) this.ramount-= 3;
}
}
else
{
this.ramount *= 1.20;
if ( this.ramount > this.rmax)
this.ramount = this.rmax;
if ( this.ramount == 0 ) this.ramount = 1;
}
}
else
{
if ( this.ramount > 0 ) this.ramount--;
}
}

onBiomeCreation()
{
this.ramount = Math.round(this.rmax * .80 );
}

draw(g)
{

g.setColor("#80D080");
for (this.ibrin = 0 ; this.ibrin < Math.trunc(this.ramount) ; this.ibrin++ )
{
g.fillRect(this.parcelle.x + this.brins_x[this.ibrin],
this.parcelle.y+ this.brins_y[this.ibrin],2,3);
}

}
}

I leave you to study the code. The variables containing the word 'brin' make it possible to display blades of grass according to the quantity of RESOURCE_HERB resources.
The grow function increases or decreases the resources depending on the terrain and, when the resources are at the maximum ( this.ramount >= this.rmax), asks the Gaia class to be able to grow on another space.
I remind you that you can see the progress of the game here:

The Island

Next time we'll talk about creatures and add the player character. Me, I have to find out why the creatures go in the water...