Commit 1097f583 authored by Hristo Yordanov's avatar Hristo Yordanov

initial commit

parents
<utterance id="0">
<speaker>npcA</speaker>
<target>player</target>
<prereqs></prereqs>
<loop>false</loop>
<dialogue>Hullo</dialogue>
</utterance>
<utterance id="1">
<speaker>npcB</speaker>
<target>player</target>
<prereqs></prereqs>
<loop>true</loop>
<dialogue>Hi!</dialogue>
</utterance>
<utterance id="2">
<speaker>npcC</speaker>
<target>player</target>
<prereqs></prereqs>
<loop>false</loop>
<dialogue>Yo.</dialogue>
</utterance>
<utterance id="3">
<speaker>npcC</speaker>
<target>player</target>
<prereqs>
<prereq>2</prereq>
</prereqs>
<loop>true</loop>
<dialogue>Dawg.</dialogue>
</utterance>
<utterance id="4">
<speaker>npcD</speaker>
<target>player</target>
<prereqs></prereqs>
<loop>false</loop>
<dialogue>hi hello how are you i am fine yes i have a lot to say thank you very much and no you shouldn't take any pauses or breaths here because that would be silly you lung sack having meat bag HAHAHAHAAAA</dialogue>
</utterance>
<utterance id="5">
<speaker>npcD</speaker>
<target>player</target>
<prereqs>
<prereq>8</prereq>
</prereqs>
<loop>false</loop>
<dialogue>...Sorry about that.</dialogue>
</utterance>
<utterance id="6">
<speaker>npcE</speaker>
<target>player</target>
<prereqs>
<prereq>!4</prereq>
</prereqs>
<loop>false</loop>
<dialogue>npcD is spouting some nonsense. Come back after you finish talking to them.</dialogue>
</utterance>
<utterance id="7">
<speaker>npcE</speaker>
<target>player</target>
<prereqs>
<prereq>4</prereq>
</prereqs>
<loop>true</loop>
<dialogue>Not all of us think you are a meat bag. There are bones in there too, right?</dialogue>
</utterance>
<utterance id="8">
<speaker>player</speaker>
<target>npcD</target>
<prereqs>
<prereq>4</prereq>
</prereqs>
<loop>false</loop>
<dialogue>That's not very nice...</dialogue>
</utterance>
<utterance id="9">
<speaker>player</speaker>
<target>npcD</target>
<prereqs>
<prereq>5</prereq>
<prereq>7</prereq>
</prereqs>
<loop>true</loop>
<dialogue>Hrmph.</dialogue>
</utterance>
<utterance id="10">
<speaker>player</speaker>
<target>npcC</target>
<prereqs>
<prereq>3</prereq>
</prereqs>
<loop>true</loop>
<dialogue>Dawg.</dialogue>
</utterance>
#include "Character.h"
/* In the .h
ofPlanePrimitive body;
ofVec3f startPos;
ofVec3f velocity;
ofColor color;
string name;
*/
Character::Character() {
// random size
body.set(ofRandom(30, 50), ofRandom(30, 50));
// random color
color = ofColor(ofRandom(40, 240), ofRandom(40, 240), ofRandom(40, 240));
// Spawn between 10% and 90% of the field height and width.
startPos = ofVec3f(ofRandom(ofGetWidth()*.1, ofGetWidth()*.9), ofRandom(ofGetHeight() * .1, ofGetHeight() * .9), 0);
// Set position and velocity
reset();
}
void Character::update() {
// Workshop 3 Step 6 -- simple addition of velocity every frame
body.setPosition(body.getPosition() + velocity);
}
void Character::draw(){
ofSetColor(color);
body.draw();
// labeling the player
ofSetColor(ofColor::black);
ofDrawBitmapString(name, body.getX() - body.getWidth() / 2, body.getY() + body.getHeight() / 2 + 10);
}
void Character::reset() {
body.setPosition(startPos);
velocity.set(0, 0, 0);
}
// Workshop 4: Moved collision here, changed it to work as this character == p1
bool Character::collide(ofPlanePrimitive p2) {
bool collide = false;
float p1x = body.getX() - body.getWidth() / 2;
float p1y = body.getY() - body.getHeight() / 2;
float p2x = p2.getX() - p2.getWidth() / 2;
float p2y = p2.getY() - p2.getHeight() / 2;
// The mighty if statement:
// logic: https://developer.mozilla.org/en-US/docs/Games/Techniques/2D_collision_detection
// "The algorithm works by ensuring there is no gap between any of the 4 sides of the rectangles.
// Any gap means a collision does not exist."
if (p1x < p2x + p2.getWidth() &&
p1x + body.getWidth() > p2x &&
p1y < p2y + p2.getHeight() &&
p1y + body.getHeight() > p2y) {
// Collision detected
collide = true;
}
return collide;
}
#pragma once
#include "ofMain.h"
class Character {
public:
Character();
void update();
void draw();
void reset();
bool collide(ofPlanePrimitive p2);
ofPlanePrimitive body;
ofVec3f startPos;
ofVec3f velocity;
ofColor color;
string name;
};
\ No newline at end of file
#include "Dialogue.h"
/* In .h
int id;
string speaker;
string target;
vector<string> prereqs;
bool loop;
string dialogue;
bool started;
bool read;
int timer;
bool looped;
*/
Dialogue::Dialogue() {
reset();
}
void Dialogue::update() {
// Don't do anything if we haven't triggered the utterance by starting it
// On collision, we set started = true, which should start this timer if Dialogues are being updated
// Also only update the timer if we haven't finished reading it yet
if (started && !read) {
if (timer > 0) {
// timer is running and we're not done yet! update the timer and wait
timer -= 1;
}
else {
// timer has hit zero or lower. We're done.
read = true;
// Well, almost. if we want to loop, we need to reset the dialogue timer so it can show up again.
if (loop) {
reset();
looped = true;
}
}
}
}
void Dialogue::reset() {
// Sets the triggering and displaying timer variables to their beginning
started = false;
read = false;
timer = 360; // 360 is 3 seconds at 60 frames per second. 6 seconds at 30 FPS
looped = false;
}
\ No newline at end of file
#pragma once
#include <string>
#include <vector>
using namespace std; // using directive, no longer need to use std::
class Dialogue {
public:
Dialogue();
//functions for displaying the dialogue for a duration
void update();
void reset();
// Data variables. All of these are read from the file and stored
int id;
string speaker;
string target;
vector<string> prereqs;
bool loop;
string dialogue;
// Timing/usage variables. These are behvaiors we program
bool started;
int timer;
// to prevent looping if necessary
bool read;
// Keep track of if we've completed a loop of a looping dialogue option
// Should always be false if a dialogue doesn't loop
bool looped;
};
\ No newline at end of file
#include "ofMain.h"
#include "ofApp.h"
//========================================================================
int main( ){
ofSetupOpenGL(1024,768,OF_WINDOW); // <-------- setup the GL context
// this kicks off the running of my app
// can be OF_WINDOW or OF_FULLSCREEN
// pass in width and height too:
ofRunApp(new ofApp());
}
#include "ofApp.h"
#include "ofxXmlSettings.h"
//--------------------------------------------------------------
void ofApp::setup(){
// Workshop 3 part 2: initialize background, player, and npc with positions
background.set(ofGetWidth(), ofGetHeight());
background.setPosition(ofGetWidth()/2, ofGetHeight()/2, 0);
player.name = "player";
for (int i = 0; i < 5; i++) {
Character c;
npcs.push_back(c);
}
npcs[0].name = "npcA";
npcs[1].name = "npcB";
npcs[2].name = "npcC";
npcs[3].name = "npcD";
npcs[4].name = "npcE";
// Workshop 3 part 3 -- set initial player/npc positions and size
reset();
ofxXmlSettings xmlFile;
if (xmlFile.loadFile("dialogueDemoXML.xml")) {
int numberOfUtterances = xmlFile.getNumTags("utterance");
for (int i = 0; i < numberOfUtterances; i++)
{
Dialogue diag;
diag.id = xmlFile.getAttribute("utterance", "id", -1, i);
//we finish xml reading
xmlFile.pushTag("utterance", i);
diag.speaker = xmlFile.getValue("speaker", "N/A");
diag.target = xmlFile.getValue("target", "N/A");
diag.dialogue = xmlFile.getValue("dialogue", "N/A");
string loop = xmlFile.getValue("loop", "N/A");
// check if the string == "true" or == "false" and set diag.loop = result;
xmlFile.popTag();
dialogues.push_back(diag);
}
}
}
//--------------------------------------------------------------
void ofApp::update(){
// Workshop 3 Step 6 -- Moved from keyPressed
ofVec3f oldPos = player.body.getPosition();
player.update();
// -- Moved from keyPressed
if (player.collide(npc.body)) {
player.body.setPosition(oldPos);
// EXTRA? Set velocity to zero as well. It makes more sense.
player.velocity.set(0, 0, 0);
}
}
//--------------------------------------------------------------
void ofApp::draw(){
// workshop 3 part 2 optional - set of background to an obvious color.
// We should never see this.
ofBackground(ofColor::black);
// Workshop 3 part 2: draw the background and player primitives
ofSetColor(ofColor::cornflowerBlue);
background.draw();
// Workshop 4
player.draw();
//npc.draw();
for (int i = 0; i < npcs.size(); i++)
{
npcs[i].draw();
}
}
//--------------------------------------------------------------
void ofApp::keyPressed(int key){
// Workshop 3 part 3: WASD for the player cube
// Step 6 is to convert these to change velocity instead
float stepsize = 10;
float velocityStepSize = 1;
// Workshop 4 moves velocity to the character class
if (key == 'w') {
player.velocity.y -= velocityStepSize;
}
if (key == 's') {
player.velocity.y += velocityStepSize;
}
if (key == 'a') {
player.velocity.x -= velocityStepSize;
}
if (key == 'd') {
player.velocity.x += velocityStepSize;
}
// reset
if (key == 'r') {
reset();
}
}
//--------------------------------------------------------------
void ofApp::keyReleased(int key){
}
//--------------------------------------------------------------
void ofApp::mouseMoved(int x, int y ){
}
//--------------------------------------------------------------
void ofApp::mouseDragged(int x, int y, int button){
}
//--------------------------------------------------------------
void ofApp::mousePressed(int x, int y, int button){
}
//--------------------------------------------------------------
void ofApp::mouseReleased(int x, int y, int button){
}
//--------------------------------------------------------------
void ofApp::mouseEntered(int x, int y){
}
//--------------------------------------------------------------
void ofApp::mouseExited(int x, int y){
}
//--------------------------------------------------------------
void ofApp::windowResized(int w, int h){
}
//--------------------------------------------------------------
void ofApp::gotMessage(ofMessage msg){
}
//--------------------------------------------------------------
void ofApp::dragEvent(ofDragInfo dragInfo){
}
// Workshop 3 part 3
// Resets the player and npc's starting positions. Also sets their size, why not?
// Workshop 4 moves this to the character class.
void ofApp::reset() {
player.reset();
//npc.reset();
for (int i = 0; i < npcs.size(); i++)
{
npcs[i].reset();
}
}
\ No newline at end of file
#pragma once
#include "ofMain.h"
#include "Character.h"
#include "Dialogue.h"
class ofApp : public ofBaseApp{
public:
void setup();
void update();
void draw();
void keyPressed(int key);
void keyReleased(int key);
void mouseMoved(int x, int y );
void mouseDragged(int x, int y, int button);
void mousePressed(int x, int y, int button);
void mouseReleased(int x, int y, int button);
void mouseEntered(int x, int y);
void mouseExited(int x, int y);
void windowResized(int w, int h);
void dragEvent(ofDragInfo dragInfo);
void gotMessage(ofMessage msg);
// Workshop 3 Step 2 -- initialzie the ofPlanePrimitives
//ofPlanePrimitive npc;
//ofPlanePrimitive player;
ofPlanePrimitive background;
// Workshop 3 Step 3 -- reset
void reset();
// Workshop 4
Character player;
Character npc;
vector<Character> npcs;
vector<Dialogue> dialogues;
};
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