Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/coop playing #5

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
language: java
jdk:
- oraclejdk8
- oraclejdk14
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,16 @@ You will need a Java JDK 8+ and maven 3+.

Execute **mvn clean package assembly:single** to build the release package.

# How to run it ?
```
mvn clean

mvn package

mvn exec:java

```

## How to play ?

- SPACE - Start a new game
Expand Down
8 changes: 8 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,14 @@
<tagNameFormat>@{project.version}</tagNameFormat>
</configuration>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>1.2.1</version>
<configuration>
<mainClass>spypunk.tetris.Main</mainClass>
</configuration>
</plugin>
</plugins>
</build>

Expand Down
50 changes: 47 additions & 3 deletions src/main/java/spypunk/tetris/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,12 @@

package spypunk.tetris;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;

import javax.swing.JOptionPane;
import javax.swing.*;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand Down Expand Up @@ -44,8 +47,8 @@ private Main() {

public static void main(final String[] args) {
try {
final Injector injector = Guice.createInjector(new TetrisModule());
injector.getInstance(TetrisController.class).start();
new PlayerChoosingScreen();

} catch (CreationException | ConfigurationException | ProvisionException e) {
LOGGER.error(e.getMessage(), e);
SwingUtils.doInAWTThread(Main::showErrorDialog);
Expand All @@ -57,5 +60,46 @@ private static void showErrorDialog() {
ERROR_MESSAGE,
ERROR_TITLE,
JOptionPane.ERROR_MESSAGE);
}

private static class PlayerChoosingScreen{
public PlayerChoosingScreen() {
JFrame mainFrame = new JFrame("Select Player Number");
mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
mainFrame.setSize(400,200);

mainFrame.getContentPane().setLayout(new FlowLayout());
//todo debug etmek icin maven sekmesini ac ordan exec:java yı debug et!
JButton onePlayer = new JButton("One Player");
onePlayer.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
final Injector injector = Guice.createInjector(new TetrisModule());
injector.getInstance(TetrisController.class).start();
mainFrame.dispose();
}
});
JButton twoPlayers = new JButton("Two Players");
twoPlayers.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
final Injector injector = Guice.createInjector(new TetrisModule());
injector.getInstance(TetrisController.class).start();
//new KeyConfigScreen();
mainFrame.dispose();

}
});

mainFrame.add(onePlayer);
mainFrame.add(twoPlayers);

mainFrame.setLocationRelativeTo(null);
mainFrame.setVisible(true);
}




}
}
3 changes: 3 additions & 0 deletions src/main/java/spypunk/tetris/guice/TetrisModule.java
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@
import spypunk.tetris.ui.font.cache.FontCache;
import spypunk.tetris.ui.font.cache.FontCacheImpl;
import spypunk.tetris.ui.view.TetrisMainView;
import spypunk.tetris.ui.view.TetrisMainView2Impl;
import spypunk.tetris.ui.view.TetrisMainViewImpl;

