Beispiel #1
0
        private void CreateRenderTextureTexture(int id, CameraData cameraData, Camera camera, UnityEngine.UI.RawImage image)//, int textureWidth, int textureHeight)
        {
            // Debug.LogError("NewRender Size: " + image.rectTransform.rect.size.ToString());
            int textureWidth  = (int)(image.rectTransform.rect.size.x) / cameraData.textureDivision;
            int textureHeight = (int)(image.rectTransform.rect.size.y) / cameraData.textureDivision;

            if (cameraRenders.ContainsKey(id))
            {
                GameObject.Destroy(cameraRenders[id]);
                cameraRenders.Remove(id);
            }
            RenderTexture newTexture = new RenderTexture(textureWidth, textureHeight, 16, UnityEngine.RenderTextureFormat.ARGB32);

            newTexture.filterMode = FilterMode.Point;
            newTexture.name       = camera.gameObject.name;
            image.texture         = newTexture;
            camera.targetTexture  = newTexture;
            cameraRenders.Add(id, newTexture);
            if (cameras.ContainsKey(id) == false)
            {
                Debug.LogError("Camera does not exist: " + id);
                return;
            }

            if (World.EntityManager.HasComponent <CameraLink>(cameras[id]))
            {
                CameraLink cameraLink = World.EntityManager.GetComponentData <CameraLink>(cameras[id]);
                cameraLink.aspectRatio = ((float)textureWidth) / ((float)textureHeight);
                World.EntityManager.SetComponentData(cameras[id], cameraLink);
            }
            else
            {
                int characterID = World.EntityManager.GetComponentData <ZoxID>(cameras[id]).creatorID;
                if (characterSpawnSystem.characters.ContainsKey(characterID))
                {
                    if (World.EntityManager.HasComponent <CameraLink>(characterSpawnSystem.characters[characterID]))
                    {
                        CameraLink cameraLink = World.EntityManager.GetComponentData <CameraLink>(characterSpawnSystem.characters[characterID]);
                        cameraLink.aspectRatio = ((float)textureWidth) / ((float)textureHeight);
                        World.EntityManager.SetComponentData(characterSpawnSystem.characters[characterID], cameraLink);
                    }
                }
            }
            // connected UIs
            CharacterUIList uiList = World.EntityManager.GetComponentData <CharacterUIList>(cameras[id]);

            Entity[] uis = uiList.uis.ToArray();
            for (int i = 0; i < uis.Length; i++)
            {
                if (World.EntityManager.Exists(uis[i]))
                {
                    CameraLink cameraLink = World.EntityManager.GetComponentData <CameraLink>(uis[i]);
                    cameraLink.aspectRatio = ((float)textureWidth) / ((float)textureHeight);
                    World.EntityManager.SetComponentData(uis[i], cameraLink);
                    //World.EntityManager.SetComponentData(uis[i], orbitor);
                    OrbitCamera orbitor = World.EntityManager.GetComponentData <OrbitCamera>(uis[i]);
                    UIUtilities.UpdateOrbiter(World.EntityManager, uis[i], orbitor.orbitPosition, orbitor.lerpSpeed);
                }
            }
        }
Beispiel #2
0
        protected override void OnUpdate()
        {
            Entities.WithAll <Controller, Targeter>().ForEach((Entity e, ref Controller controller, ref Targeter targeter) =>
            {
                // if target and new target
                // change material of character to selected
                // change last selected entity to deselected!
                if (controller.gameState != ((byte)GameState.InGame))
                {
                    return;
                }

                // seperate into another system later
                if (controller.mappingType == ((byte)ControllerMapping.Character))
                {
                    if (targeter.hasTarget == 1 && controller.Value.buttonA == 1)
                    {
                        // if clicked on target
                        //UnityEngine.Debug.LogError("Started talking with character: " + targeter.nearbyCharacter.character.Index);
                        // start dialogue here!
                        controller.mappingType = 1;
                        DialogueUISpawnSystem.SpawnUI(World.EntityManager, e);
                    }
                }
                else if (controller.mappingType == ((byte)ControllerMapping.Dialogue))
                {
                    if (controller.Value.buttonB == 1)
                    {
                        FinishedSpeaking(e, ref controller);
                    }
                    CameraLink clink     = World.EntityManager.GetComponentData <CameraLink>(e);
                    CharacterUIList list = World.EntityManager.GetComponentData <CharacterUIList>(clink.camera);
                    var uis = list.uis.ToArray();
                    //Debug.LogError("UIs: " + uis.Length);
                    for (int i = 0; i < uis.Length; i++)
                    {
                        if (World.EntityManager.HasComponent <DialogueUI>(uis[i]))
                        {
                            //Debug.LogError("UI is dialogue: " + i);
                            if (World.EntityManager.GetComponentData <DialogueUI>(uis[i]).completedTree == 1)
                            {
                                FinishedSpeaking(e, ref controller);
                            }
                            break;
                        }
                        //else {
                        //    Debug.LogError("UI isnt' dialogue: " + i);
                        //}
                    }
                }
            });
        }
Beispiel #3
0
 private void AddCaster(Entity e)
 {
     if (World.EntityManager.HasComponent <CharacterRaycaster>(e) == false)
     {
         if (World.EntityManager.HasComponent <CameraLink>(e))
         {
             CameraLink cameraLink = World.EntityManager.GetComponentData <CameraLink>(e);
             World.EntityManager.AddComponentData(e, new CharacterRaycaster {
                 camera = cameraLink.camera
             });
         }
         else
         {
             World.EntityManager.AddComponentData(e, new CharacterRaycaster {
             });
         }
     }
 }
Beispiel #4
0
        /* public void LoadGame()
         * {
         *   Entity character = Bootstrap.instance.GetPlayerMonster();
         *   LoadPlayer(character);
         * }*/

        /// <summary>
        /// Players can chose any  character in the save game folder - then it will be loaded from that
        /// </summary>
        public void LoadPlayer(Entity character)
        {
            ZoxID zoxID = EntityManager.GetComponentData <ZoxID>(character);

            LoadComponentData <Translation>(character, "Players", zoxID.id.ToString());
            LoadComponentData <Rotation>(character, "Players", zoxID.id.ToString());
            LoadComponentData <Inventory>(character, "Players", zoxID.id.ToString());
            LoadComponentData <Stats>(character, "Players", zoxID.id.ToString());
            LoadComponentData <Skills>(character, "Players", zoxID.id.ToString());
            LoadComponentData <Equipment>(character, "Players", zoxID.id.ToString());
            LoadComponentData <QuestLog>(character, "Players", zoxID.id.ToString());
            if (World.EntityManager.HasComponent <CameraLink>(character))
            {
                CameraLink cameraLink = EntityManager.GetComponentData <CameraLink>(character);
                Entity     camera     = cameraLink.camera;// CameraSystem.cameras[cameraLink.cameraID];
                LoadComponentData <FirstPersonCamera>(camera, "Players", zoxID.id.ToString());
                FirstPersonCamera firstPersonCamera = EntityManager.GetComponentData <FirstPersonCamera>(camera);
                firstPersonCamera.enabled = 1;
                EntityManager.SetComponentData(camera, firstPersonCamera);
            }
        }
Beispiel #5
0
        public static void UpdateOrbiter(EntityManager EntityManager, Entity panelUI, float3 orbitPosition, float orbitLerpSpeed)
        {
            CameraLink  cameraLink = EntityManager.GetComponentData <CameraLink>(panelUI);
            OrbitCamera orbitor    = new OrbitCamera
            {
                orbitPosition = orbitPosition,
                lerpSpeed     = orbitLerpSpeed
            };
            var   frustumHeight = 2.0f * orbitor.orbitPosition.z * math.tan(math.radians(cameraLink.fov) * 0.5f);
            var   frustumWidth  = frustumHeight * cameraLink.aspectRatio;
            float positionX     = orbitor.orbitPosition.x * frustumWidth;
            float positionY     = -orbitor.orbitPosition.y * frustumHeight;

            orbitor.position = new float3(positionX, positionY, orbitor.orbitPosition.z);
            EntityManager.SetComponentData(panelUI, orbitor);
            if (EntityManager.Exists(cameraLink.camera))
            {
                float3     cameraPosition = EntityManager.GetComponentData <Translation>(cameraLink.camera).Value;
                quaternion cameraRotation = EntityManager.GetComponentData <Rotation>(cameraLink.camera).Value;
                EntityManager.SetComponentData(panelUI, new Translation {
                    Value = orbitor.GetTranslatedPosition(cameraPosition, cameraRotation)
                });
            }
        }
Beispiel #6
0
        private void SavePlayer(Entity character)
        {
            ZoxID  zoxID        = EntityManager.GetComponentData <ZoxID>(character);
            string saveGamePath = GetSavePath() + saveGameName + "/";
            string playerPath   = saveGamePath + "Players/" + zoxID.id.ToString();

            if (!System.IO.Directory.Exists(playerPath))
            {
                System.IO.Directory.CreateDirectory(playerPath);
            }
            SaveComponentData <Translation>(character, "Players", zoxID.id.ToString());
            SaveComponentData <Rotation>(character, "Players", zoxID.id.ToString());
            SaveComponentData <Inventory>(character, "Players", zoxID.id.ToString());
            SaveComponentData <Stats>(character, "Players", zoxID.id.ToString());
            SaveComponentData <Skills>(character, "Players", zoxID.id.ToString());
            SaveComponentData <Equipment>(character, "Players", zoxID.id.ToString());
            SaveComponentData <QuestLog>(character, "Players", zoxID.id.ToString());
            if (World.EntityManager.HasComponent <CameraLink>(character))
            {
                CameraLink cameraLink = EntityManager.GetComponentData <CameraLink>(character);
                Entity     camera     = cameraLink.camera;// CameraSystem.cameras[cameraLink.cameraID];
                SaveComponentData <FirstPersonCamera>(camera, "Players", zoxID.id.ToString());
            }
        }
Beispiel #7
0
        public static Entity SpawnPanel(EntityManager EntityManager, Entity character, Material baseMaterial, Material outlineMaterial, float2 panelSize)
        {
            //float3 orbitPosition = float3.zero;
            if (panelArchtype.Valid == false)
            {
                panelArchtype = EntityManager.CreateArchetype(
                    typeof(PanelUI),
                    typeof(CameraLink),
                    typeof(OrbitCamera),
                    typeof(RenderBounds),
                    // transform
                    typeof(Translation),
                    typeof(Rotation),
                    typeof(NonUniformScale),
                    typeof(LocalToWorld),
                    // renderer
                    typeof(RenderMesh));
            }
            //float3 spawnPosition = EntityManager.GetComponentData<Translation>(character).Value;
            Material materialInstance = new Material(baseMaterial);
            //materialInstance.SetFloat("_QueueOffset", 100);
            Mesh   panelMesh   = MeshUtilities.CreateQuadMesh(panelSize);
            Entity characterUI = EntityManager.CreateEntity(panelArchtype);
            // fix bounds flickers mesh
            RenderBounds b = new RenderBounds {
                Value = new AABB {
                    Extents = new float3(panelSize.x, panelSize.y, 0.5f)
                }
            };

            EntityManager.SetComponentData(characterUI, b);
            EntityManager.SetComponentData(characterUI, new Rotation {
                Value = quaternion.identity
            });
            EntityManager.SetComponentData(characterUI, new NonUniformScale {
                Value = new float3(1, 1, 1)
            });
            EntityManager.SetSharedComponentData(characterUI, new RenderMesh {
                material       = materialInstance,
                mesh           = panelMesh,
                receiveShadows = true
            });
            if (EntityManager.HasComponent <CameraLink>(character))
            {
                CameraLink cameraLink = EntityManager.GetComponentData <CameraLink>(character);
                EntityManager.SetComponentData(characterUI, cameraLink);
                if (EntityManager.HasComponent <CharacterUIList>(cameraLink.camera))
                {
                    // update UI List
                    CharacterUIList uiList = EntityManager.GetComponentData <CharacterUIList>(cameraLink.camera);
                    // expand list
                    Entity[]      oldUIs  = uiList.uis.ToArray();
                    List <Entity> uisList = new List <Entity>();
                    for (int i = 0; i < oldUIs.Length; i++)
                    {
                        if (EntityManager.Exists(oldUIs[i]))
                        {
                            uisList.Add(oldUIs[i]);
                        }
                    }
                    uisList.Add(characterUI);
                    uiList.uis = new BlitableArray <Entity>(uisList.Count, Unity.Collections.Allocator.Persistent);
                    for (int i = 0; i < uisList.Count; i++)
                    {
                        uiList.uis[i] = uisList[i];
                    }
                    // uiList.uis[uis.Length] = characterUI;
                    EntityManager.SetComponentData(cameraLink.camera, uiList);
                }
            }
            else
            {
                Debug.LogError("No Camera link assigned on character..");
            }
            if (outlineMaterial)
            {
                // Debug.LogError("Spawning Outline Renderer.");
                var outline = SpawnVisualElement(EntityManager, characterUI, float3.zero, float2.zero, null, outlineMaterial);
                EntityManager.AddComponentData(characterUI, new OutlineLink {
                    outline = outline
                });
            }
            return(characterUI);
        }
