Example #1
0
    public GameObject CreateBullet()
    {
        Vector3 rotation = this.gameObject.transform.forward;
        Vector3 position = this.gameObject.transform.position;

        GameObject o = Resources.Load <GameObject>("Bullet");

        GameObject o2 = (GameObject)Instantiate(o);
        //GameObject o2 = PhotonNetwork.Instantiate("Bullet", new Vector3(0, -10, 0), Quaternion.identity, 0);
        Color c = ColorAlgorithm.GetColor(id);

        o2.GetComponent <Renderer>().material.color = c;
        o2.GetComponentInChildren <Light>().color   = c;
        o2.GetComponent <Bullet>().gun = this;
        o2.layer = 22; //The bullet layer
        o2.transform.position = new Vector3(0, -1000, 0);



        //o2.transform.position = position + rotation * 0.7f;
        //bullet.transform.rotation.SetLookRotation(rotation * 3);
        o2.GetComponent <Rigidbody>().velocity = rotation * BULLET_SPEED;
        //o2.GetComponent<Bullet>().network = true;
        //o2.GetComponent<Bullet>().Fire();
        return(o2);
    }
Example #2
0
    public void SetTeam(int id)
    {
        this.id = id;
        Color c = ColorAlgorithm.GetColor(id);

        this.SetColor(c);
        for (int i = 0; i < NUM_OF_BULLETS; i++)
        {
            GameObject o = Resources.Load <GameObject>("Bullet");

            GameObject o2 = (GameObject)Instantiate(o);
            //GameObject o2 = PhotonNetwork.Instantiate("Bullet", new Vector3(0, -10, 0), Quaternion.identity, 0);

            o2.GetComponent <Renderer>().material.color = c;
            o2.GetComponentInChildren <Light>().color   = c;
            o2.GetComponent <Bullet>().gun = this;
            o2.layer = 22; //The bullet layer
            o2.transform.position = new Vector3(0, -1000, 0);

            if (clip == null)
            {
                clip = new Stack <GameObject>();
            }
            clip.Push(o2);
        }
        this.gui = GameObject.Find("AmmoCounter");
        UpdateUI();
    }
Example #3
0
        public YeelightVideo()
        {
            ColorsByAlg = new Dictionary <ColorAlgorithm, Color>();

            colorAlgos = Enum.GetValues(typeof(ColorAlgorithm));
            for (int i = 0; i < colorAlgos.Length; i++)
            {
                ColorAlgorithm colorAlgo = (ColorAlgorithm)colorAlgos.GetValue(i);
                ColorsByAlg.Add(colorAlgo, Color.Black);
            }
        }
    private void Start()
    {
        id          = (int)_PhotonView.owner.customProperties["ID"];
        players[id] = this;

        _Renderer      = this.GetComponentInChildren <Renderer>();
        colorIndicator = GameObject.Find("ColorIndicator").GetComponent <Image>();
        overviewCamera = GameObject.Find("OverviewCam");
        _Camera        = GetComponentInChildren <Camera>();
        _AudioListener = GetComponentInChildren <AudioListener>();


        //Debug.Log("ID: " + id);
        _Renderer.material.color = ColorAlgorithm.GetColor(id);
        //Debug.Log(this.gameObject.layer);
        this.gameObject.layer = LayerMask.NameToLayer("Player " + (id + 1));
        //Debug.Log(this.gameObject.layer);

        teamCamera(_Camera, id);

        if (_PhotonView.isMine)
        {
            overviewCamera.SetActive(false);
            colorIndicator.color = ColorAlgorithm.GetColor(id);
            AudioSource audioSource = this.gameObject.AddComponent <AudioSource>();
            audioSource.clip        = Resources.Load("hillySpace") as AudioClip;
            audioSource.playOnAwake = true;
            audioSource.loop        = true;
            audioSource.volume      = 0.05f;
            audioSource.Play();
        }

        GetComponent <UnityStandardAssets.Characters.FirstPerson.FirstPersonController>().enabled = _PhotonView.isMine;
        _Camera.enabled        = _PhotonView.isMine;
        _AudioListener.enabled = _PhotonView.isMine;

        GameObject gun1 = Resources.Load <GameObject>("Gun");
        GameObject gun2 = (GameObject)Instantiate(gun1);

        //playerID = player.GetComponent<NetworkPlayer>().id;
        //playerID = PhotonView.owner.customProperties["ID"] + 1;
        this.gun = gun2;
        Vector3 pos1 = this.transform.position;

        gun2.transform.position = pos1 + this.transform.forward * 2;

        gun2.transform.parent = this.GetComponentInChildren <Camera>().transform;
        gun2.GetComponent <Gun>().SetTeam(id);


        GameController.GenerateLevel(id);
    }
