Commit fb6639d6 authored by Rhys Buckham's avatar Rhys Buckham

first commit

parents
File added
#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;
}
#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;
};
#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);
// Workshop 3 part 3 -- set initial player/npc positions and size
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";
ofxXmlSettings xmlFile;
//if an fml life is read do this
if(xmlFile.loadFile("dialogueDemoXML.xml")){
//variable to store the number of utterances from the file
int numberOfUtterances = xmlFile.getNumTags("utterance");
//loop through that number
for(int i = 0; i <numberOfUtterances;++i){
//create dialogue object
Dialogue dial;
//loop through the utterances
dial.id = xmlFile.getAttribute("utterance", "id", -1,i);
xmlFile.pushTag("utterance",i);
dial.speaker = xmlFile.getValue("speaker","N/A");
dial.target = xmlFile.getValue("target","N/A");
dial.dialogue = xmlFile.getValue("dialogue","N/A");
string loops = xmlFile.getValue("loop","N/A");
if (loops == "true"){
dial.loop = true;
}
if(loops == "false"){
dial.loop = false;
}
xmlFile.popTag();
//push them into the vector
di.push_back(dial);
//
//
}
};
player.name = "player";
reset();
}
bool ofApp:: checkPrecondition(string prereq){
if(prereq.front() == '!'){
prereq.erase(0);
}
if(prereq.size() == 0){
return false;
}
else{
return true;
}
}
bool ofApp::checkAllPreconditions(vector<string>prereqs){
// for(int i = 0;i< prereqs.size();++i){
// if(checkPrecondition()== true){
// return true;
// }
//
}
//--------------------------------------------------------------
void ofApp::update(){
// Workshop 3 Step 6 -- Moved from keyPressed
ofVec3f oldPos = player.body.getPosition();
player.update();
// -- Moved from keyPressed
for(int i =0;i<5;++i){
if (player.collide(npcs[i].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();
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();
for(int i = 0; i < npcs.size(); ++i){
npcs[i].reset();
}
}
#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);
bool checkPrecondition(string prereq);
bool checkAllPreconditions(vector<string>prereqs);
// 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> di;
};
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