Ejemplo n.º 1
0
        public void createEntityFromProto()
        {
            if (level.vars.protoEntity.myEntType == typeof(Player) && level.getPlayer() != null)
            {
                Console.WriteLine("Trying to add a second player. That's a bad idea.");
                return;
            }

            Entity e = ( Entity )Activator.CreateInstance(level.vars.protoEntity.myEntType, level, 0, 0);

            if (e.getComponent(GlobalVars.POSITION_COMPONENT_NAME) != null)
            {
                PositionComponent newPosComp   = ( PositionComponent )e.getComponent(GlobalVars.POSITION_COMPONENT_NAME);
                PositionComponent protoPosComp = ( PositionComponent )level.vars.protoEntity.getComponent(GlobalVars.POSITION_COMPONENT_NAME);
                e.isStartingEntity = true;
                level.getMovementSystem().changePosition(newPosComp, protoPosComp.x, protoPosComp.y, false, false);

                if (e is BasicGround)
                {
                    BasicGround ground = ( BasicGround )e;

                    //If no ground above it, change to a grass sprite
                    List <Entity> above = level.getCollisionSystem().findObjectAtPoint(newPosComp.x, (newPosComp.y - newPosComp.height / 2 - 1));
                    if (above.Count <= 0 || !(above[0] is BasicGround))
                    {
                        ground.changeSprite(false);
                    }

                    //If ground below it, make dirt
                    List <Entity> below = level.getCollisionSystem().findObjectAtPoint(newPosComp.x, (newPosComp.y + newPosComp.height / 2 + 1));
                    if (below.Count > 0 && (below[0] is BasicGround))
                    {
                        BasicGround ground2 = ( BasicGround )below[0];
                        ground2.changeSprite(true);
                    }
                }
            }
            level.addEntity(e.randId, e);
            removeProtoEntity();
            selectEntity(e);
        }
