Commit 4c765630 authored by Martin LOISEAU's avatar Martin LOISEAU 🎄
Browse files

Rework action and node logic

parent a840b6fc
......@@ -48,7 +48,7 @@ public class MainActivity extends AppCompatActivity {
SpeechRecognizer sr;
GoogleApiAvailability m_gga;
public TTSReader ttsreader;
private MainThread mainThread;
public MainThread mainThread;
private CaptureQR QRActionRequesting;
//game logic
......@@ -81,38 +81,38 @@ public class MainActivity extends AppCompatActivity {
OpenNodes = new ArrayList<Node>();
ArrayList<Action> A1 = new ArrayList<Action>();
A1.add(new TTSReading(this, "Test 1 aaa test 1"));
A1.add(new RemoveAtom(this, new ItemAtom(0)));
A1.add(new AddAtom(this, new ItemAtom(1)));
A1.add(new ClearNodes(this));
A1.add(new AddNode(this, 3));
A1.add(new TTSReading(this, 1, "Test 1 aaa test 1"));
A1.add(new RemoveAtom(this,1, new ItemAtom(0)));
A1.add(new AddAtom(this,1, new ItemAtom(1)));
A1.add(new ClearNodes(this,1));
A1.add(new AddNode(this,1, 3));
ArrayList<Atom> C1 = new ArrayList<Atom>();
C1.add(new ItemAtom(0));
ArrayList<Action> A2 = new ArrayList<Action>();
A2.add(new ClearNodes(this));
A2.add(new TTSReading(this, "Mauvais QR code"));
A2.add(new AddNode(this, 3));
A2.add(new ClearNodes(this,2));
A2.add(new TTSReading(this,2, "Mauvais QR code"));
A2.add(new AddNode(this,2, 3));
ArrayList<Atom> C2 = new ArrayList<Atom>();
ArrayList<Action> A3 = new ArrayList<Action>();
A3.add(new ClearNodes(this));
A3.add(new TTSReading(this, "Cherchons un QR"));
A3.add(new CaptureQR(this));
A3.add(new QRChecker(this, "36271", 4,2));
A3.add(new ClearNodes(this,3));
A3.add(new TTSReading(this,3, "Cherchons un QR"));
A3.add(new CaptureQR(this,3));
A3.add(new QRChecker(this,3, "36271", 4,2));
ArrayList<Atom> C3 = new ArrayList<Atom>();
C3.add(new ItemAtom(1));
ArrayList<Action> A4 = new ArrayList<Action>();
A4.add(new ClearNodes(this));
A4.add(new TTSReading(this, "Bon QR code"));
A4.add(new TTSReading(this, "Vous avez gagné"));
A4.add(new ClearNodes(this,4));
A4.add(new TTSReading(this,4, "Bon QR code"));
A4.add(new TTSReading(this,4, "Vous avez gagné"));
ArrayList<Atom> C4 = new ArrayList<Atom>();
Node n1 = new Node(1, A1, C1); // node that launch other nodes
Node n2 = new Node(2, A2, C2); // wrong QR code node
Node n3 = new Node(3, A3, C3); // Node that check QR codes (launch and check)
Node n4 = new Node(4, A4, C4); // Node that declare victory
Node n1 = new Node(this,1, A1, C1); // node that launch other nodes
Node n2 = new Node(this,2, A2, C2); // wrong QR code node
Node n3 = new Node(this,3, A3, C3); // Node that check QR codes (launch and check)
Node n4 = new Node(this,4, A4, C4); // Node that declare victory
EveryNode.add(n1);
EveryNode.add(n2);
EveryNode.add(n3);
......@@ -123,8 +123,8 @@ public class MainActivity extends AppCompatActivity {
Log.d("Debug2", "Nodes initialized");
}
protected void CheckOpenNodes() throws InterruptedException {
mainThread.CheckOpenNodes();
public void CheckOpenNodes() {
// mainThread.run();
}
public Node GetNode(int ID)
......@@ -274,7 +274,7 @@ public class MainActivity extends AppCompatActivity {
public void SetListeningResults(String result) // to display listening results
{
Log.d("Debug", "Best confidence result (from main activity): " + result);
Log.d("Debug", "Best confidence result (from main activity): " + result);
Toast.makeText(getApplicationContext(), result, Toast.LENGTH_LONG).show();
}
......
......@@ -5,8 +5,9 @@ import android.util.Log;
import com.example.qrquest.atom.Atom;
import com.example.qrquest.node.Node;
public class MainThread extends Thread {
public class MainThread implements Runnable {
private MainActivity mainActivity;
public Thread NodeThread;
public MainThread(MainActivity mainActivity)
{
......@@ -14,7 +15,7 @@ public class MainThread extends Thread {
this.mainActivity = mainActivity;
}
public void CheckOpenNodes() throws InterruptedException {
public void CheckOpenNodes() {
Log.d("Debug2", "CheckOpenNodes called");
Log.d("Debug2", "Open Nodes:");
for (Node n :
......@@ -30,13 +31,19 @@ public class MainThread extends Thread {
if (n.CheckConditions(mainActivity.Atoms))
{
Log.d("Debug2", "Triggering node " + Integer.toString(n.ID));
Thread NodeThread = new Thread(n);
//NodeThread = new Thread(n);
// n.Trigger();
NodeThread.run();
NodeThread.join();
//NodeThread.run();
//NodeThread.join();
n.Trigger();
//while(NodeThread.isAlive()) {};
this.CheckOpenNodes();
//this.CheckOpenNodes();
}
}
}
@Override
public void run() {
CheckOpenNodes();
}
}
package com.example.qrquest.actions;
import com.example.qrquest.MainActivity;
import com.example.qrquest.node.Node;
public abstract class Action {
MainActivity mainActivity;
public boolean isOver;
private int nodeID;
private Node node;
public Action(MainActivity mainActivity)
public Action(MainActivity mainActivity, int nodeID)
{
isOver = false;
this.mainActivity = mainActivity;
this.nodeID = nodeID;
}
public void Do()
{
throw new Error("Do() Method from action was not overriden");
this.node = mainActivity.GetNode(nodeID); // getting the node in order to call the next action when action is over
//throw new Error("Do() Method from action was not overriden");
}
protected void End() { node.CallNextAction(); }
}
......@@ -4,20 +4,22 @@ import android.util.Log;
import com.example.qrquest.MainActivity;
import com.example.qrquest.atom.Atom;
import com.example.qrquest.node.Node;
public class AddAtom extends Action {
private Atom atom;
public AddAtom(MainActivity mainActivity, Atom atom) {
super(mainActivity);
public AddAtom(MainActivity mainActivity, int nodeID, Atom atom) {
super(mainActivity, nodeID);
this.atom = atom;
}
public void Do()
{
super.Do();
Log.d("Debug2", "Added Atom " + atom.GetStringValue());
mainActivity.Atoms.add((atom));
isOver = true;
End();
}
}
......@@ -9,16 +9,17 @@ public class AddNode extends Action {
private int NodeToAddID;
public AddNode(MainActivity mainActivity, int NodeToAddID) {
super(mainActivity);
public AddNode(MainActivity mainActivity, int nodeID, int NodeToAddID) {
super(mainActivity, nodeID);
this.NodeToAddID = NodeToAddID;
}
public void Do()
{
super.Do();
Node toAdd = mainActivity.GetNode(NodeToAddID);
Log.d("Debug2", "Added Node " + toAdd.ID);
mainActivity.OpenNodes.add(toAdd);
isOver = true;
End();
}
}
......@@ -8,18 +8,19 @@ public class CaptureQR extends Action {
* This action will open the camera to let the user scan a QR code
* It will add the scanned QR code as a new atom
*/
public CaptureQR(MainActivity mainActivity) {
super(mainActivity);
public CaptureQR(MainActivity mainActivity, int nodeID) {
super(mainActivity, nodeID);
}
public void Do()
{
super.Do();
mainActivity.RequestQR(this);
}
public void GiveQRResult(String result)
{
mainActivity.Atoms.add(new QRAtom(result));
isOver = true;
End();
}
}
......@@ -4,13 +4,14 @@ import com.example.qrquest.MainActivity;
public class ClearNodes extends Action {
public ClearNodes(MainActivity mainActivity) {
super(mainActivity);
public ClearNodes(MainActivity mainActivity, int nodeID) {
super(mainActivity, nodeID);
}
public void Do()
{
super.Do();
this.mainActivity.OpenNodes.clear();
isOver = true;
End();
}
}
......@@ -7,8 +7,8 @@ import com.example.qrquest.atom.QRAtom;
public class QRChecker extends Action {
private String WaitedValue;
private int SuccessNodeID, FailNodeID;
public QRChecker(MainActivity mainActivity, String WaitedValue, int SuccessNodeID, int FailNodeID) {
super(mainActivity);
public QRChecker(MainActivity mainActivity, int nodeID, String WaitedValue, int SuccessNodeID, int FailNodeID) {
super(mainActivity, nodeID);
this.WaitedValue = WaitedValue;
this.SuccessNodeID = SuccessNodeID;
this.FailNodeID = FailNodeID;
......@@ -16,6 +16,7 @@ public class QRChecker extends Action {
public void Do()
{
super.Do();
for (Atom a: mainActivity.Atoms
) {
if(a.getClass() == QRAtom.class)
......@@ -25,6 +26,6 @@ public class QRChecker extends Action {
mainActivity.Atoms.remove(a);
}
}
isOver = true;
End();
}
}
......@@ -8,13 +8,14 @@ import com.example.qrquest.atom.Atom;
public class RemoveAtom extends Action {
Atom _atom;
public RemoveAtom(MainActivity mainActivity, Atom atom) {
super(mainActivity);
public RemoveAtom(MainActivity mainActivity, int nodeID, Atom atom) {
super(mainActivity, nodeID);
_atom = atom;
}
public void Do()
{
super.Do();
Atom atomToRemove = null;
for (Atom atom: mainActivity.Atoms
) {
......@@ -27,6 +28,5 @@ public class RemoveAtom extends Action {
mainActivity.Atoms.remove(atomToRemove);
}
else Log.d("Debug2", "Tried to remove an atom that doesn't exist");
isOver = true;
}
End(); }
}
......@@ -6,18 +6,19 @@ import com.example.qrquest.MainActivity;
import com.example.qrquest.node.Node;
public class RemoveNode extends Action {
private int _nodeID;
private int _nodeToRemoveID;
public RemoveNode(MainActivity mainActivity, int nodeID) {
super(mainActivity);
_nodeID = nodeID;
public RemoveNode(MainActivity mainActivity, int nodeID, int nodeToRemoveID) {
super(mainActivity, nodeID);
_nodeToRemoveID = nodeToRemoveID;
}
public void Do()
{
if(!mainActivity.OpenNodes.contains(mainActivity.GetNode(_nodeID))) throw new Error("Tried to remove a node that wasn't open");
Log.d("Debug2", "Removed node " + _nodeID);
mainActivity.OpenNodes.remove(mainActivity.GetNode(_nodeID));
isOver = true;
super.Do();
if(!mainActivity.OpenNodes.contains(mainActivity.GetNode(_nodeToRemoveID))) throw new Error("Tried to remove a node that wasn't open");
Log.d("Debug2", "Removed node " + _nodeToRemoveID);
mainActivity.OpenNodes.remove(mainActivity.GetNode(_nodeToRemoveID));
End();
}
}
......@@ -9,18 +9,19 @@ public class TTSReading extends Action {
private String TextToRead;
public TTSReading(MainActivity mainActivity, String TextToRead)
public TTSReading(MainActivity mainActivity, int nodeID, String TextToRead)
{
super(mainActivity);
super(mainActivity, nodeID);
this.TextToRead = TextToRead;
}
public void Do()
{
super.Do();
// read TextToRead
mainActivity.ttsreader.PlayTTS(TextToRead);
while(!mainActivity.ttsreader.isOver()) {} // block while speaking
isOver = true; // release when over
End();
}
}
package com.example.qrquest.node;
import com.example.qrquest.MainActivity;
import com.example.qrquest.actions.Action;
import com.example.qrquest.atom.Atom;
import java.util.List;
public class Node implements Runnable {
public class Node {
/*
* A node is a potential series of actions for the player to do.
* It triggers when the player has gathered every required atoms
*/
public List<Action> Actions;
public List<Action> Actions; // initialized at the beginning and doesn't change
private List<Action> TODOActions; // copy of Actions that empty itself when actions are done (contains the remaining actions)
public List<Atom> Conditions;
public int ID;
private MainActivity mainActivity;
public Node(int ID, List<Action> Actions, List<Atom> Conditions)
public Node(MainActivity mainActivity, int ID, List<Action> Actions, List<Atom> Conditions)
{
this.mainActivity = mainActivity;
this.ID = ID;
this.Actions = Actions;
this.Conditions = Conditions;
this.TODOActions = Actions;
}
public boolean CheckConditions(List<Atom> AvailableAtoms)
......@@ -36,15 +41,25 @@ public class Node implements Runnable {
public void Trigger()
{
TODOActions.get(0).Do();
/*
for (Action a: Actions
) {
a.Do();
while(!a.isOver){ };
}
}*/
}
@Override
public void run() {
this.Trigger();
public void CallNextAction() {
TODOActions.remove(0);
if(TODOActions.size() > 0)
{
TODOActions.get(0).Do();
}
else
{
mainActivity.CheckOpenNodes();
}
}
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment