예제 #1
0
파일: User.cs 프로젝트: huangtao36/SkyLands
        public void Update(float frameTime)
        {
            MainState     mainState  = this.mStateMgr.MainState;
            VanillaPlayer mainPlayer = mainState.CharacMgr.MainPlayer;

            if (this.mIsMainGUIOpen && this.mTimeSinceGUIOpen.Milliseconds >= 3500 && this.mStateMgr.GameInfo.IsInEditorMode)
            {
                this.mIsMainGUIOpen = false;
                GUI.Visible         = false;
            }

            if (this.mStateMgr.Controller.HasActionOccured(UserAction.Start))
            {
                if (!GUI.Visible)
                {
                    this.mStateMgr.MainState.OpenMainGUI();
                    this.SwitchGUIVisibility(true);
                    this.mIsMainGUIOpen = true;
                    this.mTimeSinceGUIOpen.Reset();
                }
                else
                {
                    if (this.mIsInventoryOpen)
                    {
                        this.SwitchInventory(false);
                        this.mIsInventoryOpen = false;
                    }
                    else if (this.mIsBuilderOpen)
                    {
                        this.mIsBuilderOpen = false;
                        if (this.BuildingMgr.HasActualBuilding() && !this.BuildingMgr.GetActualBuilding().Built)
                        {
                            this.BuildingMgr.GetActualBuilding().WaitForRessources();
                        }
                    }
                    else
                    {
                        this.mIsMainGUIOpen = false;

                        /*if (this.mStateMgr.GameInfo.IsInEditorMode)
                         *  (this.mStateMgr.MainState as StoryEditorState).OnExit();*/
                    }

                    this.SwitchGUIVisibility(false);
                    GUI.Visible = false;
                }
            }

            if (RequestBuilderClose && this.mIsBuilderOpen)
            {
                RequestBuilderClose = false;
                this.mIsBuilderOpen = false;
                this.SwitchGUIVisibility(false);
                GUI.Visible = false;
            }

            if (!this.mStateMgr.Controller.IsActionOccuring(UserAction.MainAction))
            {
                if (this.mStateMgr.Controller.HasActionOccured(UserAction.Inventory) && (!GUI.Visible || this.mIsInventoryOpen))
                {
                    this.mIsInventoryOpen = !this.mIsInventoryOpen;
                    this.SwitchInventory(this.mIsInventoryOpen);

                    this.SwitchGUIVisibility(this.mIsInventoryOpen);
                }
            }

            if (OpenBuilder)
            {
                Builder.OnOpen = this.OnBuilderOpen;
                new Builder(this.BuildingMgr);
                OpenBuilder         = false;
                this.mIsBuilderOpen = true;
                this.SwitchGUIVisibility(true);
            }

            if (this.mStateMgr.Controller.HasActionOccured(UserAction.CreateUnit))
            {
                this.PlayerRTS.CreateRobot(1);
            }

            if (this.mStateMgr.Controller.HasActionOccured(UserAction.SelectAll))
            {
                this.mAreAllCharacSelected = !this.mAreAllCharacSelected;
                foreach (VanillaNonPlayer charac in this.mStateMgr.MainState.CharacMgr.GetFactionCharacters(Faction.Blue).OfType <VanillaNonPlayer>())
                {
                    if (!charac.IsFriendlySelected() && this.mAreAllCharacSelected)
                    {
                        this.mSelectedAllies.Add(charac);
                    }
                    else if (charac.IsFriendlySelected() && !this.mAreAllCharacSelected)
                    {
                        this.mSelectedAllies.Remove(charac);
                    }

                    charac.SetSelected(this.mAreAllCharacSelected);
                }
            }

            if (this.mStateMgr.Controller.HasActionOccured(UserAction.GoTo))
            {
                int nbAllies = this.mSelectedAllies.Count;
                if (nbAllies <= 0)
                {
                    return;
                }
                VanillaNonPlayer npc = this.GetSelectedNPC();
                if (npc != null && npc.Info.Faction == Faction.Red)
                {
                    foreach (VanillaNonPlayer ally in this.mSelectedAllies)
                    {
                        ally.SetTargetAndFollow(npc);
                    }
                }
                else if (this.SelectedBlockPos != Vector3.ZERO)
                {
                    int     randomMax   = (int)((nbAllies / 4f + 0.5f) * Cst.CUBE_SIDE);
                    Vector3 imprecision = nbAllies == 1 ? Vector3.ZERO : new Vector3(this.mRandom.Next(0, randomMax), 0, this.mRandom.Next(0, randomMax));
                    foreach (VanillaNonPlayer ally in this.mSelectedAllies)
                    {
                        ally.MoveTo((this.SelectedBlockPos + new Vector3(0.5f, 1, -0.5f)) * Cst.CUBE_SIDE + imprecision);
                    }
                }
            }

            if (this.mStateMgr.Controller.HasActionOccured(UserAction.FollowMe))
            {
                foreach (VanillaNonPlayer ally in this.mSelectedAllies)
                {
                    if (ally.IsFollowingCharac(mainPlayer))
                    {
                        ally.StopFollowing();
                    }
                    else
                    {
                        ally.Follow(mainPlayer, Cst.CUBE_SIDE * 2 + this.mRandom.Next(0, 100));
                    }
                }
            }

            /* Move camera */
            if (this.IsAllowedToMoveCam)
            {
                bool secondaryActionHandled = false;
                if (this.IsFreeCamMode)
                {
                    this.mCameraMan.UpdateCamera(frameTime, this.mStateMgr.Controller);
                }
                else if (mainPlayer.MovementInfo.IsAllowedToMove) // Just pitch the camera
                {
                    this.mCamPitchNode.Pitch(new Degree(this.mStateMgr.Controller.Pitch));

                    if (2 * new Degree(Math.ACos(this.mCamPitchNode.Orientation.w)).ValueAngleUnits > 90.0f) // Limit the pitch between -90 degrees and +90 degrees
                    {
                        this.mCamPitchNode.SetOrientation(Math.Sqrt(0.5f), Math.Sqrt(0.5f) * Math.Sign(this.mCamPitchNode.Orientation.x), 0, 0);
                    }

                    if (this.mStateMgr.Controller.HasActionOccured(UserAction.SecondaryAction))
                    {
                        secondaryActionHandled = this.UpdateSelectedNPC();
                    }
                }

                /* Cube addition and suppression */
                float dist = this.UpdateSelectedBlock();
                if (!(this.mStateMgr.GameInfo.IsInEditorMode ^ mainState.User.IsFreeCamMode) && this.SelectedBlock != null)    // Allow world edition
                {
                    if (this.mStateMgr.Controller.HasActionOccured(UserAction.MainAction) && !Selector.IsBullet && this.mWorld.onLeftClick(this.SelectedBlockPos))
                    {
                        this.AddBlock(dist);
                    }
                    if (!secondaryActionHandled && this.mStateMgr.Controller.HasActionOccured(UserAction.SecondaryAction) && this.mWorld.onRightClick(this.SelectedBlockPos))
                    {
                        this.DeleteBlock();
                    }
                }
            }

            /* Move Selector */
            int selectorPos = Selector.SelectorPos;

            if (this.mStateMgr.Controller.HasActionOccured(UserAction.MoveSelectorLeft))
            {
                selectorPos--;
            }
            if (this.mStateMgr.Controller.HasActionOccured(UserAction.MoveSelectorRight))
            {
                selectorPos++;
            }
            for (int i = 0; i < this.mFigures.Length; i++)
            {
                if (this.mStateMgr.Controller.WasKeyPressed(this.mFigures[i]))
                {
                    selectorPos = i;
                    break;
                }
            }
            if (Selector.SelectorPos != selectorPos)
            {
                Selector.SelectorPos = selectorPos;
            }
        }
