Package eas.users.demos.polytris

Source Code of eas.users.demos.polytris.LukesPolytrisMaster

/*
* File name:        TetrisMaster.java (package eas.users.lukas.demos.tetris2)
* Author(s):        lko
* Java version:     7.0
* Generation date:  30.05.2013 (11:57:49)
*
* (c) This file and the EAS (Easy Agent Simulation) framework containing it
* is protected by Creative Commons by-nc-sa license. Any altered or
* further developed versions of this file have to meet the agreements
* stated by the license conditions.
*
* In a nutshell
* -------------
* You are free:
* - to Share -- to copy, distribute and transmit the work
* - to Remix -- to adapt the work
*
* Under the following conditions:
* - Attribution -- You must attribute the work in the manner specified by the
*   author or licensor (but not in any way that suggests that they endorse
*   you or your use of the work).
* - Noncommercial -- You may not use this work for commercial purposes.
* - Share Alike -- If you alter, transform, or build upon this work, you may
*   distribute the resulting work only under the same or a similar license to
*   this one.
*
* + Detailed license conditions (Germany):
*   http://creativecommons.org/licenses/by-nc-sa/3.0/de/
* + Detailed license conditions (unported):
*   http://creativecommons.org/licenses/by-nc-sa/3.0/deed.en
*
* This header must be placed in the beginning of any version of this file.
*/

package eas.users.demos.polytris;

import java.io.File;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;

import javax.imageio.ImageIO;

import com.jscoreoid.model.Score;

import eas.math.geometry.Vector2D;
import eas.miscellaneous.StaticMethods;
import eas.miscellaneous.system.windowFrames.GeneralDialog;
import eas.miscellaneous.system.windowFrames.StaticWindow;
import eas.miscellaneous.useful.MP3Player;
import eas.plugins.masterScheduler.AbstractDefaultKeyEventMaster;
import eas.plugins.standard.visualization.AllroundVideoPlugin;
import eas.simulation.ConstantsSimulation;
import eas.simulation.Wink;
import eas.simulation.spatial.sim2D.standardAgents.PacmanAgent2D;
import eas.startSetup.ParCollection;
import eas.startSetup.SingleParameter;
import eas.startSetup.parameterDatatypes.Datatypes;
import eas.users.demos.polytris.clocks.SimpleAcceleratingClock;
import eas.users.demos.polytris.highscore.ScoreoidApi;

/**
* @author lko
*/
public class LukesPolytrisMaster extends AbstractDefaultKeyEventMaster<PolytrisEnvironment> {
   
    /**
     *
     */
    private static final long serialVersionUID = 1065928959485285126L;

    @Override
    public List<SingleParameter> getParameters() {
        List<SingleParameter> liszt = super.getParameters();
       
        liszt.add(new SingleParameter("Polytris.Ultimate", Datatypes.BOOLEAN, false));
        liszt.add(new SingleParameter("Polytris.Level", Datatypes.INTEGER, 6));
        liszt.add(new SingleParameter("Polytris.SoundMode", Datatypes.fixedStringSet(new String[] {
                "Normal",
                "DuneStyle",
                "Quiet"}), "Normal"));
       
        return liszt;
    }
   
    private Vector2D size = new Vector2D(12, 30);
   
    private static int level;
    private static PolytrisEnvironment env;
   
    @Override
    public PolytrisEnvironment[] generateRunnables(ParCollection params) {
        if (env != null && level == params.getParValueInt("Polytris.Level")) {
            return new PolytrisEnvironment[] { env };
        }
       
        level = params.getParValueInt("Polytris.Level");
       
        int addWidth = 0;
        int addHeight = 0;
       
        for (int i = 6; i < level; i++) {
            addWidth += 1;
            addHeight += 2;
        }
       
        env = new PolytrisEnvironment(
                params,
                (int) size.x + addWidth,
                (int) size.y + addHeight,
                new SimpleAcceleratingClock(15, 1000, 2),
                new HashSet<ArrayList<Vector2D>>());
       
        // Make all corners of field visualized.
        PacmanAgent2D pac0 = new PacmanAgent2D(0, env, params);
        env.addAgent(pac0, new Vector2D(0, 0), 0);
        env.setAgentPosition(0, new Vector2D(env.getGridWidth() - 1, env.getGridHeight() - 1));
        env.setAgentPosition(0, new Vector2D(0, env.getGridHeight() - 1));
        env.setAgentPosition(0, new Vector2D(env.getGridWidth() - 1, 0));
        env.removeAgent(0);
       
//        int max = 1;
//       
//        for (int i = params.getParValueInt("Polytris.Level"); i > 0; i--) {
//            ArrayList<ArrayList<Vector2D>> figuren = new ArrayList<ArrayList<Vector2D>>();
//           
//            while (figuren.size() < max) {
//                figuren.addAll(ConstantsPolytris.nTrisAgents(i));
//            }
//           
//            if (max < figuren.size()) {
//                max = figuren.size();
//            }
//           
//            env.getPossibleAgents().addAll(figuren);
//        }
       
        return new PolytrisEnvironment[] {env};
    }

    @Override
    public void runBeforeSimulation(PolytrisEnvironment environment,
            ParCollection params) {
        super.runBeforeSimulation(environment, params);
        if (params.getParValueString("Polytris.SoundMode").equals("Quiet")) {
            MP3Player.turnOffAllSounds = true;
        }
    }
   