Ejemplo n.º 2
0
        //Reads in a paint image and adds all entities to the level.
        public void readImage(Level level)
        {
            for (int y = 0; y < img.Height; y++)
            {
                for (int x = 0; x < img.Width; x++)
                {
                    //Get the color of the pixel
                    Color col = img.GetPixel(x, y);

                    float levelX = x;
                    float levelY = y;

                    //First check for cases which have some variation in possible color values
                    //i.e. something identified with only it's RG, or GB, or RB, or just R or G or B
                    if (col.R == switchReserveRed)
                    {
                        Entity s = null;

                        if (col.G == permSwitchG)
                        {
                            s = new SwitchEntity(level, rand.Next(Int32.MinValue, Int32.MaxValue), levelX * tileWidth, levelY * tileHeight);
                        }
                        else if (col.G == presSwitchG)
                        {
                            //Pressure Switch
                            s = new PressureSwitchEntity(level, rand.Next(Int32.MinValue, Int32.MaxValue), levelX * tileWidth, levelY * tileWidth);
                            TimedSwitchComponent timeComp = ( TimedSwitchComponent )s.getComponent(GlobalVars.TIMED_SWITCH_COMPONENT_NAME);
                            timeComp.baseTime = 0;
                        }
                        else
                        {
                            s = new TimedSwitchEntity(level, rand.Next(Int32.MinValue, Int32.MaxValue), levelX * tileWidth, levelY * tileWidth);
                            TimedSwitchComponent timeComp = ( TimedSwitchComponent )s.getComponent(GlobalVars.TIMED_SWITCH_COMPONENT_NAME);
                            timeComp.baseTime = col.G / 10;
                        }
                        s.isStartingEntity = true;
                        adjustLocation(s, level);
                        switches.Add(col.B, s);
                        level.addEntity(s.randId, s);
                    }
                    else if (col.R == spikeSwitchReserveRed)
                    {
                        float time = -1;
                        if (col.G < 255)
                        {
                            time = col.G / 10;
                        }
                        Entity s = new SpikeSwitchEntity(level, rand.Next(Int32.MinValue, Int32.MaxValue), levelX * tileWidth, levelY * tileHeight, time);

                        s.isStartingEntity = true;
                        adjustLocation(s, level);
                        switches.Add(col.B, s);
                        level.addEntity(s.randId, s);
                    }
                    else if (col.R == shooterRedUp || col.R == shooterRedRight || col.R == shooterRedDown || col.R == shooterRedLeft)
                    {
                        float betweenBursts = ( float )col.B / ( float )10;
                        int   switchId      = col.G;

                        TimedShooterEntity shooter = new TimedShooterEntity(level, rand.Next(Int32.MinValue, Int32.MaxValue), levelX * tileWidth, levelY * tileHeight, betweenBursts, 1, col.R - 110, switchId);
                        if (switchId == 0)
                        {
                            shooter.removeComponent(GlobalVars.SWITCH_LISTENER_COMPONENT_NAME);
                        }
                        else
                        {
                            SwitchListenerComponent slComp = ( SwitchListenerComponent )shooter.getComponent(GlobalVars.SWITCH_LISTENER_COMPONENT_NAME);
                            if (switches.ContainsKey(switchId))
                            {
                                slComp.switchId = switches[switchId].randId;
                            }
                            else
                            {
                                unmachedSwitchListeners.Add(slComp, switchId);
                            }
                        }
                        adjustLocation(shooter, level);
                        shooter.isStartingEntity = true;
                        level.addEntity(shooter);
                    }
                    else if (col.R == smushRed && (col.B - 4 <= 0))
                    {
                        int switchId           = col.G;
                        SmushBlockEntity smush = new SmushBlockEntity(level, rand.Next(Int32.MinValue, Int32.MaxValue), levelX * tileWidth, levelY * tileHeight, col.B, switchId);

                        if (switchId == 0)
                        {
                            smush.removeComponent(GlobalVars.SWITCH_LISTENER_COMPONENT_NAME);
                        }
                        else
                        {
                            SwitchListenerComponent slComp = ( SwitchListenerComponent )smush.getComponent(GlobalVars.SWITCH_LISTENER_COMPONENT_NAME);
                            if (switches.ContainsKey(switchId))
                            {
                                slComp.switchId = switches[switchId].randId;
                            }
                            else
                            {
                                unmachedSwitchListeners.Add(slComp, switchId);
                            }
                        }

                        adjustLocation(smush, level);
                        smush.isStartingEntity = true;
                        level.addEntity(smush);
                    }
                    else if (col.G == tallDoorReserveGreen || col.G == wideDoorReserveGreen || col.G == openTallDoorReserveGreen || col.G == openWideDoorReserveGreen)
                    {
                        float width  = tallDoorWidth;
                        float height = tallDoorHeight;

                        if (col.G == wideDoorReserveGreen || col.G == openWideDoorReserveGreen)
                        {
                            width  = wideDoorWidth;
                            height = wideDoorHeight;
                        }

                        DoorEntity door = new DoorEntity(level, rand.Next(Int32.MinValue, Int32.MaxValue), levelX * tileWidth, levelY * tileHeight, width, height, (col.G % 100 == 0 || col.G % 100 == 1));
                        adjustLocation(door, level);
                        SwitchListenerComponent slComp = ( SwitchListenerComponent )door.getComponent(GlobalVars.SWITCH_LISTENER_COMPONENT_NAME);
                        door.isStartingEntity = true;
                        //check for its switch
                        if (switches.ContainsKey(col.B))
                        {
                            slComp.switchId = switches[col.B].randId;
                        }
                        else
                        {
                            unmachedSwitchListeners.Add(slComp, col.B);
                        }
                        level.addEntity(door);
                    }
                    else if (col.R == spikeRed && col.G == spikeGreen && (col.B - 4 <= 0))
                    {
                        SpikeEntity spike = new SpikeEntity(level, rand.Next(Int32.MinValue, Int32.MaxValue), levelX * tileWidth, levelY * tileHeight, col.B);
                        adjustLocation(spike, level);
                        spike.isStartingEntity = true;
                        level.addEntity(spike);
                    }
                    else if (col.R == signRed && col.G == signGreen)
                    {
                        SignEntity signEnt = new SignEntity(level, rand.Next(Int32.MinValue, Int32.MaxValue), levelX * tileWidth, levelY * tileHeight, col.B);
                        adjustLocation(signEnt, level);
                        signEnt.isStartingEntity = true;
                        level.addEntity(signEnt);
                    }

                    //Now just check for the specific colors
                    else if (col == playerCol)
                    {
                        Player player = new Player(level, rand.Next(Int32.MinValue, Int32.MaxValue), levelX * tileWidth, levelY * tileHeight);
                        adjustLocation(player, level);
                        PositionComponent posComp = ( PositionComponent )player.getComponent(GlobalVars.POSITION_COMPONENT_NAME);
                        level.getMovementSystem().teleportToNoCollisionCheck(posComp, posComp.x, posComp.y - GlobalVars.MIN_TILE_SIZE / 2);
                        posComp.setCurrentLocToStartingLoc();
                        player.isStartingEntity = true;
                        level.addEntity(player.randId, player);
                    }
                    else if (col == movePlatformTurn)
                    {
                        PlatformTurnEntity platTurn = new PlatformTurnEntity(level, rand.Next(Int32.MinValue, Int32.MaxValue), levelX * tileWidth, levelY * tileHeight);
                        adjustLocation(platTurn, level);
                        platTurn.isStartingEntity = true;
                        level.addEntity(platTurn.randId, platTurn);
                    }
                    else if (col == basicGroundCol)
                    {
                        float groundX     = (levelX) * tileWidth;
                        float groundWidth = tileWidth;

                        BasicGround ground = new BasicGround(level, rand.Next(Int32.MinValue, Int32.MaxValue), groundX, (levelY) * tileHeight, groundWidth, tileHeight);
                        adjustLocation(ground, level);
                        ground.isStartingEntity = true;
                        level.addEntity(ground.randId, ground);

                        if (!GlobalVars.fullForegroundImage)
                        {
                            //If no ground above it, change to a grass sprite
                            List <Entity> above = level.getCollisionSystem().findObjectAtPoint((levelX) * tileWidth, (levelY - 1) * tileWidth);
                            if (above.Count <= 0 || !(above[0] is BasicGround))
                            {
                                ground.changeSprite(false);
                            }
                        }
                    }
                    else if (col == testEntityColor)
                    {
                        float      xLoc = (levelX) * tileWidth;
                        float      yLoc = (levelY) * tileHeight;
                        int        id   = rand.Next(Int32.MinValue, Int32.MaxValue);
                        TestEntity test = new TestEntity(level, id, xLoc, yLoc);
                        adjustLocation(test, level);
                        test.isStartingEntity = true;
                        level.addEntity(test.randId, test);
                    }
                    else if (col == simpleEnemyColor)
                    {
                        float             xLoc  = (levelX) * tileWidth;
                        float             yLoc  = (levelY) * tileHeight;
                        int               id    = rand.Next(Int32.MinValue, Int32.MaxValue);
                        SimpleEnemyEntity enemy = new SimpleEnemyEntity(level, id, xLoc, yLoc, false);
                        adjustLocation(enemy, level);
                        enemy.isStartingEntity = true;
                        level.addEntity(enemy.randId, enemy);
                    }
                    else if (col == flyingEnemyColor)
                    {
                        float             xLoc  = (levelX) * tileWidth;
                        float             yLoc  = (levelY) * tileHeight;
                        int               id    = rand.Next(Int32.MinValue, Int32.MaxValue);
                        FlyingEnemyEntity enemy = new FlyingEnemyEntity(level, id, xLoc, yLoc, false);
                        adjustLocation(enemy, level);
                        enemy.isStartingEntity = true;
                        level.addEntity(enemy.randId, enemy);
                    }
                    else if (col == shieldWalkingEnemyColor)
                    {
                        float             xLoc  = (levelX) * tileWidth;
                        float             yLoc  = (levelY) * tileHeight;
                        int               id    = rand.Next(Int32.MinValue, Int32.MaxValue);
                        SimpleEnemyEntity enemy = new SimpleEnemyEntity(level, id, xLoc, yLoc, true);
                        adjustLocation(enemy, level);
                        enemy.isStartingEntity = true;
                        level.addEntity(enemy.randId, enemy);
                    }
                    else if (col == shieldFlyingEnemyColor)
                    {
                        float             xLoc  = (levelX) * tileWidth;
                        float             yLoc  = (levelY) * tileHeight;
                        int               id    = rand.Next(Int32.MinValue, Int32.MaxValue);
                        FlyingEnemyEntity enemy = new FlyingEnemyEntity(level, id, xLoc, yLoc, true);
                        adjustLocation(enemy, level);
                        enemy.isStartingEntity = true;
                        level.addEntity(enemy.randId, enemy);
                    }
                    else if (col == checkPointCollider)
                    {
                        CheckPointEntity checkEnt = new CheckPointEntity(level, rand.Next(Int32.MinValue, Int32.MaxValue), levelX * tileWidth, levelY * tileHeight);
                        adjustLocation(checkEnt, level);
                        checkEnt.isStartingEntity = true;
                        level.addEntity(checkEnt.randId, checkEnt);
                    }/*else if ( col == checkPointCollider ) {
                      *
                      * float xLoc = ( levelX ) * tileWidth;
                      * float yLoc = ( levelY ) * tileHeight;
                      * int id = rand.Next( Int32.MinValue, Int32.MaxValue );
                      * EndLevelEntity lvlEnd = new EndLevelEntity( level, id, xLoc, yLoc );
                      * adjustLocation( lvlEnd, level );
                      * lvlEnd.isStartingEntity = true;
                      * level.addEntity( lvlEnd.randId, lvlEnd );
                      *
                      * }*/
                    else if (col == vertMovPlatCol)
                    {
                        float xLoc = (levelX) * tileWidth;
                        float yLoc = (levelY) * tileHeight;
                        int   id   = rand.Next(Int32.MinValue, Int32.MaxValue);

                        MovingPlatformEntity plat = new MovingPlatformEntity(level, id, xLoc, yLoc);
                        adjustLocation(plat, level);

                        plat.isStartingEntity = true;
                        level.addEntity(plat);
                    } /*else if ( col == horizMovPlatCol ) {
                       *
                       * float xLoc = ( levelX ) * tileWidth;
                       * float yLoc = ( levelY ) * tileHeight;
                       * int id = rand.Next( Int32.MinValue, Int32.MaxValue );
                       *
                       * MovingPlatformEntity plat = new MovingPlatformEntity( level, id, xLoc, yLoc );
                       * adjustLocation( plat, level );
                       *
                       * plat.isStartingEntity = true;
                       * MovingPlatformComponent movPlatComp = ( MovingPlatformComponent )plat.getComponent( GlobalVars.MOVING_PLATFORM_COMPONENT_NAME );
                       * movPlatComp.vertical = false;
                       * VelocityComponent velComp = ( VelocityComponent )plat.getComponent( GlobalVars.VELOCITY_COMPONENT_NAME );
                       * velComp.y = 0;
                       * level.addEntity( plat );
                       *
                       * }*/
                    else if (col == bouncePickup)
                    {
                        float xLoc = (levelX) * tileWidth;
                        float yLoc = (levelY) * tileHeight;
                        int   id   = rand.Next(Int32.MinValue, Int32.MaxValue);

                        PowerupPickupEntity pickup = new PowerupPickupEntity(level, id, xLoc, yLoc, GlobalVars.BOUNCE_NUM);
                        adjustLocation(pickup, level);

                        pickup.isStartingEntity = true;
                        level.addEntity(pickup);
                    }
                    else if (col == speedyPickup)
                    {
                        float xLoc = (levelX) * tileWidth;
                        float yLoc = (levelY) * tileHeight;
                        int   id   = rand.Next(Int32.MinValue, Int32.MaxValue);

                        PowerupPickupEntity pickup = new PowerupPickupEntity(level, id, xLoc, yLoc, GlobalVars.SPEED_NUM);
                        adjustLocation(pickup, level);

                        pickup.isStartingEntity = true;
                        level.addEntity(pickup);
                    }
                    else if (col == jmpPickup)
                    {
                        float xLoc = (levelX) * tileWidth;
                        float yLoc = (levelY) * tileHeight;
                        int   id   = rand.Next(Int32.MinValue, Int32.MaxValue);

                        PowerupPickupEntity pickup = new PowerupPickupEntity(level, id, xLoc, yLoc, GlobalVars.JMP_NUM);
                        adjustLocation(pickup, level);

                        pickup.isStartingEntity = true;
                        level.addEntity(pickup);
                    }
                    else if (col == glidePickup)
                    {
                        float xLoc = (levelX) * tileWidth;
                        float yLoc = (levelY) * tileHeight;
                        int   id   = rand.Next(Int32.MinValue, Int32.MaxValue);

                        PowerupPickupEntity pickup = new PowerupPickupEntity(level, id, xLoc, yLoc, GlobalVars.GLIDE_NUM);
                        adjustLocation(pickup, level);

                        pickup.isStartingEntity = true;
                        level.addEntity(pickup);
                    }
                    else if (col == spawnPickup)
                    {
                        float xLoc = (levelX) * tileWidth;
                        float yLoc = (levelY) * tileHeight;
                        int   id   = rand.Next(Int32.MinValue, Int32.MaxValue);

                        PowerupPickupEntity pickup = new PowerupPickupEntity(level, id, xLoc, yLoc, GlobalVars.SPAWN_NUM);
                        adjustLocation(pickup, level);

                        pickup.isStartingEntity = true;
                        level.addEntity(pickup);
                    }
                    else if (col == grapPickup)
                    {
                        float xLoc = (levelX) * tileWidth;
                        float yLoc = (levelY) * tileHeight;
                        int   id   = rand.Next(Int32.MinValue, Int32.MaxValue);

                        PowerupPickupEntity pickup = new PowerupPickupEntity(level, id, xLoc, yLoc, GlobalVars.GRAP_NUM);
                        adjustLocation(pickup, level);

                        pickup.isStartingEntity = true;
                        level.addEntity(pickup);
                    }
                    else if (col == visionOrbUnlock)
                    {
                        float xLoc = (levelX) * tileWidth;
                        float yLoc = (levelY) * tileHeight;
                        int   id   = rand.Next(Int32.MinValue, Int32.MaxValue);

                        VisionOrbUnlock visUnlock = new VisionOrbUnlock(level, id, xLoc, yLoc);
                        adjustLocation(visUnlock, level);

                        visUnlock.isStartingEntity = true;
                        level.addEntity(visUnlock);
                    }
                }
            }

            //Match any unmatched switch listeners to their switch
            foreach (SwitchListenerComponent sc in unmachedSwitchListeners.Keys)
            {
                if (switches.ContainsKey(unmachedSwitchListeners[sc]))
                {
                    SwitchListenerComponent slComp = sc;
                    slComp.switchId = switches[unmachedSwitchListeners[sc]].randId;
                }
                else
                {
                    Console.WriteLine("Unmatched Switch Listener - B: " + unmachedSwitchListeners[sc]);
                }
            }
        }