예제 #2
0
        //    *
        //	 * Builds the mesh into the given TriangleBuffer
        //	 * @param buffer The TriangleBuffer on where to append the mesh.
        //
        //
        //ORIGINAL LINE: void addToTriangleBuffer(TriangleBuffer& buffer) const
        public override void addToTriangleBuffer(ref TriangleBuffer buffer)
        {
            std_vector <Vector3> vertices = new  std_vector <Vector3>();
            int offset = 0;

            /// Step 1 : Generate icosahedron
            float phi     = 0.5f * (1.0f + Math.Sqrt(5.0f));
            float invnorm = 1f / Math.Sqrt(phi * phi + 1f);

            vertices.push_back(invnorm * new Vector3(-1f, phi, 0f));  //0
            vertices.push_back(invnorm * new Vector3(1f, phi, 0f));   //1
            vertices.push_back(invnorm * new Vector3(0f, 1f, -phi));  //2
            vertices.push_back(invnorm * new Vector3(0f, 1f, phi));   //3
            vertices.push_back(invnorm * new Vector3(-phi, 0f, -1f)); //4
            vertices.push_back(invnorm * new Vector3(-phi, 0f, 1f));  //5
            vertices.push_back(invnorm * new Vector3(phi, 0f, -1f));  //6
            vertices.push_back(invnorm * new Vector3(phi, 0f, 1f));   //7
            vertices.push_back(invnorm * new Vector3(0f, -1f, -phi)); //8
            vertices.push_back(invnorm * new Vector3(0f, -1f, phi));  //9
            vertices.push_back(invnorm * new Vector3(-1f, -phi, 0f)); //10
            vertices.push_back(invnorm * new Vector3(1f, -phi, 0f));  //11

            int[] firstFaces = { 0,   1,  2,
                                 0,   3,  1,
                                 0,   4,  5,
                                 1,   7,  6,
                                 1,   6,  2,
                                 1,   3,  7,
                                 0,   2,  4,
                                 0,   5,  3,
                                 2,   6,  8,
                                 2,   8,  4,
                                 3,   5,  9,
                                 3,   9,  7,
                                 11,  6,  7,
                                 10,  5,  4,
                                 10,  4,  8,
                                 10,  9,  5,
                                 11,  8,  6,
                                 11,  7,  9,
                                 10,  8, 11,
                                 10, 11, 9 };

            //C++ TO C# CONVERTER WARNING: This 'sizeof' ratio was replaced with a direct reference to the array length:
            //ORIGINAL LINE: std::vector<int> faces(firstFaces, firstFaces + sizeof(firstFaces)/sizeof(*firstFaces));
            // 定义一个容纳100个int型数据的容器,初值赋为0
            //vector<int> vecMyHouse(100,0);
            std_vector <int> faces = new  std_vector <int>(firstFaces);//(firstFaces, firstFaces + firstFaces.Length);

            int size = 60;

            /// Step 2 : tessellate
            for (ushort iteration = 0; iteration < mNumIterations; iteration++)
            {
                size *= 4;
                std_vector <int> newFaces = new std_vector <int>();
                newFaces.Clear();
                //newFaces.resize(size);
                for (int i = 0; i < size / 12; i++)
                {
                    int     i1  = faces[i * 3];
                    int     i2  = faces[i * 3 + 1];
                    int     i3  = faces[i * 3 + 2];
                    int     i12 = vertices.Count;
                    int     i23 = i12 + 1;
                    int     i13 = i12 + 2;
                    Vector3 v1  = vertices[i1];
                    Vector3 v2  = vertices[i2];
                    Vector3 v3  = vertices[i3];
                    //make 1 vertice at the center of each edge and project it onto the sphere
                    vertices.push_back((v1 + v2).NormalisedCopy);
                    vertices.push_back((v2 + v3).NormalisedCopy);
                    vertices.push_back((v1 + v3).NormalisedCopy);
                    //now recreate indices
                    newFaces.push_back(i1);
                    newFaces.push_back(i12);
                    newFaces.push_back(i13);
                    newFaces.push_back(i2);
                    newFaces.push_back(i23);
                    newFaces.push_back(i12);
                    newFaces.push_back(i3);
                    newFaces.push_back(i13);
                    newFaces.push_back(i23);
                    newFaces.push_back(i12);
                    newFaces.push_back(i23);
                    newFaces.push_back(i13);
                }
                //faces.swap(newFaces);
                faces = newFaces;
            }

            /// Step 3 : generate texcoords
            std_vector <Vector2> texCoords = new std_vector <Vector2>();

            for (ushort i = 0; i < vertices.size(); i++)
            {
                Vector3 vec   = vertices[i];
                float   u     = 0f;
                float   v     = 0f;
                float   r0    = sqrtf(vec.x * vec.x + vec.z * vec.z);
                float   alpha = 0f;
                alpha = atan2f(vec.z, vec.x);
                u     = alpha / Math.TWO_PI + .5f;
                v     = atan2f(vec.y, r0) / Math.PI + .5f;
                texCoords.push_back(new Vector2(u, v));
            }

            /// Step 4 : fix texcoords
            // find vertices to split
            std_vector <int> indexToSplit = new  std_vector <int>();

            for (int i = 0; i < faces.size() / 3; i++)
            {
                Vector2 t0 = texCoords[faces[i * 3 + 0]];
                Vector2 t1 = texCoords[faces[i * 3 + 1]];
                Vector2 t2 = texCoords[faces[i * 3 + 2]];
                if (Math.Abs(t2.x - t0.x) > 0.5)
                {
                    if (t0.x < 0.5)
                    {
                        indexToSplit.push_back(faces[i * 3]);
                    }
                    else
                    {
                        indexToSplit.push_back(faces[i * 3 + 2]);
                    }
                }
                if (Math.Abs(t1.x - t0.x) > 0.5)
                {
                    if (t0.x < 0.5)
                    {
                        indexToSplit.push_back(faces[i * 3]);
                    }
                    else
                    {
                        indexToSplit.push_back(faces[i * 3 + 1]);
                    }
                }
                if (Math.Abs(t2.x - t1.x) > 0.5)
                {
                    if (t1.x < 0.5)
                    {
                        indexToSplit.push_back(faces[i * 3 + 1]);
                    }
                    else
                    {
                        indexToSplit.push_back(faces[i * 3 + 2]);
                    }
                }
            }

            //split vertices
            for (ushort i = 0; i < indexToSplit.size(); i++)
            {
                int index = indexToSplit[i];
                //duplicate vertex
                Vector3 v = vertices[index];
                Vector2 t = texCoords[index] + Vector2.UNIT_X;
                vertices.push_back(v);
                texCoords.push_back(t);
                int newIndex = vertices.size() - 1;
                //reassign indices
                for (ushort j = 0; j < faces.size(); j++)
                {
                    if (faces[j] == index)
                    {
                        int index1 = faces[(j + 1) % 3 + (j / 3) * 3];
                        int index2 = faces[(j + 2) % 3 + (j / 3) * 3];
                        if ((texCoords[index1].x > 0.5f) || (texCoords[index2].x > 0.5f))
                        {
                            faces[j] = newIndex;
                        }
                    }
                }
            }

            /// Step 5 : realize
            buffer.rebaseOffset();
            buffer.estimateVertexCount((uint)vertices.size());
            buffer.estimateIndexCount((uint)size);

            for (ushort i = 0; i < vertices.size(); i++)
            {
                addPoint(ref buffer, mRadius * vertices[i], vertices[i], new Vector2(texCoords[i].x, texCoords[i].y));
            }
            for (ushort i = 0; i < size; i++)
            {
                buffer.index(offset + faces[i]);
            }
            offset += vertices.size();
        }
예제 #3
0
 protected float sqrtf(float p)
 {
     return(Math.Sqrt(p));
 }
예제 #4
0
 private float sqrt(float p)
 {
     return(Math.Sqrt(p));//throw new NotImplementedException();
 }