예제 #1
0
        public void Render(RenderTarget rt)
        {
            var  _sprite          = Sprites.GetSpriteHandle(((SpriteRendererDef)Def).Sprite);
            var  pos              = new Vector2f(RendererPosition.X, RendererPosition.Y);
            bool inverseToTheLeft = _lastRenderPosition.X > pos.X;
            bool moved            = _lastRenderPosition != pos;

            if (moved)
            {
                _toTheLeft = inverseToTheLeft;
            }
            _lastRenderPosition = pos;
            pos.Y = -pos.Y;
            if (_toTheLeft)
            {
                _sprite.Scale = new Vector2f(-1, 1);
            }
            else
            {
                _sprite.Scale = new Vector2f(1, 1);
            }

            HierarchyTransform v = new HierarchyTransform(RendererPosition, 0, null);

            v.DrawSpriteAt(_sprite,
                           Vec2.New(_sprite.TextureRect.X / 2, _sprite.TextureRect.Y / 2),
                           Vec2.New(0.5f, 0.5f));
        }
예제 #2
0
        public override void OnCreate()
        {
            var hT = new HierarchyTransform(Position, Rotation, null);

            foreach (var seRef in SceneDef.Entities)
            {
                Locators.Add(new LocToId()
                {
                    Id = CurrentServer.AcquireId(), Loc = seRef.Def
                });
            }
            foreach (var seRef in Locators)
            {
                var spawnedEntitySceneDef = seRef.Loc;
                var spawnedEntityDef      = EntityObjectsMap.GetDefFromSceneDef(spawnedEntitySceneDef);
                var scenePos   = (Vec2)spawnedEntitySceneDef.GetType().GetProperty(nameof(IPositionedEntity.Position)).GetValue(spawnedEntitySceneDef);
                var sceneRot   = (float)spawnedEntitySceneDef.GetType().GetProperty(nameof(IPositionedEntity.Rotation)).GetValue(spawnedEntitySceneDef);
                var objItemDef = spawnedEntitySceneDef.GetType().GetProperty(nameof(WorldItemEntity.StartingItemDef));
                var itemDef    = objItemDef == null ? null : ((DefRef <ItemDef>)objItemDef.GetValue(spawnedEntitySceneDef)).Def;
                var lT         = new HierarchyTransform(scenePos, sceneRot, hT);
                CurrentServer.Create(seRef.Id, EntityObjectsMap.GetTypeFromDef(spawnedEntityDef), e =>
                {
                    ((IEntityObject)e).Def          = (IEntityObjectDef)(((ISceneDef)spawnedEntitySceneDef).Object.Def);
                    ((IPositionedEntity)e).Position = lT.GlobalPos;
                    ((IPositionedEntity)e).Rotation = lT.GlobalRot;
                    if (e is WorldItemEntity wie)
                    {
                        wie.StartingItemDef = itemDef;
                    }
                });
            }
        }
