// à revoir en utilisant publishMessage
        public void sendReplay(string sender, string receiver, string fieldName, string fieldValue)
        {
            ReplayMessage msg     = new ReplayMessage(sender, receiver, "content not set", fieldName, fieldValue, DateTime.Now.ToString());
            string        message = MsgSerialization.serialization(msg);

            publishMessage(message, MqttSetting.REPLAY_TOPIC);
        }
Exemple #2
0
    public byte[] SaveToMem()
    {
        int iCount = oReplayMessages.Count;

        byte[] data = new byte[iCount * 32];

        int iOffs = 0;

        for (int i = 0; i < iCount; i++)
        {
            ReplayMessage stAction = oReplayMessages[i];

            byte[] b = BitConverter.GetBytes(stAction.iTimeSlot);
            System.Array.Copy(BitConverter.GetBytes(stAction.iTimeSlot), 0, data, iOffs + 0, 4);
            System.Array.Copy(BitConverter.GetBytes(stAction.iType), 0, data, iOffs + 4, 1);
            System.Array.Copy(BitConverter.GetBytes(stAction.iKeyFlag), 0, data, iOffs + 5, 1);
            System.Array.Copy(BitConverter.GetBytes(stAction.iGeneralByte1), 0, data, iOffs + 6, 1);
            System.Array.Copy(BitConverter.GetBytes(stAction.iGeneralByte2), 0, data, iOffs + 7, 1);
            System.Array.Copy(BitConverter.GetBytes(stAction.iID), 0, data, iOffs + 8, 4);

            //scale floats with 2^18 to get ints that will fit in 32 bit,
            // we never have a float bigger than +-8192.0 so
            System.Array.Copy(BitConverter.GetBytes((int)(stAction.vPos.x * (65536 * 4))), 0, data, iOffs + 12, 4);
            System.Array.Copy(BitConverter.GetBytes((int)(stAction.vPos.y * (65536 * 4))), 0, data, iOffs + 16, 4);
            System.Array.Copy(BitConverter.GetBytes((int)(stAction.vVel.x * (65536 * 4))), 0, data, iOffs + 20, 4);
            System.Array.Copy(BitConverter.GetBytes((int)(stAction.vVel.y * (65536 * 4))), 0, data, iOffs + 24, 4);
            System.Array.Copy(BitConverter.GetBytes((int)(stAction.fDirection * (65536 * 4))), 0, data, iOffs + 28, 4);
            iOffs += 32;
        }
        return(data);
    }
Exemple #3
0
    public bool Get(out ReplayMessage o_stAction, int i_iID) //returns the first action not yet returned, if any (based on iCurTimeSlot), replay only
    {
        int iSize = oReplayMessages.Count;

        if (aiCurGetPosForID[i_iID] < iSize)
        {
            ReplayMessage stAction = oReplayMessages[aiCurGetPosForID[i_iID]];
            while (stAction.iTimeSlot <= iCurTimeSlot)
            {
                if (stAction.iID == i_iID)
                {
                    //do it as fast as possible if less than current (never skip actions)
                    aiCurGetPosForID[i_iID]++;
                    o_stAction = stAction;
                    return(true);
                }
                aiCurGetPosForID[i_iID]++;
                if (aiCurGetPosForID[i_iID] >= iSize)
                {
                    break;
                }
                stAction = oReplayMessages[aiCurGetPosForID[i_iID]];
            }
        }
        o_stAction = new ReplayMessage(); //this will never be used
        return(false);
    }