Beispiel #8
0
        private Entity SpawnCamera(int id, float3 spawnPosition, quaternion spawnRotation, Entity attachEntity, CameraData cameraData)
        {
            if (cameraData.isRenderTexture == 0)
            {
                if (Camera.main)
                {
                    //Camera.main.gameObject.tag = "Untagged";
                    Camera.main.gameObject.SetActive(false);
                    if (canvas)
                    {
                        canvas.SetActive(false);
                    }
                }
            }
            else
            {
                if (canvas)
                {
                    canvas.SetActive(true);
                }
            }
            //AudioManager.instance.SpawnMonsterSpawnSound(spawnPosition);
            cameraDatas.Add(id, cameraData);
            GameObject cameraObject = SpawnCameraGameObject(id, cameraData, spawnPosition, spawnRotation);

            cameraObjects.Add(id, cameraObject);
            var    cameraEntity = AddECSComponents(cameraObject, id, attachEntity, cameraData);
            Camera camera       = cameraObject.GetComponent <Camera>();

            if (World.EntityManager.Exists(attachEntity))
            {
                //Entity charaterEntity = characterSpawnSystem.characters[followID];
                if (cameraData.cameraType == ((byte)CameraDataType.ThirdPerson))
                {
                    Translation characterTranslation = World.EntityManager.GetComponentData <Translation>(attachEntity);
                    Rotation    characterRotation    = World.EntityManager.GetComponentData <Rotation>(attachEntity);
                    spawnPosition  = characterTranslation.Value + new float3(0, cameraData.followCameraData.cameraAddition.y, 0);
                    spawnPosition += math.rotate(characterRotation.Value, new float3(0, 0, cameraData.followCameraData.cameraAddition.z));
                    Quaternion newRotation = new Quaternion(characterRotation.Value.value.x,
                                                            characterRotation.Value.value.y, characterRotation.Value.value.z, characterRotation.Value.value.w);
                    float3 newRotation2 = newRotation.eulerAngles;
                    newRotation2.x = cameraData.followCameraData.cameraRotation.x;
                    spawnRotation  = Quaternion.Euler(newRotation2);
                }
                if (World.EntityManager.HasComponent <CameraLink>(attachEntity))
                {
                    CameraLink cameraLink = World.EntityManager.GetComponentData <CameraLink>(attachEntity);
                    cameraLink.camera = cameraEntity;
                    cameraLink.fov    = camera.fieldOfView;
                    World.EntityManager.SetComponentData(attachEntity, cameraLink);
                }
            }
            else
            {
                World.EntityManager.AddComponentData(cameras[id], new CameraLink
                {
                    camera = cameraEntity,
                    fov    = camera.fieldOfView
                });
            }
            // Setup camera object
            if (cameraData.isRenderTexture == 1)
            {
                CreateRenderTexture(camera, cameraData, id);
            }
            else
            {
                Camera.SetupCurrent(camera);
                camera.tag = "MainCamera";
            }
            Debug.Log("Spawned camera: " + id);
            ResizeRenderTextures();
            return(cameraEntity);
        }
Beispiel #9
0
        public void ConnectCameraToCharacter(Entity camera, Entity attachEntity)
        {
            if (World.EntityManager.Exists(camera) == false)
            {
                Debug.LogError("Cannot connect camera to character as camera id is false.");
                return;
            }
            if (World.EntityManager.Exists(attachEntity) == false)
            {
                Debug.LogError("Cannot connect camera to character as character id is false.");
                return;
            }
            int zoxID = World.EntityManager.GetComponentData <ZoxID>(camera).id;
            //Debug.LogError("attaching camera " + cameraID + " to character " + characterID);
            CameraData cameraData = cameraDatas[zoxID];
            //Entity characterEntity = characterSpawnSystem.characters[characterID];
            //Entity cameraEntity = cameras[zoxID];
            Translation characterTranslation = World.EntityManager.GetComponentData <Translation>(attachEntity);

            //AudioManager.instance.SpawnMonsterSpawnSound(characterTranslation.Value);
            if (World.EntityManager.HasComponent <CameraLink>(attachEntity))
            {
                //Debug.LogError("Connecting Camera to character: " + characterEntity.Index + "::" + cameraID);
                CameraLink cameraLink = World.EntityManager.GetComponentData <CameraLink>(attachEntity);
                cameraLink.camera = camera;
                cameraLink.fov    = cameraObjects[zoxID].GetComponent <Camera>().fieldOfView; //camera.fieldOfView;
                World.EntityManager.SetComponentData(attachEntity, cameraLink);
            }
            if (World.EntityManager.HasComponent <CameraLink>(camera))
            {
                World.EntityManager.RemoveComponent <CameraLink>(camera);
            }
            int           characterID       = World.EntityManager.GetComponentData <ZoxID>(attachEntity).id;
            CharacterLink characterToCamera = new CharacterLink {
                character = attachEntity
            };

            if (World.EntityManager.HasComponent <CharacterLink>(camera))
            {
                World.EntityManager.SetComponentData(camera, characterToCamera);
            }
            else
            {
                World.EntityManager.AddComponentData(camera, characterToCamera);
            }
            if (cameraData.cameraType == 0)
            {
                //Debug.LogError("Setting camera position offset: " + cameraData.firstPersonCamera.cameraAddition);
                //Debug.LogError("Setting camera position offset2: " + World.EntityManager.GetComponentData<FirstPersonCamera>(cameras[cameraID]).Value.cameraAddition);
                World.EntityManager.SetComponentData(camera, new ZoxID {
                    id        = zoxID,
                    creatorID = characterID
                });
                World.EntityManager.SetComponentData(camera, new CameraSynch
                {
                    Value         = attachEntity,
                    localPosition = cameraData.firstPersonCamera.cameraAddition
                });
                // Debug.LogError("Setting camera FirstPersonCamera: " + cameraEntity.Index);
                // Debug.LogError("    Pre camera FirstPersonCamera enabled: " + World.EntityManager.GetComponentData<FirstPersonCamera>(cameraEntity).enabled);
                World.EntityManager.SetComponentData(camera, new FirstPersonCamera
                {
                    enabled = 1,
                    Value   = cameraData.firstPersonCamera
                });
                //Debug.LogError("    Post camera FirstPersonCamera enabled: " + World.EntityManager.GetComponentData<FirstPersonCamera>(cameraEntity).enabled);
            }
        }