예제 #3
0
        private void ProcessConnections(MapSiteInstance site, MapSiteInstance subSite, HierarchyTransform t)
        {
            if (Process())
            {
                if (subSite.Def.Connections.Count != subSite.Connections.Count)
                {
                    //has free connections
                    for (int i = 0; i < subSite.Def.Connections.Count; i++)
                    {
                        if (subSite.Connections.ContainsKey(i))
                        {
                            continue;
                        }
                        if (TryAttach())
                        {
                            foreach (var siteDef in _def.Palette.Shuffle())
                            {
                                if (subSite.Def.Connections[i].CanAttach(site.World, siteDef.Def, t))
                                {
                                    if (DoAttach())
                                    {
                                        var con = subSite.Def.Connections[i];
                                        //var vec = t.GetWorldPosInSpaceOf(con.Pos);
                                        var instance = subSite.Connections[i] = new MapSiteInstance(siteDef, con.Pos, con.Rot, true);

                                        Sites.Add(instance);
                                        var localT       = new HierarchyTransform(instance.Pos, instance.Rot, t);
                                        var attachmentrT = new HierarchyTransform(default, instance.Def.AttachmentRotation, localT);
예제 #4
0
        private void ProcessSite(MapSiteInstance site, HierarchyTransform t)
        {
            if (Process())
            {
                if (site.Def.SubSites.Count != site.SubSites.Count)
                {
                    for (int i = 0; i < site.Def.SubSites.Count; i++)
                    {
                        //has free subSites
                        if (TryAttach())
                        {
                            foreach (var siteDef in _def.Palette.Shuffle())
                            {
                                if (site.Def.SubSites[i].CanPlace(site.World, siteDef.Def, t))
                                {
                                    if (DoAttach())
                                    {
                                        var instance = site.SubSites[i] = new MapSiteInstance(siteDef, site.Def.SubSites[i].Pos, site.Def.SubSites[i].Rot, false);

                                        //var localT = new HierarchyTransform(site.Pos, site.Rot, t);
                                        var subT = new HierarchyTransform(instance.Pos, instance.Rot, t);
                                        instance.GlobalPos = subT.GlobalPos;
                                        instance.GlobalRot = subT.GlobalRot;
                                        Sites.Add(instance);
                                        var box = new OverlapBox(instance.GlobalPos, Vec2.New(instance.Def.SizeX, instance.Def.SizeY), instance.GlobalRot, instance.AttachedToBottom);
                                        site.World.Add(box);
                                        _sitesCount++;

                                        if (_sitesCount >= _def.SitesCount)
                                        {
                                            return;
                                        }
                                        break;
                                    }
                                }
                            }
                        }
                    }
                }
            }
            foreach (var subSite in site.SubSites)
            {
                var localT = new HierarchyTransform(subSite.Value.Pos, subSite.Value.Rot, t);
                if (Process())
                {
                    ProcessSite(subSite.Value, localT);
                }
                if (Process())
                {
                    ProcessConnections(site, subSite.Value, localT);
                }
            }
        }
예제 #5
0
        public void Init()
        {
            var world = (Volatile.VoltWorld)CurrentServer.CustomData;

            lock (world)
            {
                var pos = PhysicalPos;
                List <VoltShape> shapes = new List <VoltShape>();
                var def = (PhysicalBodyEngineDef)Def;
                for (int i = 0; i < def.Shapes.Count; i++)
                {
                    var shape = def.Shapes[i].Def;
                    if (!shape.HasBody)
                    {
                        continue;
                    }
                    if (shape is BoxPhysicalShapeDef bshape)
                    {
                        var hSize = new Vec2()
                        {
                            X = bshape.SizeX / 2, Y = bshape.SizeY / 2
                        };
                        var offset = new Vector2(shape.Offset.X, shape.Offset.Y);
                        var st     = new HierarchyTransform(Vec2.New(0, 0), bshape.Rotation, null);
                        shapes.Add(
                            world.CreatePolygonBodySpace(
                                new[] {
                            (Vector2)st.GetWorldPosInSpaceOf(new Vector2(-hSize.X, -hSize.Y)) + offset,
                            (Vector2)st.GetWorldPosInSpaceOf(new Vector2(-hSize.X, hSize.Y)) + offset,
                            (Vector2)st.GetWorldPosInSpaceOf(new Vector2(hSize.X, hSize.Y)) + offset,
                            (Vector2)st.GetWorldPosInSpaceOf(new Vector2(hSize.X, -hSize.Y)) + offset
                        }));
                    }
                }
                VoltBody body;
                var      radFromAngles = Rotation / 180 * Mathf.PI;
                if (def.IsStatic)
                {
                    body = world.CreateStaticBody(new Vector2(pos.X, pos.Y), radFromAngles, shapes.ToArray());
                }
                else
                {
                    body = world.CreateDynamicBody(new Vector2(pos.X, pos.Y), radFromAngles, shapes.ToArray());
                }
                body.UserData = Id;
                VoltBody      = body;
            }
        }
예제 #6
0
        private void DrawSite(MapSiteInstance site)
        {
            var ownTransform = new HierarchyTransform(site.GlobalPos, site.GlobalRot, null);

            ownTransform.DrawAsDir(0.3f);
            var _rectShape = new RectShapeHandle();

            _rectShape.Size             = new Vector2f(site.Def.SizeX, site.Def.SizeY);
            _rectShape.FillColor        = Color.Transparent;
            _rectShape.OutlineThickness = 1;
            _rectShape.OutlineColor     = Color.Red;
            ownTransform.DrawShapeAt(_rectShape, Vec2.New(0.5f, 0.5f));
            foreach (var subSite in site.SubSites)
            {
                //siteT.Rotate(subSite.Value.Rot);
                DrawSite(subSite.Value);
            }
            foreach (var con in site.Connections)
            {
                //siteT.Rotate(con.Value.Rot);
                DrawSite(con.Value);
            }
            foreach (var shape in site.World._shapes)
            {
                if (shape is OverlapBox box)
                {
                    _rectShape.OutlineColor = Color.Green;
                    _rectShape.Size         = new Vector2f(box.Size.X, box.Size.Y);
                    var t = new HierarchyTransform(Vec2.New(box.Pos.X, box.Pos.Y), box.RotAngles, null);
                    t.DrawShapeAt(_rectShape, Vec2.New(0.5f, 0.5f));
                }
            }
            foreach (var shape in site.World._failedShapes)
            {
                if (shape is OverlapBox box)
                {
                    _rectShape.OutlineColor = Color.Cyan;
                    _rectShape.Size         = new Vector2f(box.Size.X, box.Size.Y);
                    var t = new HierarchyTransform(Vec2.New(box.Pos.X, box.Pos.Y), box.RotAngles, null);
                    t.DrawShapeAt(_rectShape, Vec2.New(0.5f, 0.5f));
                }
            }
        }
예제 #7
0
        public void Update(bool onlyDrawGUI)
        {
            if (onlyDrawGUI && character != null)
            {
                CharGUI.Render(_node, _win, character as CharacterEntity, _charView);
                return;
            }
            if (!_connected.IsCompleted)
            {
                _node.Tick().Wait();
                return;
            }
            else if (!_connected.Result)
            {
                _node.Tick().Wait();
                return;
            }
            if (!joined)
            {
                var session = _node.AllGhosts().SingleOrDefault(x => x is SessionEntity);
                if (session != null)
                {
                    ((SessionEntity)session).Join("Name" + (new System.Random()).Next().ToString());
                    joined = true;
                }
            }
            if (_win != null)
            {
                _win.DispatchEvents();
                _win.Clear(Color.Blue);
            }
            var deltaTime = GetDeltaTime();

            foreach (var ghost in _node.AllGhosts())
            {
                if (ghost is ICharacterLikeMovement charLikeMovement)
                {
                    if (charLikeMovement.PhysicsBody == null)
                    {
                        var pos = ((IPositionedEntity)ghost).Position;
                        lock (_physicsWorld)
                        {
                            var body = _physicsWorld.CreateDynamicBody(new Vector2(pos.X, pos.Y), 1, _physicsWorld.CreateCircleWorldSpace(new Vector2(pos.X, pos.Y), 1f, 1));
                            body.UserData = ghost.Id;
                            charLikeMovement.PhysicsBody = body;
                        }
                    }
                    else
                    {
                        var pos = ((IPositionedEntity)ghost).Position;

                        var _debugPhysicsShape = new RectShapeHandle();
                        var aabb             = charLikeMovement.PhysicsBody.AABB;
                        HierarchyTransform v = new HierarchyTransform(Vec2.New(aabb.Center.x, aabb.Center.y), 0, null);
                        _debugPhysicsShape.FillColor        = Color.Transparent;
                        _debugPhysicsShape.OutlineColor     = Color.Red;
                        _debugPhysicsShape.OutlineThickness = 1;
                        _debugPhysicsShape.Size             = new SFML.System.Vector2f(aabb.Extent.x * 2, aabb.Extent.y * 2);
                        v.DrawShapeAt(_debugPhysicsShape, Vec2.New(0.5f, 0.5f));
                    }
                    if (ghost.HasAuthority)
                    {
                        character = ghost;
                        if (_win != null ? _win.HasFocus() : true)
                        {
                            charLikeMovement.UpdateControls();
                        }
                        charLikeMovement.UpdateMovement();
                        _charView.Center = new Vector2f(charLikeMovement.SmoothPosition.X, charLikeMovement.SmoothPosition.Y);
                        if (_win != null)
                        {
                            _charView.Size = new Vector2f(256, -256 * ((float)_win.Size.Y / (float)_win.Size.X));
                            _win.SetView(_charView);
                        }
                    }
                    else
                    {
                        charLikeMovement.InterpolationUpdate(deltaTime);
                    }
                }
            }
            DrawSite(SimpleServer._debugCreator._rootInstance);
            foreach (var ghost in _node.AllGhosts())
            {
                if (ghost is IRenderable rnd)
                {
                    rnd.Render(_win);
                }
            }
            CharGUI.Render(_node, _win, character as CharacterEntity, _charView);
            var tick = _node.Tick();

            lock (_physicsWorld)
            {
                _physicsWorld.DeltaTime = EnvironmentAPI.Time.DeltaTime;
                _physicsWorld.Update();
            }
            lock (_physicsWorld)
            {
                foreach (var body in _physicsWorld.Bodies)
                {
                    var aabb             = body.AABB;
                    var _shape           = new RectShapeHandle();
                    HierarchyTransform v = new HierarchyTransform(Vec2.New(aabb.Center.x, aabb.Center.y), body.Angle / Mathf.PI * 180, null);

                    _shape.FillColor        = Color.Transparent;
                    _shape.OutlineColor     = Color.Red;
                    _shape.OutlineThickness = 1;
                    _shape.Size             = new SFML.System.Vector2f(aabb.Extent.x * 2, aabb.Extent.y * 2);
                    foreach (var shape in body.shapes)
                    {
                        if (shape is VoltPolygon vp)
                        {
                            HierarchyTransform vpt = new HierarchyTransform(Vec2.New(vp.bodySpaceAABB.Center.x, vp.bodySpaceAABB.Center.y), 0, v);
                            _shape.FillColor        = Color.Transparent;
                            _shape.OutlineColor     = Color.Yellow;
                            _shape.OutlineThickness = 1;
                            _shape.Size             = new SFML.System.Vector2f(vp.bodySpaceAABB.Extent.x * 2, vp.bodySpaceAABB.Extent.y * 2);
                            vpt.DrawShapeAt(_shape, Vec2.New(0.5f, 0.5f));
                        }
                    }
                }
            }
            if (_win != null)
            {
                _win.Display();
            }
            tick.Wait();
        }