Beispiel #1
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();
        }
Beispiel #2
0
 public void Update()
 {
     try
     {
         if (firstTime)
         {
             var r = new Random(0);
             firstTime = false;
             foreach (var site in _debugCreator.Sites)
             {
                 if (site.Def.Type.Def != null && site.Def.Type.Def.EntitiesToSpawnOn.Count > 0)
                 {
                     var randomEntity = site.Def.Type.Def.EntitiesToSpawnOn[r.Next(site.Def.Type.Def.EntitiesToSpawnOn.Count)].Def;
                     var objId        = _node.Create(EntityObjectsMap.GetTypeFromDef(randomEntity), e =>
                     {
                         ((IEntityObject)e).Def          = randomEntity;
                         ((IPositionedEntity)e).Position = site.GlobalPos;
                         ((IPositionedEntity)e).Rotation = site.GlobalRot;
                     });
                     if (site.Def.AttachedScene != null)
                     {
                         _node.Create <SceneEntity>(se =>
                         {
                             se.Position = site.GlobalPos;
                             se.Rotation = site.GlobalRot;
                             se.SceneDef = site.Def.AttachedScene;
                         });
                     }
                 }
             }
             if (_debugCreator._rootInstance.Def.AttachedScene != null)
             {
                 _node.Create <SceneEntity>(se =>
                 {
                     se.Position = _debugCreator._rootInstance.GlobalPos;
                     se.Rotation = _debugCreator._rootInstance.GlobalRot;
                     se.SceneDef = _debugCreator._rootInstance.Def.AttachedScene;
                 });
             }
         }
         var deltaTime = EnvironmentAPI.Time.DeltaTime;
         foreach (var ghost in _node.AllGhosts())
         {
             if (ghost is ICharacterLikeMovement charLikeMovement)
             {
                 if (charLikeMovement.PhysicsBody == null)
                 {
                     var pos = ((IPositionedEntity)ghost).Position;
                     lock (_physicsWorld)
                     {
                         var circleShape = _physicsWorld.CreateCircleWorldSpace(new Vector2(pos.X, pos.Y), 1f, 1);
                         var body        = _physicsWorld.CreateDynamicBody(new Vector2(pos.X, pos.Y), 1, circleShape);
                         body.UserData = ghost.Id;
                         charLikeMovement.PhysicsBody = body;
                     }
                 }
                 charLikeMovement.InterpolationUpdate(deltaTime);
             }
         }
         var tick = _node.Tick();
         lock (_physicsWorld)
         {
             _physicsWorld.DeltaTime = EnvironmentAPI.Time.DeltaTime;
             _physicsWorld.Update();
         }
         tick.Wait();
     }
     catch (Exception e)
     {
         Logger.LogError?.Invoke(e.ToString());
     }
 }