示例#1
0
        /// <summary>
        /// Creates a new GameModel and its subcomponents.
        /// </summary>
        /// <returns>The created GameModel.</returns>
        public GameModel BuildModel()
        {
            // build game objects
            Planet planet = new Planet();
            planet.Radius = 300;

            // build planet in orbit
            int distance = 700;

            Planet planet2 = new Planet();
            planet2.Radius = 82;
            planet2.IsFlexible = true;
            planet2.Position = new Vector2(distance, 0);
            // calculate velocity needed to circuit in orbit
            float planet2Velocity = (float) Math.Sqrt(planet.Mass * GameAssets.G / distance / (GameAssets.N * 1000));
            planet2.Velocity = new Vector2(0, planet2Velocity);

            Spaceship spaceship1 = new Spaceship();
            Spaceship spaceship2 = new Spaceship();

            Player player1 = new HumanPlayer(1, this.playerHandler, Color.Green, this.configuration.GetKeyboardConfiguration(1));
            Player player2 = new HumanPlayer(2, this.playerHandler, Color.Orange, this.configuration.GetKeyboardConfiguration(2));
            player1.Spaceship = spaceship1;
            player2.Spaceship = spaceship2;

            player1.Spaceship.Position = new Vector2(-1900, 0);
            player2.Spaceship.Position = new Vector2(1900, 0);

            List<Player> players = new List<Player>();
            players.Add(player1);
            players.Add(player2);

            WorldObject[] worldObjects = {planet, planet2, spaceship1, spaceship2};

            // build ShortLifespanObjectFactory
            ShortLifespanObjectFactory shortLifespanObjectFactory = new SimpleShortLifespanObjectFactory();

            // build game model
            World world = new World(worldObjects);
            Physics physics = new SimplePhysicsAlgorithm(this.collisionHandler, world, configuration);
            GameModel gameModel = new GameModel(shortLifespanObjectFactory, physics, players, world);
            return gameModel;
        }
示例#2
0
        /// <summary>
        /// Builds a new game using the ruby script
        /// </summary>
        /// <returns>The new Game</returns>
        public GameModel BuildModel()
        {
            // build game objects
            LevelBuilder levelBuilder = new LevelBuilder(this.configuration, this.playerHandler);
            ScriptEngine scriptEngine = IronRuby.Ruby.CreateEngine();
            ScriptScope scriptScope = scriptEngine.CreateScope();
            scriptScope.SetVariable("level", levelBuilder);
            scriptScope.SetVariable("physic", new RubyPhysic(GameAssets.G, GameAssets.N));
            try
            {
                ScriptSource source = scriptEngine.CreateScriptSourceFromFile(this.path);
                source.Compile();
                source.Execute(scriptScope);
            }
            catch (Exception e)
            {
                string errorMessage = "[" + this.path + "]\nError: " + e.Message;
                System.Windows.Forms.MessageBox.Show(errorMessage);
            }

            // build ShortLifespanObjectFactory
            ShortLifespanObjectFactory shortLifespanObjectFactory = new SimpleShortLifespanObjectFactory();

            // build game model
            World world = new World(levelBuilder.WorldObjects);
            Physics physics = new SimplePhysicsAlgorithm(this.collisionHandler, world, configuration);
            GameModel gameModel = new GameModel(shortLifespanObjectFactory, physics, levelBuilder.Players, world);
            return gameModel;
        }