Exemple #4
0
    public void Reset(int iReplayVersion)
    {
        oReplayMessages.Clear();
        ResetBeforePlay();

        //add replay version as first element
        iVersion = iReplayVersion;
        ReplayMessage rm = new ReplayMessage();

        rm.iType         = (byte)MsgType.REPLAY_VERSION;
        rm.iID           = 0;
        rm.iGeneralByte1 = (byte)iReplayVersion;
        Add(rm);
        rm       = new ReplayMessage();
        rm.iType = (byte)MsgType.REPLAY_EASYMODE;
        rm.iID   = 0;
        if (GameManager.theGM != null)
        {
            rm.iGeneralByte1 = GameManager.theGM.bEasyMode ? (byte)1 : (byte)0;
        }
        else
        {
            rm.iGeneralByte1 = (byte)0;
        }
        bEasyMode = rm.iGeneralByte1 == (byte)1;
        Add(rm);
    }
        // à revoir en utilisant publishMessage
        public void SendReplay(string sender, string receiver, string fieldName, string fieldValue)
        {
            ReplayMessage msg     = new ReplayMessage(sender, receiver, "content not set", fieldName, fieldValue, DateTime.Now.ToString());
            string        message = MsgSerialization.ToXML(msg);

            connector.Publish(IMQTTConnector.REPLAY_TOPIC, message);
        }
Exemple #6
0
    void CreateBullet(Vector2 i_vFirePoint, float i_fDirection, float i_fSpeed, int iBulletNum)
    {
        S_BulletInfo stBulletInfo;

        float fSin = Mathf.Sin(i_fDirection * (Mathf.PI / 180.0f));
        float fCos = Mathf.Cos(i_fDirection * (Mathf.PI / 180.0f));

        stBulletInfo.vPos       = i_vFirePoint; // new Vector2(vPos.x + fCos * i_fOffset, vPos.y + fSin * i_fOffset);
        stBulletInfo.vVel       = new Vector2(vVel.x + fCos * i_fSpeed, vVel.y + fSin * i_fSpeed);
        stBulletInfo.fDirection = i_fDirection;

        GameObject o = Instantiate(oMap.oBulletObjBase, oMap.transform);

        o.GetComponent <Bullet>().Init(stBulletInfo, 1);

        ReplayMessage rm = new ReplayMessage();

        rm.vPos          = stBulletInfo.vPos;
        rm.vVel          = stBulletInfo.vVel;
        rm.fDirection    = stBulletInfo.fDirection;
        rm.iID           = 1;
        rm.iType         = (byte)MsgType.BULLETE_NEW;
        rm.iGeneralByte1 = (byte)iBulletNum; //used for playing sound only for first bullet
        GameLevel.theReplay.Add(rm);
    }
 public static void ReplaySerialize(string path, List <ReplayMessage> data)
 {
     using (FileStream fileStream = File.Create(path))
     {
         using (BinaryWriter binaryWriter = new BinaryWriter(fileStream, Encoding.UTF8))
         {
             for (int i = 0; i < data.Count; i++)
             {
                 ReplayMessage replayMessage = data[i];
                 if (replayMessage.param != null && replayMessage.param.Length > 0 && replayMessage.param.Length <= 31000)
                 {
                     binaryWriter.Write((short)replayMessage.param.Length);
                     binaryWriter.Write((byte)replayMessage.code);
                     binaryWriter.Write(replayMessage.time);
                     binaryWriter.Write(replayMessage.param);
                 }
             }
         }
     }
 }
Exemple #8
0
 public void Add(ReplayMessage i_stAction) //must be added in timeslot order (uses iCurTimeSlot as timeslot), save only
 {
     i_stAction.iTimeSlot = iCurTimeSlot;
     oReplayMessages.Add(i_stAction);
 }