Example #5
0
    public void Kill(int killer, int victim)
    {
        if (this.kicknext < 0)
        {
            this.kicknext = TIME_TO_LIVE;
        }
        string kill = ColorAlgorithm.GetName(killer) + " has killed " + ColorAlgorithm.GetName(victim);

        kills.Enqueue(kill);
        if (kills.Count > MAXIMUM)
        {
            kills.Dequeue();
        }
        UpdateText();
    }
Example #6
0
    private void UpdateLeaderboards()
    {
        this.Sort();
        Text text = this.gameObject.GetComponent <Text>();

        text.text  = "Kills: " + yourKills + "\n";
        text.text += "-----------------------\n";

        for (int i = 0; i < leaderboards.Length; i++)
        {
            string name = ColorAlgorithm.GetName(leaderboards[i]);
            int    val;
            kills.TryGetValue(leaderboards[i], out val);
            text.text += (i + 1) + " | " + name + " | " + val + "\n";
        }
    }
Example #7
0
        public bool Initialize(string strIP, int numScreen,
                               ColorAlgorithm colorAlgorithm, int numInterval,
                               double dColorBrightFactor,
                               double dBrightFactor, bool bBrightnessEnabled,
                               bool bGeneratePreview, bool bGenerateAllPreview)
        {
            this.strIP               = strIP;
            this.numInterval         = numInterval;
            this.colorAlgorithm      = colorAlgorithm;
            this.dColorBrightFactor  = dColorBrightFactor;
            this.dBrightFactor       = dBrightFactor;
            this.bBrightnessEnabled  = bBrightnessEnabled;
            this.bGeneratePreview    = bGeneratePreview;
            this.bGenerateAllPreview = bGenerateAllPreview;

            screen                = Screen.AllScreens[numScreen];
            bmpScreen             = new Bitmap(screen.Bounds.Width, screen.Bounds.Height);
            bmpScreenSmall        = new Bitmap(screen.Bounds.Width / 8, screen.Bounds.Height / 8, PixelFormat.Format24bppRgb);
            bmpScreenSmallPreview = new Bitmap(screen.Bounds.Width / 8, screen.Bounds.Height / 8, PixelFormat.Format24bppRgb);

            gSnapshot     = Graphics.FromImage(bmpScreen);
            gSmall        = Graphics.FromImage(bmpScreenSmall);
            gSmallPreview = Graphics.FromImage(bmpScreenSmallPreview);

            lightDevice = new Device(strIP);

            try {
                AsyncHelpers.RunSync(lightDevice.Connect);
            } catch {
                return(false);
            }

            lightDevice.SetBrightness(100);
            locker = new LockBitmap(bmpScreenSmall);

            if (colorAlgorithm == ColorAlgorithm.DominantColorThief ||
                bGenerateAllPreview)
            {
                colorThief = new ColorThief();
            }

            return(true);
        }
