Skip to content

suraj kumar #4

@surajkumarbusiness995-tech

Description

using UnityEngine;
using System.Collections.Generic;

public class GameManager : MonoBehaviour
{
public static GameManager Instance;
public GameObject playerPrefab;
public GameObject otherPrefab; // prefab for remote player
Dictionary<string, GameObject> players = new Dictionary<string, GameObject>();
public string localPlayerId = "P1";
public bool onlineMode = false;
public string remoteIP = "127.0.0.1";
public int port = 7777;

void Awake()
{
    if (Instance == null) Instance = this; else Destroy(gameObject);
}

void Start()
{
    // Spawn local player
    SpawnLocalPlayer();
}

void Update()
{
    if (!onlineMode) return;

    // Process network incoming messages
    while (SimpleNetwork.Instance != null && SimpleNetwork.Instance.incoming.TryDequeue(out string msg))
    {
        // messages format: "POS|id|x|y"
        msg = msg.Trim();
        if (string.IsNullOrEmpty(msg)) continue;
        string[] parts = msg.Split('|');
        if (parts.Length >= 4 && parts[0] == "POS")
        {
            string id = parts[1];
            if (id == localPlayerId) continue; // ignore our own echoes
            if (players.ContainsKey(id))
            {
                float x = float.Parse(parts[2]);
                float y = float.Parse(parts[3]);
                var pc = players[id].GetComponent<PlayerController>();
                pc.SetPositionFromNetwork(new Vector2(x, y));
            }
            else
            {
                // spawn remote player
                SpawnRemotePlayer(id, new Vector2(float.Parse(parts[2]), float.Parse(parts[3])));
            }
        }
    }

    // If online, broadcast our position periodically
    if (onlineMode)
    {
        BroadcastLocalPosition();
    }
}

void SpawnLocalPlayer()
{
    GameObject p = Instantiate(playerPrefab, Vector3.zero, Quaternion.identity);
    p.name = localPlayerId;
    var pc = p.GetComponent<PlayerController>();
    pc.playerId = localPlayerId;
    players[localPlayerId] = p;
}

void SpawnRemotePlayer(string id, Vector2 pos)
{
    GameObject p = Instantiate(otherPrefab, pos, Quaternion.identity);
    p.name = id;
    var pc = p.GetComponent<PlayerController>();
    pc.playerId = id;
    players[id] = p;
}

public bool IsLocalPlayer(string id)
{
    return id == localPlayerId;
}

float broadcastTimer = 0f;
void BroadcastLocalPosition()
{
    broadcastTimer += Time.deltaTime;
    if (broadcastTimer < 0.05f) return; // 20 msgs/sec
    broadcastTimer = 0f;

    if (!players.ContainsKey(localPlayerId)) return;
    Vector2 pos = players[localPlayerId].transform.position;
    string msg = $"POS|{localPlayerId}|{pos.x}|{pos.y}";
    // if we are server we may want to echo to client; network layer handles send
    SimpleNetwork.Instance?.SendMessageToPeer(msg);
}

// UI / Control methods you can call from buttons:
public void StartAsHost()
{
    onlineMode = true;
    SimpleNetwork.Instance.StartHost(port);
}

public void StartAsClient()
{
    onlineMode = true;
    SimpleNetwork.Instance.StartClient(remoteIP, port);
}

public void Stop()
{
    onlineMode = false;
    SimpleNetwork.Instance.StopNetwork();
}

}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions