Пример #1
0
        public override void OnInitGeometry()
        {
            var rbCmp = Arena.Owner.RigidBodyCmp;

            float width      = ConvertUnits.ToSimUnits(Arena.ArenaHalfSize.X);
            float height     = ConvertUnits.ToSimUnits(Arena.ArenaHalfSize.Y);
            float goalWidth  = ConvertUnits.ToSimUnits(100);
            float goalHeight = ConvertUnits.ToSimUnits(80);

            rbCmp.Body.CollidesWith        = (Category)CollisionType.All | (Category)CollisionType.Ball;
            rbCmp.Body.CollisionCategories = (Category)CollisionType.Wall;

            m_mobileObjs = new GameObject[2];

            for (int i = 0; i < m_mobileObjs.Length; i++)
            {
                m_mobileObjs[i] = new GameObject("Arena Mobile Object");
                Sprite mobileObjAOSprite = Sprite.CreateFromTexture("Arenas/ArenaMobLarge/AOMob.png");
                mobileObjAOSprite.Scale = 0.5f * Vector2.One;
                ColorMaskedSprite mobileObjColorSprite = new ColorMaskedSprite(mobileObjAOSprite, "ArenaOverlay3");


                var texAsset = Engine.AssetManager.GetAsset <Texture2D>("Arenas/ArenaMobLarge/materialMob" + i + ".png");
                mobileObjColorSprite.Mask   = texAsset.Content;
                mobileObjColorSprite.Color1 = Arena.ColorScheme.Color2;
                mobileObjColorSprite.Color2 = Ball.Game.GameManager.Teams[i].ColorScheme.Color1.SetSaturation(0.5f); //mettre en public depuis Arena
                mobileObjColorSprite.Color3 = Ball.Game.GameManager.Teams[i].ColorScheme.Color1.SetSaturation(0.5f);

                m_mobileObjs[i].Attach(mobileObjColorSprite);

                Asset <Texture2D>   textureAsset        = Engine.AssetManager.GetAsset <Texture2D>("Arenas/ArenaMobLarge/collisionMob.png");
                CollisionDefinition collisionDefinition = CollisionDefinitionHelper.FromTexture(textureAsset.Content, 2.0f);
                for (int iCol = 0; iCol < collisionDefinition.Entries.Length; iCol++)
                {
                    Engine.Log.Write(String.Format("Collision {0}: {1} vertices", iCol, collisionDefinition.Entries[iCol].Vertices.Length));
                }

                RigidBodyComponentParameters parameters = new RigidBodyComponentParameters {
                    Collision = collisionDefinition
                };

                RigidBodyComponent rigidBodyCmp = new RigidBodyComponent(new Asset <RigidBodyComponentParameters>(parameters));
                m_mobileObjs[i].Attach(rigidBodyCmp);

                m_mobileObjs[i].RigidBodyCmp.Body.CollidesWith        = (Category)CollisionType.Player | (Category)CollisionType.Ball;
                m_mobileObjs[i].RigidBodyCmp.Body.CollisionCategories = (Category)CollisionType.Wall;
            }

            m_mobileObjs[0].Position    = new Vector2(-mobileObjsPosX, 0);
            m_mobileObjs[1].Position    = new Vector2(mobileObjsPosX, 0);
            m_mobileObjs[0].Orientation = 0;
            m_mobileObjs[1].Orientation = 0;

            Engine.World.EventManager.AddListener((int)EventId.FirstPeriod, OnFirstPeriod);
            Engine.World.EventManager.AddListener((int)EventId.HalfTime, OnHalfTime);
            Engine.World.EventManager.AddListener((int)EventId.HalfTimeTransition, OnHalfTimeTransition);
            Engine.World.EventManager.AddListener((int)EventId.SecondPeriod, OnSecondPeriod);
            Engine.World.EventManager.AddListener((int)EventId.MatchEnd, OnMatchEnd);

            m_rotateTimer         = new Timer(Engine.GameTime.Source, m_timeFirstMS, TimerBehaviour.Restart);
            m_rotateTimerEvent    = new TimerEvent(StartRotation);
            m_rotateTimer.OnTime += m_rotateTimerEvent;
        }