Exemple #9
0
    public bool LoadFromMem(byte[] i_pMem)
    {
        oReplayMessages.Clear();
        ResetBeforePlay();

        if (i_pMem.Length % 32 != 0)
        {
            return(false);                         //not an array containing elements with size 32 bytes
        }
        int iOffset = 0;

        for (int i = 0; i < i_pMem.Length / 32; i++)
        {
            ReplayMessage stAction = new ReplayMessage();

            stAction.iTimeSlot     = BitConverter.ToInt32(i_pMem, iOffset + 0);
            stAction.iType         = (byte)BitConverter.ToChar(i_pMem, iOffset + 4);
            stAction.iKeyFlag      = (byte)BitConverter.ToChar(i_pMem, iOffset + 5);
            stAction.iGeneralByte1 = (byte)BitConverter.ToChar(i_pMem, iOffset + 6);
            stAction.iGeneralByte2 = (byte)BitConverter.ToChar(i_pMem, iOffset + 7);
            stAction.iID           = BitConverter.ToInt32(i_pMem, iOffset + 8);

            //scale down the ints by 2^18 to get floats again,
            // we never have a float bigger than +-8192.0 so
            int px = BitConverter.ToInt32(i_pMem, iOffset + 12);
            int py = BitConverter.ToInt32(i_pMem, iOffset + 16);
            int vx = BitConverter.ToInt32(i_pMem, iOffset + 20);
            int vy = BitConverter.ToInt32(i_pMem, iOffset + 24);
            int d  = BitConverter.ToInt32(i_pMem, iOffset + 28);
            stAction.vPos.x     = BitConverter.ToInt32(i_pMem, iOffset + 12) / (65536 * 4.0f);
            stAction.vPos.y     = BitConverter.ToInt32(i_pMem, iOffset + 16) / (65536 * 4.0f);
            stAction.vVel.x     = BitConverter.ToInt32(i_pMem, iOffset + 20) / (65536 * 4.0f);
            stAction.vVel.y     = BitConverter.ToInt32(i_pMem, iOffset + 24) / (65536 * 4.0f);
            stAction.fDirection = BitConverter.ToInt32(i_pMem, iOffset + 28) / (65536 * 4.0f);

            iOffset += 32;
            oReplayMessages.Add(stAction);
        }

        //get replay version
        if (oReplayMessages.Count > 0 && oReplayMessages[0].iType == (byte)MsgType.REPLAY_VERSION)
        {
            iVersion = oReplayMessages[0].iGeneralByte1;
        }
        else
        {
            iVersion = 0;
        }

        if (oReplayMessages.Count > 1 && oReplayMessages[1].iType == (byte)MsgType.REPLAY_EASYMODE)
        {
            bEasyMode = oReplayMessages[1].iGeneralByte1 == 1;
        }
        else
        {
            bEasyMode = false;
        }


        return(true);
    }