    @Override
    public String id() {
        return ConstantsSimulation.DEFAULT_MASTER_SCHEDULER_ID + "-tetris2";
    }
   
    private MP3Player mp3;
   
    @Override
    public void runDuringSimulation(PolytrisEnvironment umg, Wink simZyk,
            ParCollection params) {
        super.runDuringSimulation(umg, simZyk, params);
        if (simZyk.getLastTick() == 0) {
            // Logo.
            File ostToPlay;
           
            if (params.getParValueString("Polytris.SoundMode").equals("DuneStyle")) {
                ostToPlay = new File("./sharedDirectory/Polytris/ost2");
            } else {
                ostToPlay = new File("./sharedDirectory/Polytris/ost1");
            }
           
            mp3 = new MP3Player(ostToPlay);
            mp3.setRepeat(true);
            new Thread(mp3).start();
           
            try {
                StaticWindow window;
               
                if (params.getParValueBoolean("Polytris.Ultimate")) {
                    window = StaticMethods.showImage(ImageIO.read(new File("sharedDirectory/Polytris/PolytrisLogo-extreme.png")), "", true);
                    window.setSize(window.getWidth() - 40, window.getHeight() - 15);
                } else {
                    window = StaticMethods.showImage(ImageIO.read(new File("sharedDirectory/Polytris/PolytrisLogo.png")), "", true);
                    window.setSize(window.getWidth() - 35, window.getHeight() - 15);
                }
               
                window.setLocationRelativeTo(null);
                Thread.sleep(7500);
                window.dispose();
            } catch (Exception e1) {
                System.out.println("Whoops!" + e1);
            }
           
            AllroundVideoPlugin vid = (AllroundVideoPlugin) umg.getSimTime().getPluginObject(new AllroundVideoPlugin().id());
            vid.showTooltipps(false);
            if (params.getParValueBoolean("Polytris.Ultimate")) {
//                vid.setZoom(new Rectangle2D(new Vector2D(0, 0), size.mult(100)));
                vid.setFollowAgent(true);
            }
        }
    }
   
    @Override
    public boolean isTerminationRequested(PolytrisEnvironment env,
            Wink currentTime, ParCollection params) {
        return env.isGo();
    }
   
    @Override
    public void runAfterSimulation(PolytrisEnvironment umg, ParCollection params) {
        super.runAfterSimulation(umg, params);
        mp3.requestStop();
        new Thread(new MP3Player(new File("./sharedDirectory/Polytris/gameOver"))).start();
        try {Thread.sleep(5000);} catch (InterruptedException e) {}

        String username = "";
       
        if (new File("./sharedDirectory/Polytris/username.txt").exists()) {
            username = StaticMethods.liesTextArray(new File("./sharedDirectory/Polytris/username.txt"), params, true).get(0);
        }
       
        GeneralDialog dia0 = new GeneralDialog(
                null,
                null,
                "Enter your name for score " + umg.getScore(),
                new String[] {"Store"},
                username,
                30,
                50,
                true);
       
        dia0.setLocationRelativeTo(null);
        dia0.setVisible(true);
       
        if (umg.getScorez().isHighscore(umg.getScore())) {
            umg.addHighscore(umg.getScore(), dia0.getText(), params.getParValueInt("Polytris.Level"));
        }

        boolean isExtreme = false;
        String extreme = ".";
        if (params.getParValueBoolean("Polytris.Ultimate")) {
            extreme = " Extreme!";
            isExtreme = true;
        }
       
        List<Score> globalScorez = ScoreoidApi.addScore((int) umg.getScore(), dia0.getText(), level, isExtreme);
        List<String> globalScorezString = ScoreoidApi.convertList(globalScorez, umg.getScore(), username, level, isExtreme);
       
        StaticMethods.speichereTextDatei("./sharedDirectory/Polytris", "username.txt", dia0.getText(), params);
       
        if (globalScorez != null) {
            GeneralDialog diaGlobalScore = new GeneralDialog(
                    null,
                    null,
                    "Global highscorez -- Your score: " + umg.getScore(),
                    new String[] {"Close"},
                    globalScorezString.toString().replace("]", "").replace("[", "").replace(", ", "\n")
                        + "\n\nThank you for playing Lhuk's Polytris" + extreme,
                    15,
                    50,
                    false);
            diaGlobalScore.setLocationRelativeTo(null);
            diaGlobalScore.setVisible(true);
        } else {
            GeneralDialog diaGlobalScore = new GeneralDialog(
                    null,
                    "The server is not available, global scores were not submitted.",
                    "Server not available.",
                    new String[] {"Close"},
                    null,
                    15,
                    50,
                    false);
            diaGlobalScore.setLocationRelativeTo(null);
            diaGlobalScore.setVisible(true);
           
            GeneralDialog diaLocalScore = new GeneralDialog(
                    null,
                    null,
                    "Local highscorez -- Your score: " + umg.getScore(),
                    new String[] {"Close"},
                    umg.getScorez().toString() + "\n\nThank you for playing Lhuk's Polytris" + extreme,
                    15,
                    50,
                    false);
            diaLocalScore.setLocationRelativeTo(null);
            diaLocalScore.setVisible(true);
        }

        System.exit(0);
    }
}
TOP

Related Classes of eas.users.demos.polytris.LukesPolytrisMaster

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.