Пример #2
0
        public override AssetLoadResult <CollisionDefinition> Load(string path)
        {
            List <IAssetDependency> dependencies = new List <IAssetDependency>();

            if (!path.EndsWith(".png") && !path.EndsWith(".col"))
            {
                Engine.Log.Error("Can't load collision \"" + path + "\", unknow format");
            }

            String pngPath = Path.ChangeExtension(path, ".png");

            pngPath = Engine.AssetManager.AssetSource.GetFullPath(pngPath);
            String colPath = Path.ChangeExtension(path, ".png.col");

            colPath = Engine.AssetManager.AssetSource.GetFullPath(colPath);


            bool rebuildCol = false;

            if (Engine.AssetManager.AssetSource.Exists(pngPath))
            {
                DateTime lastSourceEditTime = DateTime.MinValue;
                var      colFileExist       = Engine.AssetManager.AssetSource.Exists(colPath);
                if (!colFileExist || File.GetLastWriteTimeUtc(colPath) < lastSourceEditTime)
                {
                    rebuildCol = true;
                }

                if (rebuildCol)
                {
                    Asset <Texture2D> textureAsset = Engine.AssetManager.GetAsset <Texture2D>(pngPath);
                    var colDef = CollisionDefinitionHelper.FromTexture(textureAsset.Content, 2.0f);

                    // Save the collision
                    using (var file = File.Create(colPath))
                        Serialize(file, colDef);
                }
            }

            CollisionDefinition instance = null;

            using (var stream = Engine.AssetManager.AssetSource.Open(colPath))
                instance = Deserialize(stream);

            dependencies.Add(Engine.AssetManager.AssetSource.CreateDependency(colPath));
            AssetLoadResult <CollisionDefinition> result = new AssetLoadResult <CollisionDefinition>();

            result.Instance     = instance;
            result.Dependencies = dependencies;

            return(result);


            ////Load collision from file
            //CollisionDefinition instance = null;
            //if (Engine.AssetManager.AssetSource.Exists(colPath))
            //{
            //    using (var stream = Engine.AssetManager.AssetSource.Open(colPath))
            //        instance = Deserialize(stream);

            //    dependencies.Add(Engine.AssetManager.AssetSource.CreateDependency(colPath));
            //}
            //else
            //{
            //    Asset<Texture2D> textureAsset = Engine.AssetManager.GetAsset<Texture2D>(pngPath);
            //    instance = CollisionDefinitionHelper.FromTexture(textureAsset.Content, 2.0f);

            //    dependencies.Add(textureAsset);
            //}

            //AssetLoadResult<CollisionDefinition> result = new AssetLoadResult<CollisionDefinition>();
            //result.Instance = instance;
            //result.Dependencies = dependencies;

            //return result;

            //CollisionTextureDefinition textureDefinition = null;
            //DateTime lastSourceEditTime = DateTime.MinValue;

            //if (path.EndsWith(".png"))
            //{
            //    textureDefinition = new CollisionTextureDefinition();
            //    textureDefinition.Texture = new AssetReference(path);
            //    dependencies.Add(Engine.AssetManager.GetFileDependency(path));

            //    lastSourceEditTime = File.GetLastWriteTimeUtc(path);
            //}
            //else if(path.Contains(".lua::"))
            //{
            //    var defAsset = Engine.AssetManager.GetAsset<CollisionTextureDefinition>(path);
            //    dependencies.Add(defAsset);

            //    textureDefinition = defAsset.Content;

            //    var textureEditTime = File.GetLastWriteTimeUtc(defAsset.Content.Texture.Path);
            //    var definitionEditTime = File.GetLastWriteTimeUtc(path);

            //    if(textureEditTime > definitionEditTime)
            //        lastSourceEditTime = textureEditTime;
            //    else
            //        lastSourceEditTime = definitionEditTime;
            //}
            //else
            //{
            //    Engine.Log.Error("Can't load collision \"" + path + "\", unknow format");
            //}

            //String buildedColPath = path + ".col";
            //if (!File.Exists(buildedColPath) || File.GetLastWriteTimeUtc(buildedColPath) < lastSourceEditTime)
            //{
            //    Engine.Log.Write("Building collisions from texture: " + textureDefinition.Texture.Path);
            //    Texture2D sourceTexture = Engine.AssetManager.Get<Texture2D>(textureDefinition.Texture.Path);
            //    CollisionDefinition colDef = CollisionDefinitionHelper.FromTexture(sourceTexture, textureDefinition.Tolerance, textureDefinition.Transform);

            //    //Save the collision
            //    using (var file = File.Create(buildedColPath))
            //        Serialize(file, colDef);
            //}

            ////Load collision from file
            //CollisionDefinition instance = null;
            //using (var file = File.OpenRead(buildedColPath))
            //    instance = Deserialize(file);

            //AssetLoadResult<CollisionDefinition> result = new AssetLoadResult<CollisionDefinition>();
            //result.Instance = instance;
            //result.Dependencies = dependencies;

            //return result;
        }