#How to implement it?
1 messages · Page 1 of 1 (latest)
Cool. thanks
Start with, if you take your board and create a gameobject for each point.
Each one of those gameobjects would get a Node script
Done
I would make my Tigers script into a singleton
Should i make your tigers script derive from monobehaviour?
yes, my Node class also
btw, this is the full list of the lines array
// 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 };
you'll note it has 10 entries not 9 as I put in my script
alright i made two scripts for node, tigers and made instances of them
so you have 24 game objects?
23 represent each point and has a Node script
and 1 with the tigers script
I see you have a GameManager there, that should probably hold the Tiger script
gamemanager is pratically empty. I made a seperate script for the tigers class
doesn't matter.
ok first, in the nodes array of the Tiger script you need to drag in each of your points in the correct sequence
Done
ok, do you understand my lines array?
Not really. I never used array of lists before
ok, so on your board you have 10 lines. Each line is represented by one entry in the array
in each line is a List of the points in that line
yes
ok so now it is simple
when you make a move you need to select a start point and an end point, correct?
yup
start point = the placce the tiger is already on
end point = mouse clicked position. ie the neighbouring one
ok, it may be easier to add a point number to the node class so you can set that in the inspector for each point
this is what the final game looks like. https://www.onlinesologames.com/tigers-and-goats
i didnt understand, u want me to make a new variable?
yes like this
public class Node : MonoBehaviour
{
public int ix;
public GameObject point;
public GameObject occupant;
}
it just makes it easier to see which point is which
so i change the number to the node number now right?
yes
done
ok. So now checking is easy.
you have the 2 node numbers for start and end
you call the isValidMove method of the Tigers script and pass in those two numbers
if it returns true it's valid, if it returns false it's not valid
i pass ix as start and and tigers.nodes as end??
you pass the ix from the start point as start and the ix of the end point as end
I didnt understand. Is it too much to ask if you could write the tiger movement script too? 😛
not a chance
you have 2 points selected. both have a Node script attached. you get the ix value from both of those scripts
yeah. ill write the tiger movement script and keep you posted.
ok, before you go there is one more thing you need to do to make this work
when you place a Tiger or a Sheep on a point you need to fill the occupant variable in the corresponding Node script with that gameobject, so the point knows what is on the point
I need to call the Init() function in tigers in start right?
i am getting this error.
IndexOutOfRangeException: Index was outside the bounds of the array.
on line 36
I did tell you the array has 10 entries, in my script I only defined 9
That makes no sense
which line
What am i supposed to do instead of ray
it's not the ray which is the problem.
first you check MouseDown for this object, then you use MousePosition to find the second object. but mousposition will be on the first selected object
you need to re think your logic of 'how do I select 2 different points'
tbh If I were to make this I would do it as a UI game then each point could be a button
can u change the script and share if possible? I am not getting the logic
How exactly with buttons
I'm not going to change your script but look at this
if (Input.GetMouseButtonDown(0)){
RaycastHit2D rayHit = Physics2D.GetRayIntersection(Camera.main.ScreenPointToRay(Input.mousePosition));
where do you think the mouse is at this time?
Each point is a button, when you click on it it fires an event which you can capture. When 2 buttons have been clicked then you use the isValidMove method
The tiger current position?
exactly, but you are treating it like it is where you want the tiger to move to
removed it.
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class TigerMovement : MonoBehaviour
{
public bool _isSelected = false;
private Color initialColor;
private Node _node;
private void Start()
{
initialColor = this.GetComponent<SpriteRenderer>().color;
}
private void OnMouseDown()
{
_isSelected = true;
}
private void Update(){
if (_isSelected){
this.GetComponent<SpriteRenderer>().color = Color.yellow;
RaycastHit2D rayHit = Physics2D.GetRayIntersection(Camera.main.ScreenPointToRay(Input.mousePosition));
if (rayHit.collider != null){
if (Tigers.Instance.IsValidMove(this._node.ix, rayHit.transform.GetComponent<Node>().ix)){
this.transform.position = rayHit.transform.position;
}
}
}
}
private void OnTriggerEnter2D(Collider2D other){
if (other.transform.CompareTag("Node")){
_node = other.GetComponent<Node>();
}
}
}
this script is on a point object, yes?
No, on tiger sprite
wrong. it needs to be on the point because that is what has the node script
the tiger needs no script
U mean the red dots right?
yes
But how can this script an instance if its not a single script
arent instances supposed to be only one
I think you are confused
Singletons only have 1 instance, all other Monoscripts can have multiple instances.
In this case you need each red dot to be able to know when MouseDown has happened to it so therefore this script should be on each red dot
It strikes me that you have not really sat down and designed what you want to be happening
Believe me i have been on this for a week, got no where.
I finished half of it by myself, but not able to do the kill movement
A week? Hmm, I could have made this in the time it's taken to have this conversation
I did guess
np
let's look at your design
your mechanic is click on a red dot occupied by a Tiger, drag the mouse to a new red dot and see if the is a valid move. If it is then move the Tiger to the new red dot and remove the Sheep from play. Correct?
have you played the game i posted above.
would not load
you would better understand if you played it
Yup
OK, my logic is not going to work because it only allows Tigers to jump in a straight line
https://gdl.space/ikuvibatop.cs
try this.
should work for Tigers and Sheep. but still only in a straight line
Movement script should be on each red dot
I replace tigers and node script with above scripts or all scripts in one
each script must have it's own file
now the only thing you have to do is when you are placing Tigers and Sheep on the board to set the red dot Node.occupant with the Tiger or Sheep gameobjet
Node and Movement
Tigers on it's own gameobject or the gamemanager object
The logic is, Tigers and Sheep do not move themselves, they are moved by the red dots which they occupy
occupant should be tiger right?
if a Tiger has been placed on that red dot, yes
if a Sheep has been placed then it should be sheep
So here you have a Tiger on the pinacle node
getting null ref on 18
please think about things yourself, dont just copy/paste
deliberate mistake
public Node _node;
then set in inspector
is this supposed to happen?
what are you doing your mouse down on?
I tried on tiger. But its not moving
Then I clicked on node.
It's color is changing to yellow because it is selected.
done. same result
show the inspector for node 1
can i add u as friend and share screen? feel that would make our communcation alot better
no
that is node 0, I said 1
So why is the Ix 0 and not 1 ?
why does it have a tiger as occupant? It is empty
did you do it for all nodes?
yup
set the correct ix and only set occupant for occpied nodes
so should work now
you might like to add code to the movement script so that the Tiger or Sheep follow the mouse
whats line 49 of Tigers?
there is no way that line can throw an Argument out of range exception
wait, my bad
replace the foreach with
for (int i = 0; i < selLine.Count; i++)
{
if (selLine[i] == start) startIx = i;
if (selLine[i] == end) endIx = i;
}
cool
so same tigers script for all the sheep?
no, the Tigers script you have will work for both Tigers and Sheep so you only need 1 instance of it for both
ok so u mean, i need to make tigers script such that there is only one script in the game and it controls all the tigers and sheeps movement
am i right?
yes, that is what it is atm by being a singleton
you could rename it to something a little more sensible
something like MovementManager for example
alright i did as u said. It is only working on first tiger.
nvm. it is working for all the tigers
how do i change the occupants of the nodes to the goats now?
Do you have code to place them on the grid ?
I thought its the same for both
placement and movement are 2 different things
how are you placing your Tigers on the grid?
well that's not right is it? You will need to write code to place them
something like this?https://gdl.space/oyuheburon.cs
i was doing this previously to place the sheep on grid
all you need to add when placing is
Node node = rayHit.transform.GetComponent<Node>();
if (node.occupant == null) node.occupant = gameObject;
but the script looks a mess so you might want to re-write it
The same script should be able to work for both Tigers and Goats
how to make the tiger jump over sheep.
that was the main problem i faced to begin with.
that is already in the tigers script
Node ix is wrong, should be 5
pinacle is node 0. they should be numbered from left to right, top to bottom
Maybe you should add some Debug.Logs into isValidMove so you get a sense of what it is doing and how/why it works
Actually just checked. The Node.ix is correct for node 6. but you've added the sheep to node 6 instead of node 5
hey bro u still here?
this error is still persisting
Damn. U even implemented the vertical kill also.
Haven't u completed everything. What is left?
Thanks alot btw.
You really helped me out of a pinch
you have to implement the turn based logic. and the win/lose/stalemate logic. Have fun.
btw. this took 2 hours to make.
just working on a revised version so that you can also jump not in a straight line
it's a fun little exercise, keeps me from doing real work
@vestal nymph Have revised the logic in MovementManager so that you can jump in any direction.
Amazingly is simplifies the logic in isValidMove considerably