Example #8
0
        private void LightThread()
        {
            Color lastColor      = new Color();
            int   counter        = 0;
            int   lastBrightness = -1;
            int   lastRGB        = -1;

            for (; ;)
            {
                if (bStop)
                {
                    break;
                }

                Thread.Sleep(TimeSpan.FromMilliseconds(numInterval));
                Console.WriteLine($"Tick {counter++}");
                Status = "OK";

                if (callsPerSecStop.ElapsedMilliseconds > 10000)
                {
                    callsPerSecStop.Restart();
                    CallsPerSec  = (numCalls / 10.0);
                    numCallsMin += numCalls;
                    numCalls     = 0;
                }

                if (callsPerMinStop.ElapsedMilliseconds > 60000)
                {
                    callsPerMinStop.Restart();
                    CallsPerMin = numCallsMin;
                    numCallsMin = 0;
                }

                // snapshot display and copy to bitmap
                //g.CopyFromScreen(-1920, 0, 0, 0, new Size(1920, 1080));
                Rectangle bounds = screen.Bounds;
                gSnapshot.CopyFromScreen(bounds.X, bounds.Y, 0, 0, new Size(bounds.Width, bounds.Height));
                // scale bitmap down 8x
                gSmall.DrawImage(bmpScreen, new Rectangle(0, 0, bmpScreenSmall.Width, bmpScreenSmall.Height));
                if (bGeneratePreview)
                {
                    gSmallPreview.DrawImage(bmpScreen, new Rectangle(0, 0, bmpScreenSmall.Width, bmpScreenSmall.Height));
                }
                //bmpScreenSmall.Save($"F:\\Nuke\\img{counter}.png");

                int   brightness;
                Color color = ParseFrame(colorAlgorithm, out brightness);
                lastPreviewColor = color;

                if (this.bGenerateAllPreview)
                {
                    for (int i = 0; i < colorAlgos.Length; i++)
                    {
                        ColorAlgorithm colorAlgo = (ColorAlgorithm)colorAlgos.GetValue(i);
                        if (colorAlgo == colorAlgorithm)
                        {
                            ColorsByAlg[colorAlgo] = color;
                        }
                        else
                        {
                            int   subBrightness;
                            Color subColor = ParseFrame(colorAlgo, out subBrightness);
                            ColorsByAlg[colorAlgo] = subColor;
                        }
                    }
                }

                int red   = color.R;
                int green = color.G;
                int blue  = color.B;
                int num2  = red << 16 | green << 8 | blue;

                if (bBrightnessEnabled)
                {
                    if (brightness != lastBrightness)
                    {
                        int finalBrightness = (int)(brightness * dBrightFactor);
                        finalBrightness = MathUtil.Clamp(finalBrightness, 1, 100);

                        int       deltaBright    = Math.Abs(finalBrightness - lastBrightness);
                        const int minDeltaBright = 13;

                        if (deltaBright > minDeltaBright)
                        {
                            lastBrightness = finalBrightness;

                            lastLightBrightness = finalBrightness;
                            try {
                                AsyncHelpers.RunSync(() => {
                                    numCalls++;
                                    return(lightDevice.SetBrightness(finalBrightness));
                                });
                            } catch (Exception ex) {
                                Status = ex.Message;
                            }

                            Console.WriteLine($"New brightness: {finalBrightness}");
                        }
                    }
                }

                if (num2 != lastRGB)
                {
                    lastRGB = num2;

                    int deltaR = Math.Abs(red - lastColor.R);
                    int deltaG = Math.Abs(green - lastColor.G);
                    int deltaB = Math.Abs(blue - lastColor.B);

                    const int minDeltaColor = 40;

                    if (deltaR > minDeltaColor ||
                        deltaG > minDeltaColor ||
                        deltaB > minDeltaColor)
                    {
                        //if (deltaR > 5 ||
                        //   deltaG > 5 ||
                        //   deltaB > 5) {
                        lastColor      = color;
                        lastLightColor = color;
                        // dont change if the delta is too small

                        try {
                            AsyncHelpers.RunSync(() => {
                                numCalls++;
                                return(lightDevice.SetRGBColor(red, green, blue));
                            });
                        } catch (Exception ex) {
                            Status = ex.Message;
                        }
                        Console.WriteLine($"New color: {red}, {green}, {blue}");
                    }
                }
                //string strRepeat = StringUtil.RepeatCharacter('■', peakValueBars);
                //Console.WriteLine($"({peakValueCent}) {strRepeat}");
            }

            lightDevice.Disconnect();
        }
Example #9
0
        private Color ParseFrame(ColorAlgorithm colorAlg, out int brightness)
        {
            brightness = 0;
            Color color = new Color();

            switch (colorAlg)
            {
            case ColorAlgorithm.DominantColor: {
                // lock bits
                locker.LockBits();
                color = GetDominantColor(locker.Pixels, out brightness);
                locker.UnlockBits();
            }
            break;

            case ColorAlgorithm.AverageColor: {
                // lock bits
                locker.LockBits();
                color = GetAverageColor(locker.Pixels, out brightness);
                locker.UnlockBits();
            }
            break;

            case ColorAlgorithm.DominantColorThief: {
                locker.LockBits();
                brightness = GetBrightness(locker.Pixels);
                locker.UnlockBits();

                QuantizedColor qColor = colorThief.GetColor(bmpScreenSmall);
                color = Color.FromArgb(qColor.Color.R, qColor.Color.G, qColor.Color.B);
            }
            break;

            case ColorAlgorithm.WeightedBrightestColor: {
                return(Color.Black);

                locker.LockBits();
                color = GetWeightedDominantColor(locker.Pixels, out brightness);
                locker.UnlockBits();
            }
            break;

            case ColorAlgorithm.DominantColorKMeans: {
                locker.LockBits();
                brightness = GetBrightness(locker.Pixels);
                locker.UnlockBits();

                // copy colors
                byte[]       pixels      = locker.Pixels;
                int          totalColors = pixels.Length / 3;
                List <Color> colorList   = new List <Color>(totalColors);

                for (int i = 0; i < pixels.Length - 3; i += 3)
                {
                    byte b = pixels[i];
                    byte g = pixels[i + 1];
                    byte r = pixels[i + 2];

                    Color nColor = Color.FromArgb(255,
                                                  (byte)(r * dColorBrightFactor),
                                                  (byte)(g * dColorBrightFactor),
                                                  (byte)(b * dColorBrightFactor));

                    //l = 0.75;

                    //// convert back to rgb
                    //l *= dColorBrightFactor;
                    //Color nColor = ColorUtil.HSL2RGB(h, s, l);

                    // skip dark pixels
                    //if (b < 50 && g < 50 && r < 50) {
                    //    continue;
                    //}

                    // skip dark pixels
                    //if (KCluster.EuclideanDistance(nColor, Color.Black) >= 200 &&
                    //    KCluster.EuclideanDistance(nColor, Color.White) >= 200) {
                    //}

                    // TODO: skipping pixels make the closest color fluctuate
                    colorList.Add(nColor);
                }

                const int k = 1;

                KMeansClusteringCalculator clustering = new KMeansClusteringCalculator();
                IList <Color> dominantColours         = clustering.Calculate(k, colorList, 5.0d);
                color = dominantColours.FirstOrDefault();

                // convert to HSV
                double h, s, l;
                //ColorUtil.RGB2HSL(b, g, r, out h, out s, out l);

                double[] hsl = SimpleColorTransforms.RgBtoHsl(color);

                Color newColor = SimpleColorTransforms.HsLtoRgb(hsl[0], hsl[1], hsl[2]);

                int x = -1;
            }
            break;
            }
            return(color);
        }