Exemple #10
0
    void FixedUpdate()
    {
        if (!bInited)
        {
            return;
        }

        //////handle creation of all new enemy bullets, could be done in another place...
        if (GameLevel.bRunReplay)
        {
            ReplayMessage rm;
            while (GameLevel.theReplay.Get(out rm, 1))
            {
                if (rm.iType == (byte)MsgType.BULLETE_NEW)
                {
                    //part of CreateBullet()
                    S_BulletInfo stBulletInfo;
                    stBulletInfo.vPos       = rm.vPos;
                    stBulletInfo.vVel       = rm.vVel;
                    stBulletInfo.fDirection = rm.fDirection;
                    GameObject o = Instantiate(oMap.oBulletObjBase, oMap.transform);
                    o.GetComponent <Bullet>().Init(stBulletInfo, 1 /*iOwnerId*/);

                    if (rm.iGeneralByte1 == 0)
                    {
                        GetComponent <AudioSource>().PlayOneShot(oClipFire);
                    }
                }
            }

            if (GameLevel.theReplay.iVersion >= 1)
            {
                if (iNumHits != 0)
                {
                    iNumHits = 3;             //fake always 3 hits left in replay, until kill below
                }
                while (GameLevel.theReplay.Get(out rm, iOwnerId))
                {
                    if (rm.iType == (byte)MsgType.ENEMY_KILL)
                    {
                        bStartExplosion = true;
                        iNumHits        = 0; //kill below
                    }
                }
            }
        }
        //////

        //wait for explosion to finish and remove object
        if (iNumHits == 0 || bExplosionStarted)
        {
            //start explosion
            //note: some bug above can make this run more than once if hit by rapid fire.
            if (bStartExplosion && !bExplosionStarted)
            {
                bStartExplosion   = false;
                bExplosionStarted = true; //hack: fixes bug

                //create kill message
                if (!GameLevel.bRunReplay && GameLevel.theReplay.iVersion >= 1)
                {
                    ReplayMessage rm = new ReplayMessage();
                    rm.iID   = iOwnerId;
                    rm.iType = (byte)MsgType.ENEMY_KILL;
                    GameLevel.theReplay.Add(rm);
                }

                //play explosion sound
                oAudioSource.PlayOneShot(oClipExplosion);

                //add score
                oMap.iAchieveEnemiesKilled++;
                oMap.player.iScore += SENEMY_SCOREPOINTS[stInfo.iEnemyType];

                //add flying score text
                if (oMap.iLevelType == (int)LevelType.MAP_MISSION)
                {
                    S_FlyingScoreInfo stFlyingScoreInfo;
                    stFlyingScoreInfo.iScore = SENEMY_SCOREPOINTS[stInfo.iEnemyType];
                    stFlyingScoreInfo.vPos   = new Vector3(transform.position.x, transform.position.y, -0.2f);
                    stFlyingScoreInfo.vVel   = new Vector3(UnityEngine.Random.Range(-0.15f, 0.15f), UnityEngine.Random.Range(-0.15f, 0.15f), -0.35f);
                    FlyingScore o = Instantiate(oMap.oFlyingScoreObjBase, oMap.transform);
                    o.Init(stFlyingScoreInfo);
                }

                //set the specific enemytype inactive
                enemy0.SetActive(false);
                enemy1.SetActive(false);
                enemy2.SetActive(false);
                enemy3.SetActive(false);
                enemy4.SetActive(false);
                enemy5.SetActive(false);
                enemy6.SetActive(false);

                oExplosionParticle.Play();
                fExplosionTimer = 0.0f;
            }

            fExplosionTimer += Time.fixedDeltaTime;
            if (fExplosionTimer > 2.0f)
            {
                oExplosionParticle.Stop();
                Destroy(gameObject);
            }
        }
        else
        {
            //move along waypoints
            if (stInfo.iNumWayPoints > 1)
            {
                fWPTime -= Time.fixedDeltaTime;
                if (fWPTime <= 0)
                {
                    Vector2 vDist;
                    float   fDist;

                    vDist = new Vector3(stInfo.vWayPoints[iCurWP].x - vPos.x, stInfo.vWayPoints[iCurWP].y - vPos.y, 0);

                    fDist   = vDist.magnitude;
                    fWPTime = fDist / (stInfo.iSpeed / 10.0f / 32.0f);

                    vVel   = new Vector3(vDist.x / fWPTime, vDist.y / fWPTime, 0);
                    iCurWP = (iCurWP + 1) % stInfo.iNumWayPoints;
                    //send change in movement - m_stMsg1.iMsg |= N_MSG1_MOVEMENT;
                }
                vPos += vVel * Time.fixedDeltaTime;
            }

            //fire bullets
            if (!GameLevel.bRunReplay)
            {
                fFireTime -= Time.fixedDeltaTime;
                if (fFireTime <= 0)
                { //fire bullet, set in replay
                    if (stInfo.iFireInterval == -1)
                    {
                        stInfo.iFireInterval = 2000;                             //previous versions random time becomes 2 sec
                    }
                    fFireTime = stInfo.iFireInterval / 1000.0f;

                    if (InFireRange())
                    {
                        oAudioSource.PlayOneShot(oClipFire);

                        float fDirection = stInfo.iAngle;
                        if (SENEMY_RANDOMBULLETANGLE[stInfo.iEnemyType])
                        {
                            fDirection = Random.value * 359;
                        }

                        for (int i = 0; i < SENEMY_NUMBULLETS[stInfo.iEnemyType]; i++)
                        {
                            Vector2 vFirePoint = vPos;
                            if (stInfo.iEnemyType == 5 && i == 1)
                            {
                                vFirePoint += SENEMY5_FIREPOINT2[stInfo.iAngle / 90] / 10.0f;
                            }
                            else
                            {
                                vFirePoint += SENEMY_FIREPOINT[stInfo.iEnemyType, stInfo.iAngle / 90] / 10.0f;
                            }
                            CreateBullet(vFirePoint, fDirection + SENEMY_BULLETANGLE[stInfo.iEnemyType, i], Bullet.BULLETBASEVEL / 2, i);
                        }
                    }
                }
            }
        }

        //set transform
        transform.position = vPos;
    }