public class TetrisModule extends AbstractModule {
Expand All @@ -66,6 +67,7 @@ public class TetrisModule extends AbstractModule {

private final Tetris tetris;


public TetrisModule() {
String name;
String version;
Expand All @@ -85,6 +87,7 @@ public TetrisModule() {
}

tetris = new Tetris(name, version, uri);

}

@Override
Expand Down
120 changes: 120 additions & 0 deletions src/main/java/spypunk/tetris/guice/TetrisModule2.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
/*
* Copyright © 2016-2017 spypunk <[email protected]>
*
* This work is free. You can redistribute it and/or modify it under the
* terms of the Do What The Fuck You Want To Public License, Version 2,
* as published by Sam Hocevar. See the COPYING file for more details.
*/

package spypunk.tetris.guice;

import java.io.IOException;
import java.io.InputStream;
import java.lang.annotation.ElementType;
import java.lang.annotation.Target;
import java.net.URI;
import java.util.Properties;

import javax.inject.Inject;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.google.inject.AbstractModule;
import com.google.inject.BindingAnnotation;
import com.google.inject.Provides;

import spypunk.tetris.Main;
import spypunk.tetris.exception.TetrisException;
import spypunk.tetris.factory.ShapeFactory;
import spypunk.tetris.factory.ShapeFactoryImpl;
import spypunk.tetris.model.Tetris;
import spypunk.tetris.service.TetrisService;
import spypunk.tetris.service.TetrisServiceImpl;
import spypunk.tetris.sound.cache.SoundClipCache;
import spypunk.tetris.sound.cache.SoundClipCacheImpl;
import spypunk.tetris.sound.service.SoundService;
import spypunk.tetris.sound.service.SoundServiceImpl;
import spypunk.tetris.ui.cache.ImageCache;
import spypunk.tetris.ui.cache.ImageCacheImpl;
import spypunk.tetris.ui.controller.TetrisController;
import spypunk.tetris.ui.controller.TetrisControllerImpl;
import spypunk.tetris.ui.controller.command.cache.TetrisControllerCommandCache;
import spypunk.tetris.ui.controller.command.cache.TetrisControllerCommandCacheImpl;
import spypunk.tetris.ui.controller.event.TetrisControllerTetrisEventHandler;
import spypunk.tetris.ui.controller.event.TetrisControllerTetrisEventHandlerImpl;
import spypunk.tetris.ui.controller.gameloop.TetrisControllerGameLoop;
import spypunk.tetris.ui.controller.gameloop.TetrisControllerGameLoopImpl;
import spypunk.tetris.ui.controller.input.TetrisControllerInputHandler;
import spypunk.tetris.ui.controller.input.TetrisControllerInputHandlerImpl;
import spypunk.tetris.ui.font.cache.FontCache;
import spypunk.tetris.ui.font.cache.FontCacheImpl;
import spypunk.tetris.ui.view.TetrisMainView;
import spypunk.tetris.ui.view.TetrisMainView2Impl;
import spypunk.tetris.ui.view.TetrisMainViewImpl;

public class TetrisModule2 extends AbstractModule {

private static final Logger LOGGER = LoggerFactory.getLogger(Main.class);

private static final String NAME_KEY = "name";

private static final String VERSION_KEY = "version";

private static final String URL_KEY = "url";

private static final String TETRIS_PROPERTIES = "/tetris.properties";

private final Tetris tetris;


public TetrisModule2() {
String name;
String version;
URI uri;

try (InputStream inputStream = TetrisModule.class.getResource(TETRIS_PROPERTIES).openStream()) {
final Properties properties = new Properties();

properties.load(inputStream);

name = properties.getProperty(NAME_KEY);
version = properties.getProperty(VERSION_KEY);
uri = URI.create(properties.getProperty(URL_KEY));
} catch (final IOException e) {
LOGGER.error(e.getMessage(), e);
throw new TetrisException(e);
}

tetris = new Tetris(name, version, uri);

}

@Override
protected void configure() {
bind(TetrisService.class).to(TetrisServiceImpl.class);
bind(ShapeFactory.class).to(ShapeFactoryImpl.class);
bind(TetrisController.class).to(TetrisControllerImpl.class);
bind(ImageCache.class).to(ImageCacheImpl.class);
bind(FontCache.class).to(FontCacheImpl.class);
bind(TetrisControllerCommandCache.class).to(TetrisControllerCommandCacheImpl.class);
bind(SoundService.class).to(SoundServiceImpl.class);
bind(SoundClipCache.class).to(SoundClipCacheImpl.class);
bind(TetrisControllerInputHandler.class).to(TetrisControllerInputHandlerImpl.class);
bind(TetrisControllerTetrisEventHandler.class).to(TetrisControllerTetrisEventHandlerImpl.class);
bind(TetrisControllerGameLoop.class).to(TetrisControllerGameLoopImpl.class);
bind(TetrisMainView.class).to(TetrisMainView2Impl.class);
}

@Target({ ElementType.FIELD, ElementType.PARAMETER, ElementType.METHOD })
@BindingAnnotation
public @interface TetrisProvider {
}

@Provides
@TetrisProvider
@Inject
public Tetris getTetris() {
return tetris;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@

package spypunk.tetris.ui.controller;

import javax.swing.*;

public interface TetrisController {

void start();
Expand All @@ -19,4 +21,6 @@ public interface TetrisController {
void onKeyPressed(int keyCode);

void onKeyReleased(int keyCode);

JPanel getJPanel();
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,11 @@

import javax.inject.Inject;
import javax.inject.Singleton;
import javax.swing.*;

import spypunk.tetris.ui.controller.gameloop.TetrisControllerGameLoop;
import spypunk.tetris.ui.controller.input.TetrisControllerInputHandler;
import spypunk.tetris.ui.view.TetrisMainView;

@Singleton
public class TetrisControllerImpl implements TetrisController {
Expand All @@ -21,12 +23,17 @@ public class TetrisControllerImpl implements TetrisController {

private final TetrisControllerInputHandler tetrisControllerInputHandler;

private TetrisMainView mainView;

@Inject
public TetrisControllerImpl(final TetrisControllerGameLoop tetrisControllerGameLoop,
final TetrisControllerInputHandler tetrisControllerInputHandler) {
final TetrisControllerInputHandler tetrisControllerInputHandler,
final TetrisMainView mainView) {

this.tetrisControllerGameLoop = tetrisControllerGameLoop;
this.tetrisControllerInputHandler = tetrisControllerInputHandler;
this.mainView = mainView;

}

@Override
Expand All @@ -53,4 +60,10 @@ public void onKeyPressed(final int keyCode) {
public void onKeyReleased(final int keyCode) {
tetrisControllerInputHandler.onKeyReleased(keyCode);
}

@Override
public JPanel getJPanel() {
return this.mainView.getJPanel();
}

}
Loading