#Turn Based Implementation
1 messages ยท Page 1 of 1 (latest)
so what is placement for
it is for placing the gameobject on nodes
these two are for movement after being placed on the nodes
alright
you have any idea about it @junior bough how to implement ?
you have raycast onto a piece right?
Yes
so check if the piece you trying to pick up is only the one that matches the TurnManager
@lethal burrow Did I not tell you NOT to just copy/paste mine. This way you learn nothing
btw. All done
I am not copy/pasting bro. I am not able to implement turnmanager since both goat and lion use the same node script for movement. It is kinda confusing for me what to do
what? The video you posted is my work. Not yours
yup. i didnt say it was mine
but you need to implement what I did into your own game so you can understand what/how/why it is working. This way you learn absolutely nothing
ik. Thats why i am trying to get a hint on how to implement turn manager. ๐ฆ
As @junior bough said. set up an enum you can switch between Tigers and Goats and locate everywhere in the code where it needs to be checked and check it
this is why you need to take the time to understand the code first so you know where to add the checks
I used a bool instead. And checking if the placed object is goat or tiger using tags. but unfortunately it is working only the first time.
So that is the mouse up. what do you check in mouse down?
why would i check in mousedown. It is just instantiation.
dont you think it's a good idea to check if it is someone's turn at the moment of selection? That is in mouse down
i didnt get you @tawdry sigil
you are saying that first we should actually check whether the selection is tiger or goat and then the script should compare it with whatever tag we have written and then it should execute what we have written ?
Think about it.
If you are playing a turn based board game, at which point in a players move do you decide 2 things. 1 Is it his move and 2. what pieces can he move?
we will check it by if he is clicking and compare it by other ones
I asked at which point
after player 1 will play
let me make it simple for you.
At which point in a player move do you decide if it is his move
The start of a move
During the move
The End of the move
?
Start of the move
exactly. And in this case what signifys the start of a move?
whether we are selecting it or not
instantiation if it is the goat.
no, physical action
mouseclick?
yeah
so, where ever you have a mouse down event you need to check, is it this players turn
But how can we check it. We are using the same script for both goat and tiger.
yes, but in the placement script, for example, you have the prefab variable and the tag/name of that prefab tells you what is trying to be selected
ok
the same thing can be applied to the Movement script by using the node.occupant
yeah
trying it
@tawdry sigil we are enabling and disabling placement script based upon the turn. is this wrong?
not necessary. A simple if in MouseDown is sufficient.
this is mine
private void OnMouseDown()
{
if (max > 0)
{
if (GameManager.instance.turn.ToString() == prefab.name)
{
_isSelected = true;
toPlace = Instantiate(prefab);
toPlace.name = prefab.name;
toPlace.transform.position = transform.position;
}
}
}
we made three tigerHomes as the position of them should be fixed when the game starts.
that should not be tigerhome just tiger
you only need the Home prefabs for manual placement. That is what they are for
https://gdl.space/temegovomo.cs
i am writing this enums are they correct ?
so i will use them in onmousedown and create a condition
yes
yeah exactly
okok
but why are they not accessible ?
so you access it as
TurnManager.PlayerType
this
private PlayerType currentPlayer = PlayerType.Tiger;
needs to be public so you can do
TurnManager.instance,currentPlayer
from another script
if (_node.occupant != null && TurnManager.instance.currentPlayer.ToString()==_node.occupant.name)
is this the way in movement to write ?
yes
my enums are working and switching to but how to freeze the moves of respective turns objects
I dont understand, explain
so when we move them the enum is switching between goat and tiger respectively but the movement of them is not freezing meaning that i can move tiger by any amount of turn
I still do not understand what you mean
like tiger will play once then goat right ?
yes
so after playing for tiger the current situation is we can move the tiger once again
then you are not swityching the enum correctly
should i share the code
sure
In placement you have the switchturn in the wrong place. it should only happen if the move was successful
but isnt it succesful? since the position is being changed to node position
and you have no switchturn in your movement code
yeah i noticed it now
not as you have it, no
look in the mouseup method and ask yourself how do I know if the turn was successful?
I am not getting it. sorry, isnt it when the position is equal to node position
no. it is when node != null.
you are doing switchturn always
in movement i can write the switch tunrs anywhere
because on mouse up and down we cant write
check move is checking the movement and then we are calling it in update
in movement the obvious place to call switchturn is after a succesful move in CheckMove
so if i am writing in update it is working but sometimes it is returning the enum double time
exactly i did it
after that too it is happening that when we move the tiger it is not switching
it is behaving like a bug
show code
so now you know how to implement turn based movement
- At the start of a move determine if the player can move the selected piece
- At the end of a successful move, switch turns.
It's that simple
i have one more doubt that the goat should move after the all the 15 goat placed on a node
so should i add the count for it
a bool in TurnManager will do
initially set to false
set to true by placement script when all goats are placed
checked by movement script when it is goats turn
i didnt understand the last line
ok, you you have in movement
private void OnMouseDown()
{
if (_node.occupant != null && TurnManager.instance.currentPlayer.ToString()==_node.occupant.name)
{
//this.GetComponent<SpriteRenderer>().color = Color.yellow;
_isSelected = true;
}
}
which checks the selection against the turn
here you need to add an additional check that if it is Goats turn has he placed all of his goats yet?
should than not be if NOT all Goats placed?
it's round the wrong way
i didnt get it
you are setting to false if all goats have been placed
ya it should be true
when all goat will be placed then the movement should be there
thats what i am writing right ?
like the bool will get true when max ==0 in placement and the toplace.name=="Goat" and in movement if the bool will be true and the player will be the goat then it should be false
is this wrong
no it is not what you are writing
i wrote this in placement added an else if
just look at the code for both if statements and tell me the value of _isSelected
if it is goats turn and all goats have not been placed
NO
then how should i compare in the placement
There is already a very clear place in placement when you know if all goats have been placed
in the on mouseup where toplace is null ?
no.
Mouse up is correct but what do you think this
if (max == 0)
is telling you?
oooo i am sorry i didnt saw that
if you want to even think about being a programmer you need to learn to read and understand every line of code
yeah actually there were no brackets there so .......
but in movement how should i compare then whether it is goat or not if my condition is wrong
simply switch allgoatsplaced to not allgoatsplaced
means we dont need this condition TurnManager.instance.currentPlayer==TurnManager.PlayerType.Goat
as well
then its fine
which one
mousedown
from where ?
movement, that is the one we are working on
i should copy/paste the mousedown of movement to where ?
here
private void OnMouseDown()
{
if (_node.occupant != null && TurnManager.instance.currentPlayer.ToString()==_node.occupant.name)
{
//this.GetComponent<SpriteRenderer>().color = Color.yellow;
_isSelected = true;
}
if(!TurnManager.instance.allgoatplaced && TurnManager.instance.currentPlayer==TurnManager.PlayerType.Goat)
{
_isSelected = false;
}
}
but now also the goat is moving it is not working actually
private void OnMouseDown()
{
// Start of Players Turn
// Is the node occupied ?
// If so, is it occupied by the players piece ?
if (_node.occupant != null && TurnManager.instance.currentPlayer.ToString()==_node.occupant.name)
{
// Yes, so can be selected
//this.GetComponent<SpriteRenderer>().color = Color.yellow;
_isSelected = true;
}
// But wait...
// Goats can only move when all goats are placed
// Is this true ?
if(!TurnManager.instance.allgoatplaced && TurnManager.instance.currentPlayer==TurnManager.PlayerType.Goat)
{
// No it is not so it can not be selected
_isSelected = false;
}
It helps if you add comments to the code
sure
but then also it is not working
but it is still not right because the second if needs to be inside of the first one doesn't it?
wait what
why
because the second if only needs to be evaluated if the first one is true
but after placing it inside it is still moving
i am placing it inside after the first amd setting the isselect=true
so where do you set allgoatplaced to true
what?
i didnt get that
private void OnMouseDown()
{
// Start of Players Turn
// Is the node occupied ?
// If so, is it occupied by the players piece ?
if (_node.occupant != null && TurnManager.instance.currentPlayer.ToString()==_node.occupant.name)
{
// Yes, so can be selected
//this.GetComponent<SpriteRenderer>().color = Color.yellow;
_isSelected = true;
// But wait...
// Goats can only move when all goats are placed
// Is this true ?
if(!TurnManager.instance.allgoatplaced && TurnManager.instance.currentPlayer==TurnManager.PlayerType.Goat)
{
// No it is not so it can not be selected
_isSelected = false;
}
}
}
i placed like this
correct
so back to mouseup of placement
which should be
if (max == 0) {
TurnManager.instance.allgoatplaced = true;
gameObject.SetActive(false);
}
ya i did this earlier
ok. are you still using TigerHome for your preplaced Tigers?
yeah
should i place it in a node
occupied node
don't
remove them you do not need TigerHome at all
ok sure
add 3 Tigers. place them in the correct positions and add them to the correct node.occupant
in inspector right in a node of each 3 of them ?
yes
if you are placing a tiger on node 0, yes
so i have to change something else with it ?
i am just drag and dropping the tiger prefab
first you drag and drop a tiger prefab into the hierarchy
you move it to the node you want in the scene view
and then you can drag and drop the tiger gameobject onto the node in the inspector
then you have not done it correctly
firstly i draged and droped the tiger prefab into hierarchy then i duplicated it into 3 tigers then i drag and droped it into the specified node occupant object
screenshot your unity window with one of the correct nodes selected
that is correct
yeah
Only tiger on node 0 is working for some reason
wait.
change this to
if(TurnManager.instance.currentPlayer==TurnManager.PlayerType.Goat && !TurnManager.instance.allgoatplaced)
check for Goatmove first
trying
show me all 3 Tigers in the hierarchy
Right Now think on this
TurnManager.instance.currentPlayer.ToString()==_node.occupant.name
and this
which is why the placement code contains this
toPlace.name = prefab.name;
it should have the same name
yes
i gave them the same name
but the thing is for me the second one is just moving slighly and then it does not move the other 2 are working fine
then you need to see what is different. it's what we call debugging
There is a bug i came across @tawdry sigil
even tho the tiger on node 0 is jumping to neigbhouring node, the sheep got deleted
is it happening for u too?
that is not a bug, it is correct. The tiger is going from node 0 to node 4 via node 5. it is a legal move
No bro. U played it here? play as coop
it is not a legal move
technically it is a legal move but as for game it should not be a legal move actually
the rules state it is legal to jump a goat if the adjacent node is empty
Bagh-Chal (Nepali: bฤgh cฤl, Newar: เคงเฅเค เคเคพเคธเคพ dhun kasa meaning "tiger game", Hindi: เคฌเคพเค เคฌเคเคฐเฅ bagh bakri) is a strategic, two-player board game that originated in Nepal. The game is asymmetric in that one player controls four tigers and the other player controls up to twenty goats. The tigers 'hunt' the goats while the goats attempt to block the...
i think so it is because the board is square ๐
But anyway, it is easy enough to add any rules you want into the MovementManager script.
For instance atm jumping will always take precedence over moving. The rules state that when both are possible jumping should be optional so that would need to be added
the rule is the same for the Indian boards as well
okok
if you want to make moving take precedence over jumping in the MovementManger script just move the //Check direct connect loop above the //Check jump code in the script
this is why it is important to read and understand the code
I cannot stress this enough
btw, with the code and prefabs you have you do realize that it is very trivial to setup any board shape and configuration you want
can u share where u got those tiger and goat models in your game?
i wanna place those
instead of cube or capsule
fully animated as well, much nicer
ah. its a paid pack
yep, there is a free tiger on the store but no decent free goats
honestly, the animated animals are worth the money, they really bring the game to life, when you guys give me 10 minutes to get back to it I can finish off the animation sequences
U saying u will share yours? ๐
this isn't bad but I found it a bit cartoony for the effect I wanted for the game
no, that would be iilegal
tbh, the game works with any characters, last night I played with Knights and Demons. works just as well
I also had a play with Cars and Motorcycles. It works
Yeah. we were getting a blue screen in mobile build
Shouldn't be a problem. Building to Android?
yup
let me try it rq
yeah
which Unity version you guys using?
2022.3.10
not a good choice 2022 is still very buggy
but its LTS
So what? Unity quality control even for LTS versions is totally crap
is it working for you ? @tawdry sigil
oooooooooo
just installing Android build
I'm using 2021.3.31
is it more stable ?
yes, very much so
this is the res settings i used for android build
Huge window width and height, very much for high end phones
That is my mobile res
switch the aspect ratio from Native to 16:9
it is in 16:9 only
the setup I made was based on a 16:9 ratio
The other thing you can do, this is using a perspective camera, change it to orthographic and adjust the node placement slightly
you will see I have a canvas in the hierarchy with your original game board image. So you can use that to help the placement of the nodes
ok
thanks for helping us @tawdry sigil though i have some doubts in the code that i will ask you tomorrow if its okay with you
Ok, built to android using 16:9 resolution. Works perfectly
checking that
i have to change the editor reso only right ?
no, the android build resolution
apologies for the bad photo
where is it ?
no worries
in the screenshot your friend sent
could be a 2022 thing, it's different in 2021
Also I noticed that the nodes are a little difficult to pick up on mobile so you might want to increase the size of them, they are scaled to 0.7 atm
mind you I have a crappy old phone because I only use it for, guess what, making phone calls
Ok ok
Ok will do it
Very rich guy
old school, very old school. I prefer phones that are attached by a cable and have a dial
ooo ok
@tawdry sigil want some help
Don't you think you've had enough help already? This is your implementation of the game, don't expect me to do all the work for you
I just wanted a explanation
OK, what do you want explaining?
wait one whilst I bring up the code
yeah sure
To start with we have this
// Fill lines with node numbers
lines[0] = new List<int>() { 0, 2, 8, 14, 19 };
lines[1] = new List<int>() { 0, 3, 9, 15, 20 };
lines[2] = new List<int>() { 0, 4, 10, 16, 21 };
lines[3] = new List<int>() { 0, 5, 11, 17, 22 };
lines[4] = new List<int>() { 1, 2, 3, 4, 5, 6 };
lines[5] = new List<int>() { 7, 8, 9, 10, 11, 12 };
lines[6] = new List<int>() { 13, 14, 15, 16, 17, 18 };
lines[7] = new List<int>() { 19, 20, 21, 22 };
lines[8] = new List<int>() { 1, 7, 13 };
lines[9] = new List<int>() { 6, 12, 18 };
which fills our lines array with all the node numbers on each line of the board
do you understand this?
yeah
ok next we have this
// Calculate node connections
foreach (Node node in nodes)
{
foreach (List<int> line in lines)
{
for (i=0;i<line.Count;i++)
{
if (line[i] == node.ix)
{
if (i > 0) node.connects2.Add(line[i - 1]);
if (i < line.Count - 1) node.connects2.Add(line[i + 1]);
break;
}
}
}
}
which takes those lines and for each node it tells it which nodes it is connected to
do you understand this?
yes
Ok, so then it is simple in this code
// Check direct connect
foreach (int ix in startNode.connects2)
{
if (ix == end)
{
return endNode;
}
}
we have a start and an end node
so because connects2 of a node tells us exactly which nodes this node is directly connected to
if the connnects2 List of the start node contains the end node we know they are directly connected and we already checked if the end node is empty so that start end combination is a valid move so we pass back a node to the caller as proof of this
Ok ok got it
this could also have been written as
if (startNode.connects2.Contains(end)) return endNode
i was just trying to write the win condition for goat so i just got confused that what was that code for ๐
win condition for goat is quite tricky however the stalemate condition is very tricky indeed
yeah
my approach was i should check whether the neighbour nodes and jump nodes are occupied or not
firstly, you only need to check the nodes which are occupied by a tiger
ok
so
- is the node occupied by a tiger
- if this node has an empty connecting node - can move
- if this node has a connecting node occupied by a goat and that node has an empty connecting node - can move
wait til you try to figure out the stalemate logic and implement that. I will be interested to see what you come up with, as I said, it's very tricky
yeah