Example #10
0
    public static void GenerateLevel(int id)//curent level plane is 50x50 centered on origin
    {
        Debug.Log("Generating Level. ID:" + id);

        playerID = id;
        color    = ColorAlgorithm.GetColor(playerID);

        /* Puts Walls around Every node */
        Object[,,] walls = new Object[10, 10, 2]; //Tot#Walls for nxm grid = n*(m+1)+ m*(n+1) = 2mn+n+m
        {
            float x, z;
            for (int i = 0; i < 10; i++)
            {
                x = i * 5 - 25;
                for (int j = 0; j < 10; j++)
                {
                    z = j * 5 - 25;
                    if (j != 0)
                    {
                        walls[i, j, 0] = makeWall(x, z, true);
                    }
                    if (i != 0)
                    {
                        walls[i, j, 1] = makeWall(x, z, false);
                    }
                }
            }
            ;
        }


        System.Random rng  = new System.Random();
        Vector2[]     dirs = //Adjacent Node Directions
        {
            new Vector2(0,  -1),
            new Vector2(0,   1),
            new Vector2(1,   0),
            new Vector2(-1, 0)
        };
        HashSet <Vector2> visited  = new HashSet <Vector2>(); //Holds Visited Nodes
        List <Vector2>    deadends = new List <Vector2>();    //Holds Dead Ends

        Stack <Vector2> stack   = new Stack <Vector2>();      //Holds Backtrack Worthy Nodes
        Vector2         current = new Vector2(0, 0);          //Start node

        visited.Add(current);

        List <Vector2> options = new List <Vector2>(); //Holds Viable Directions
        Vector2        adj;                            //Temp for Node in Direction
        Vector2        choice;                         //Selected Directions

        while (true)                                   //Still options left
        {
            /* Loads in Viable Directions */
            options.Clear();
            for (int i = 0; i < dirs.Length; i++)
            {
                adj = current + dirs[i];
                if (!visited.Contains(adj) && adj.x >= 0 && adj.x < 10 && adj.y >= 0 && adj.y < 10)
                {
                    options.Add(dirs[i]);
                }
            }

            if (options.Count <= 0) //Dead End
            {
                deadends.Add(current);
                if (stack.Count <= 0)
                {
                    break;
                }                               //No Backtrack Options = Quit
                else
                {
                    current = stack.Pop();
                }                              //Backtrack = keep trying
            }
            else
            {
                choice = options[0];
                if (options.Count > 1) //Multiple options, add to stack
                {
                    stack.Push(current);
                    choice = options[rng.Next(options.Count)];//pick random adj
                }
                if (choice.x + choice.y < 0)
                {
                    Destroy(walls[(int)current.x, (int)current.y, (int)Mathf.Abs(choice.x)]);
                    current += choice;
                }
                else
                {
                    current += choice;
                    Destroy(walls[(int)current.x, (int)current.y, (int)choice.x]);
                }
                visited.Add(current);
            }
        }
        /* Makes Dead ends less likely */
        for (int d = 0; d < deadends.Count; d++)
        {
            current = deadends[d];
            if (rng.Next(3) != 0)//remove adj wall
            {
                options.Clear();
                for (int i = 0; i < dirs.Length; i++)
                {
                    adj = current + dirs[i];
                    if (adj.x >= 0 && adj.x < 10 && adj.y >= 0 && adj.y < 10)
                    {
                        options.Add(dirs[i]);
                    }
                }
                if (options.Count > 0)
                {
                    choice = options[rng.Next(options.Count)];//pick random adj
                    if (choice.x + choice.y < 0)
                    {
                        Destroy(walls[(int)current.x, (int)current.y, (int)Mathf.Abs(choice.x)]);
                    }
                    else
                    {
                        current += choice;
                        Destroy(walls[(int)current.x, (int)current.y, (int)choice.x]);
                    }
                }
            }
        }
    }