Skip to content
Merged
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
76 changes: 76 additions & 0 deletions Test/Main.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
import com.javagamemaker.javagameengine.GameWorld;
import com.javagamemaker.javagameengine.JavaGameEngine;
import com.javagamemaker.javagameengine.Scene;
import com.javagamemaker.javagameengine.components.GameObject;
import com.javagamemaker.javagameengine.components.gamecompnents.CollidingBox;
import com.javagamemaker.javagameengine.components.gamecompnents.Grabber;
import com.javagamemaker.javagameengine.components.gamecompnents.PlatformPlayerController;
import com.javagamemaker.javagameengine.input.Input;
import com.javagamemaker.javagameengine.input.InputComponent;
import com.javagamemaker.javagameengine.input.InputManager;
import com.javagamemaker.javagameengine.input.Keys;
import com.javagamemaker.javagameengine.msc.Debug;
import com.javagamemaker.javagameengine.msc.Vector2;

import java.awt.*;

public class Main extends JavaGameEngine {

public static void main(String[] args){
Scene scene = new Scene();

scene.add(new Player("Player 1"));
scene.add(new Player("Player 2"){
@Override
public void start() {
setPosition(new Vector2(0,-150));
super.start();
}
});

Input.addContext("Player 1");
//Input.addContext("Player 2");
Input.addContext("All");

scene.add(new CollidingBox(new Vector2(200,200)){
@Override
public void start() {
setScale(new Vector2(1500,100));
super.start();
}
});

setSelectedScene(scene);
start();

}

static class Player extends GameObject {

public Player(String context) {
add(new PlatformPlayerController(new InputComponent(context)));
add(new Grabber(this));
}

@Override
public void update() {
super.update();
if(Input.isKeyPressed(Keys.E)){
if(Input.getActiveContext().contains("Player 1")){
Input.addContext("Player 2");
Input.removeContext("Player 1");
}
else{

Input.addContext("Player 1");
Input.removeContext("Player 2");
}
}
if(Input.isKeyPressed(Keys.P)){
Input.addContext("Player 1");
Input.addContext("Player 2");
}
}
}

}
1 change: 0 additions & 1 deletion src/com/javagamemaker/javagameengine/GameWorld.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package com.javagamemaker.javagameengine;

import com.javagamemaker.javagameengine.input.Input;
import com.javagamemaker.javagameengine.msc.Debug;
import com.javagamemaker.javagameengine.msc.Vector2;

import javax.swing.*;
Expand Down
2 changes: 0 additions & 2 deletions src/com/javagamemaker/javagameengine/JavaGameEngine.java
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
package com.javagamemaker.javagameengine;

import com.javagamemaker.javagameengine.input.Input;
import com.javagamemaker.javagameengine.msc.Debug;
import com.javagamemaker.javagameengine.msc.Vector2;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.rmi.server.ExportException;

