ccdevnet / openc2e

openc2e

openc2e / SimpleAgent.cpp
100644 97 lines (87 sloc) 2.985 kb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
/*
* SimpleAgent.cpp
* openc2e
*
* Created by Alyssa Milburn on Tue May 25 2004.
* Copyright (c) 2004 Alyssa Milburn. All rights reserved.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
*/
 
#include "SimpleAgent.h"
#include <string>
#include <fstream>
#include <iostream>
#include "c16Image.h"
#include "openc2e.h"
 
SimpleAgent::SimpleAgent(unsigned char family, unsigned char genus, unsigned short species, unsigned int plane,
unsigned int firstimage, unsigned int imagecount) :
Agent(family, genus, species, plane) {
first_image = firstimage;
image_count = imagecount;
setAttributes(0);
pose = 0;
}
 
void SimpleAgent::setImage(std::string img) {
sprite = gallery.getImage(img);
assert(sprite);
}
 
void SimpleAgent::setAttributes(unsigned int attr) {
carryable = (attr & 1);
mouseable = (attr & 2);
activateable = (attr & 4);
invisible = (attr & 16);
floatable = (attr & 32);
suffercollisions = (attr & 64);
sufferphysics = (attr & 128);
camerashy = (attr & 256);
rotatable = (attr & 1024);
presence = (attr & 2048);
}
 
unsigned int SimpleAgent::getAttributes() {
unsigned int a = (carryable ? 1 : 0);
a += (mouseable ? 2: 0);
a += (activateable ? 4: 0);
a += (invisible ? 16: 0);
a += (floatable ? 32: 0);
a += (suffercollisions ? 64: 0);
a += (sufferphysics ? 128: 0);
a += (camerashy ? 256: 0);
a += (rotatable ? 1024: 0);
return a + (presence ? 2048: 0);
}
 
void SimpleAgent::setFrameNo(unsigned int f) {
assert(f < animation.size());
frameno = f;
pose = animation[frameno]; // TODO: should start from BASE
}
 
void SimpleAgent::tick() {
Agent::tick();
if (!animation.empty()) {
unsigned int f = frameno + 1;
if (f == animation.size()) return;
if (animation[f] == 255) {
if (f == (animation.size() - 1)) f = 0;
else f = animation[f + 1];
}
setFrameNo(f);
}
}
 
void SimpleAgent::render(SDLBackend *renderer, int xoffset, int yoffset) {
int xoff = xoffset + (int)x;
int yoff = yoffset + (int)y;
renderer->render(getSprite(), getCurrentSprite(), xoff, yoff);
renderer->renderLine(xoff + (getWidth() / 2), yoff, xoff + getWidth(), yoff + (getHeight() / 2), 0xFF0000CC);
renderer->renderLine(xoff + getWidth(), yoff + (getHeight() / 2), xoff + (getWidth() / 2), yoff + getHeight(), 0xFF0000CC);
renderer->renderLine(xoff + (getWidth() / 2), yoff + getHeight(), xoff, yoff + (getHeight() / 2), 0xFF0000CC);
renderer->renderLine(xoff, yoff + (getHeight() / 2), xoff + (getWidth() / 2), yoff, 0xFF0000CC);
}
 
/* vim: set noet: */