#How to implement it?

1 messages · Page 1 of 1 (latest)

formal crow
#

you mean from scratch?
Lets take this to a thread

vestal nymph
#

Cool. thanks

formal crow
#

Start with, if you take your board and create a gameobject for each point.
Each one of those gameobjects would get a Node script

formal crow
#

I would make my Tigers script into a singleton

vestal nymph
#

Should i make your tigers script derive from monobehaviour?

formal crow
#

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

vestal nymph
#

alright i made two scripts for node, tigers and made instances of them

formal crow
#

so you have 24 game objects?
23 represent each point and has a Node script
and 1 with the tigers script

vestal nymph
#

yes

formal crow
#

I see you have a GameManager there, that should probably hold the Tiger script

vestal nymph
#

gamemanager is pratically empty. I made a seperate script for the tigers class

formal crow
#

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

vestal nymph
#

Done

formal crow
#

ok, do you understand my lines array?

vestal nymph
#

Not really. I never used array of lists before

formal crow
#

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

vestal nymph
#

yes

formal crow
#

ok so now it is simple
when you make a move you need to select a start point and an end point, correct?

vestal nymph
#

yup

#

start point = the placce the tiger is already on

#

end point = mouse clicked position. ie the neighbouring one

formal crow
#

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

vestal nymph
vestal nymph
formal crow
#

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

vestal nymph
#

so i change the number to the node number now right?

formal crow
#

yes

vestal nymph
#

done

formal crow
#

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

vestal nymph
#

i pass ix as start and and tigers.nodes as end??

formal crow
#

you pass the ix from the start point as start and the ix of the end point as end

vestal nymph
#

I didnt understand. Is it too much to ask if you could write the tiger movement script too? 😛

formal crow
#

not a chance

vestal nymph
#

haha.

#

np

formal crow
#

you have 2 points selected. both have a Node script attached. you get the ix value from both of those scripts

vestal nymph
formal crow
#

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

vestal nymph
#

I need to call the Init() function in tigers in start right?

formal crow
#

yes

#

or just change it to Awake

vestal nymph
#

i am getting this error.

#

IndexOutOfRangeException: Index was outside the bounds of the array.

#

on line 36

formal crow
#

I did tell you the array has 10 entries, in my script I only defined 9

vestal nymph
#

doing this for movement.

formal crow
#

That makes no sense

vestal nymph
#

which line

formal crow
#

all of it

#

your update is just checking itself

vestal nymph
#

What am i supposed to do instead of ray

formal crow
#

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

vestal nymph
formal crow
formal crow
vestal nymph
formal crow
#

exactly, but you are treating it like it is where you want the tiger to move to

vestal nymph
#

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>();
        }
    }
}
formal crow
#

this script is on a point object, yes?

vestal nymph
#

No, on tiger sprite

formal crow
#

wrong. it needs to be on the point because that is what has the node script

#

the tiger needs no script

vestal nymph
formal crow
#

yes

vestal nymph
#

But how can this script an instance if its not a single script

#

arent instances supposed to be only one

formal crow
#

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

vestal nymph
#

I finished half of it by myself, but not able to do the kill movement

formal crow
#

A week? Hmm, I could have made this in the time it's taken to have this conversation

vestal nymph
#

😦

#

i am beginner bro.

formal crow
#

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?

vestal nymph
formal crow
#

would not load

vestal nymph
#

you would better understand if you played it

formal crow
#

loaded now, let me take a look

#

Ah, I see, It's kinda like Go

vestal nymph
#

Yup

formal crow
#

OK, my logic is not going to work because it only allows Tigers to jump in a straight line

vestal nymph
#

Oh.

#

What should I do bro

formal crow
#

Movement script should be on each red dot

vestal nymph
#

I replace tigers and node script with above scripts or all scripts in one

formal crow
#

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

vestal nymph
#

what scripts should the red dot(Nodes) have

#

Node, tigers

#

and movement also?

formal crow
#

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

vestal nymph
#

occupant should be tiger right?

formal crow
#

if a Sheep has been placed then it should be sheep

formal crow
vestal nymph
#

getting null ref on 18

formal crow
#

please think about things yourself, dont just copy/paste

#

deliberate mistake

    public Node _node;
#

then set in inspector

vestal nymph
formal crow
#

what are you doing your mouse down on?

vestal nymph
#

I tried on tiger. But its not moving

#

Then I clicked on node.

#

It's color is changing to yellow because it is selected.

formal crow
#

you need to set the Tigers to ignore raycasts

#

and the Sheep

vestal nymph
#

done. same result

formal crow
#

show the inspector for node 1

vestal nymph
#

can i add u as friend and share screen? feel that would make our communcation alot better

formal crow
#

no

vestal nymph
formal crow
#

that is node 0, I said 1

vestal nymph
#

this is tigers.

formal crow
#

So why is the Ix 0 and not 1 ?
why does it have a tiger as occupant? It is empty

vestal nymph
#

done

formal crow
#

did you do it for all nodes?

vestal nymph
#

yup

formal crow
#

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

vestal nymph
#

no bro. not working

#

tiger is right?

formal crow
#

whats line 49 of Tigers?

vestal nymph
#

it is because i dragged too many times. I dont think it is an error

formal crow
#

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;
        }

vestal nymph
#

👍

formal crow
#

cool

vestal nymph
#

so same tigers script for all the sheep?

formal crow
#

no, the Tigers script you have will work for both Tigers and Sheep so you only need 1 instance of it for both

vestal nymph
#

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?

formal crow
#

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

vestal nymph
#

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?

formal crow
#

Do you have code to place them on the grid ?

vestal nymph
#

I thought its the same for both

formal crow
#

placement and movement are 2 different things

#

how are you placing your Tigers on the grid?

vestal nymph
#

they are already placed

#

so no need to place them

formal crow
#

well that's not right is it? You will need to write code to place them

vestal nymph
#

i was doing this previously to place the sheep on grid

formal crow
#

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

vestal nymph
#

how to make the tiger jump over sheep.

#

that was the main problem i faced to begin with.

formal crow
#

that is already in the tigers script

vestal nymph
#

it is not working then.

formal crow
#

show me the inspector of the sheep

#

and the inspector of the node the sheep is on

vestal nymph
formal crow
#

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

vestal nymph
formal crow
#

I leave the rest of the game logic to you

vestal nymph
vestal nymph
#

Thanks alot btw.

#

You really helped me out of a pinch

formal crow
#

it's a fun little exercise, keeps me from doing real work

formal crow
#

@vestal nymph Have revised the logic in MovementManager so that you can jump in any direction.
Amazingly is simplifies the logic in isValidMove considerably

vestal nymph
#

nice!

#

how should i implement the turn manager.

#

can u give me a blueprint?

#

like explain.