/**
* This is the main class in the JavaGameEngine gameengine
Expand Down
4 changes: 0 additions & 4 deletions src/com/javagamemaker/javagameengine/Scene.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,9 @@
import com.javagamemaker.javagameengine.msc.Debug;
import com.javagamemaker.javagameengine.msc.Vector2;

import javax.sound.sampled.*;
import javax.swing.*;
import java.awt.*;
import java.io.IOException;
import java.io.InputStream;
import java.util.*;
import java.util.List;

/**
* Scene is the level where the game is playing out. Scenes can be changed by calling JavaGameEngine.setSelectedScene(new Scene())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import com.javagamemaker.javagameengine.JavaGameEngine;
import com.javagamemaker.javagameengine.components.shapes.Rect;
import com.javagamemaker.javagameengine.input.Input;
import com.javagamemaker.javagameengine.msc.Debug;
import com.javagamemaker.javagameengine.input.InputComponent;
import com.javagamemaker.javagameengine.msc.Vector2;

import java.awt.*;
Expand Down Expand Up @@ -398,6 +398,7 @@ public void updateMili(){
* This method updates all the values to the component
*/
public void update(){

Point p = new Point((int) Input.getMousePosition().getX(), (int) Input.getMousePosition().getY());
/*
if mouse is inside, and we have not been we call mouse entered and we say it is entered
Expand Down Expand Up @@ -427,8 +428,8 @@ public void update(){
child.children.add(component);
}
child.addedChildren.clear();

}

}
/**
* @return polygon based on components vertices
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ public CollidingBox(Vector2 vector2) {
@Override
public void start() {
super.start();
setTag("Ground");
add(new Collider(localVertices));
setPosition(newPost);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import com.javagamemaker.javagameengine.components.Component;
import com.javagamemaker.javagameengine.components.PhysicsBody;
import com.javagamemaker.javagameengine.input.Input;
import com.javagamemaker.javagameengine.msc.Debug;
import com.javagamemaker.javagameengine.msc.Vector2;

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
package com.javagamemaker.javagameengine.components.gamecompnents;

import com.javagamemaker.javagameengine.CollisionEvent;
import com.javagamemaker.javagameengine.JavaGameEngine;
import com.javagamemaker.javagameengine.components.Collider;
import com.javagamemaker.javagameengine.components.Component;
import com.javagamemaker.javagameengine.components.PhysicsBody;
import com.javagamemaker.javagameengine.input.Input;
import com.javagamemaker.javagameengine.input.InputComponent;
import com.javagamemaker.javagameengine.input.Keys;
import com.javagamemaker.javagameengine.msc.Debug;
import com.javagamemaker.javagameengine.msc.Vector2;
Expand All @@ -23,8 +23,10 @@ public class PlatformPlayerController extends Component {
private int left = Keys.A;
private int space = Keys.SPACE;
private float jumpForce = 30;
private String groundTag = "Grounded";

private String groundTag = "Ground";


private InputComponent input;


public PlatformPlayerController(float maxSpeed, float friction) {
Expand Down Expand Up @@ -106,6 +108,10 @@ public void setSpace(int space) {
public PlatformPlayerController() {
}

public PlatformPlayerController(InputComponent input) {
this.input = input;
}

@Override
public void start() {
super.start();
Expand All @@ -114,6 +120,12 @@ public void start() {
if(parent.<PhysicsBody>getChild(new Collider()) == null)
parent.add(new Collider(true));

if(parent.getChild(new InputComponent("")) == null){
if(input == null)
input = new InputComponent("");
add(input);
}

if(parent.<PhysicsBody>getChild(new PhysicsBody()) == null)
parent.add(new PhysicsBody(true));
}
Expand All @@ -126,18 +138,18 @@ public void update() {
PhysicsBody physicsBody = parent.<PhysicsBody>getChild(new PhysicsBody());
if(physicsBody!= null){

if(Input.isKeyDown(right)){
if(input.isKeyDown(right)){
if(physicsBody.velocity.getX() < maxSpeed){
physicsBody.addForce(Vector2.right);
}
}
if(Input.isKeyDown(left)){
if(input.isKeyDown(left)){
if(physicsBody.velocity.getX() > -maxSpeed){
physicsBody.addForce(Vector2.left);
}
}

if(Input.isKeyPressed(space) && grounded){
if(input.isKeyPressed(space) && grounded){
physicsBody.addForce(Vector2.up.multiply(jumpForce));
}

Expand Down
36 changes: 32 additions & 4 deletions src/com/javagamemaker/javagameengine/input/Input.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,12 @@

import java.awt.event.KeyEvent;
import java.awt.event.MouseEvent;
import java.util.ArrayList;
import java.util.LinkedList;

public class Input {
private static final LinkedList<Integer> keyDowns = new LinkedList<>();

private static LinkedList<Integer> keyDowns = new LinkedList<>();
private static boolean isPressed = false;
private static int mouseIsPressed = 1000;
private static final LinkedList<Integer> mouseButtonDowns = new LinkedList<>();
Expand All @@ -16,6 +18,17 @@ public class Input {
private static Vector2 mousePositionOnCanvas = new Vector2(0, 0);

private static MouseEvent mouseEvent = null;
/**
* this is a list which says which InputComponents should give InputEvent
*/
private static ArrayList<String> activeContext = new ArrayList<>();

private Input() {
}

public static LinkedList<Integer> getKeyDowns() {
return keyDowns;
}

/**
*
Expand All @@ -25,6 +38,21 @@ public static Vector2 getMousePosition() {
return mousePosition;
}

public static void addContext(String context){
activeContext.add(context);
}
public static void removeContext(String context){
activeContext.remove(context);
}

public static ArrayList<String> getActiveContext() {
return activeContext;
}

public static void setActiveContext(ArrayList<String> activeContext_) {
activeContext = activeContext_;
}

public static MouseEvent getMouseEvent() {
return mouseEvent;
}
Expand All @@ -50,9 +78,9 @@ public static void addMouseButton(MouseEvent e) {
}

/**
*
* @return mouse position of panel so top right = 0,0
*/
*
* @return mouse position of panel so top right = 0,0
*/
public static Vector2 getMousePositionOnCanvas() {
return mousePositionOnCanvas;
}
Expand Down
68 changes: 68 additions & 0 deletions src/com/javagamemaker/javagameengine/input/InputComponent.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
package com.javagamemaker.javagameengine.input;

import com.javagamemaker.javagameengine.components.Component;
import com.javagamemaker.javagameengine.msc.Debug;

import java.util.ArrayList;
import java.util.LinkedList;

/**
* The InputComponent is used to get input
* In the component we can set the chanel to listen to
* and we can then set which chanel the Input class
* should send to
*/
public class InputComponent extends Component {
/**
* The chanel name which
*/
private String context = "";
private LinkedList<Integer> leftKeys = new LinkedList<>();

public String getContext() {
return context;
}

public void setContext(String context) {
this.context = context;
}

public InputComponent(String context) {
this.context = context;
}

@Override
public void update() {
super.update();
/*
If the left key does not exist in the down remove it
*/
LinkedList<Integer> keyBuffer = new LinkedList<>();
for(int key : leftKeys){
if(!Input.isKeyDown(key)){
keyBuffer.add(key);
}
}
leftKeys.removeAll(keyBuffer);

}

public boolean isKeyPressed(int keycode){
boolean pressed = Input.getKeyDowns().contains(keycode) &&
!leftKeys.contains(keycode) &&
Input.getActiveContext().contains(context);
if(pressed)
leftKeys.add(keycode);
return pressed;
}

public boolean isMousePressed(int mouseCode){
return Input.isMousePressed() && Input.getActiveContext().contains(context);
}

public boolean isKeyDown(int keycode){
return Input.getKeyDowns().contains(keycode) &&
Input.getActiveContext().contains(context);
}

}
15 changes: 15 additions & 0 deletions src/com/javagamemaker/javagameengine/input/InputManager.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package com.javagamemaker.javagameengine.input;

public class InputManager {

private String contextName = "";


public InputManager() {

}

public InputManager(String contextName) {
this.contextName = contextName;
}
}