Skip to content

Commit

Permalink
Some bugfixes/entity API tweaks.
Browse files Browse the repository at this point in the history
  • Loading branch information
Gayo committed May 16, 2012
1 parent f7e20f9 commit ecfc802
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 25 deletions.
1 change: 1 addition & 0 deletions Examples/Sully/Sully/GlobalScripts/Party.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ public static void MapSwitch( string map, int x, int y, bool pixel_coordinates )

for( int i = 1; i<pm.Length; i++ ) {
pm[i].ent = _.sg.map.spawn_entity(0, 0, pm[i].normal_chr );
pm[i].ent.name = "Party Member: " + pm[i].name;
_.sg.followers.add( pm[i].ent );
}

Expand Down
8 changes: 3 additions & 5 deletions Examples/Sully/Sully/MapScripts/ParadiseIsle.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,7 @@ public void start() {
// pre-built into the map...
if( _.getFlagB( _.F.F_CRYS_JOIN ) ) {

this.map.entities[0].x = 30000;
this.map.entities[0].speed = 0;
this.map.entities[0].visible = false;
}

//if Sully's shown the way
Expand Down Expand Up @@ -214,10 +213,9 @@ public void crystal_event( Entity ent ) {

_.sg.textbox.OnDone = () => {

map.delete_entity(map.entities[0]);
_.sg.party.AddPartyMember( "Crystal", 3 );


map.entities[0].visible = false;
_.sg.followers.add(PartyData.partymemberData["crystal"].ent);

_.setFlag( _.F.F_CRYS_JOIN, 1 );
};
Expand Down
3 changes: 2 additions & 1 deletion Examples/Sully/Sully/Party.cs
Original file line number Diff line number Diff line change
Expand Up @@ -247,7 +247,8 @@ public void AddPartyMember( string name, int level ) {
}

if( pm.ent == null && _.sg.map != null) {
pm.ent = new Entity( pm.normal_chr, "" );
pm.ent = _.sg.map.spawn_entity(-10, -10, pm.normal_chr);
pm.ent.name = "Party Member: " + pm.name;
}

if( pm.level < level ) {
Expand Down
8 changes: 5 additions & 3 deletions XNAVERGE/core/VERGEGame_Draw.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@ public partial class VERGEGame {
/// This is called when the game should draw itself.
/// </summary>
/// <param name="gameTime">Provides a snapshot of timing values.</param>
protected override void Draw(GameTime gameTime) {
protected override void Draw(GameTime gameTime) {
Entity ent;
base.Draw(gameTime); // not sure if this actually does anything
if (map == null) return;

Expand All @@ -44,8 +45,9 @@ protected override void Draw(GameTime gameTime) {
fps++;

// Update entity frames
if (!entities_paused) for (int i = 0; i < map.num_entities; i++) { map.entities[i].advance_frame(); }
else for (int i = 0; i < map.num_entities; i++) { map.entities[i].last_draw_tick = tick; }
map.start_listing_entities();
if (!entities_paused) { while (map.get_next_entity(out ent)) { ent.advance_frame(); } }
else { while (map.get_next_entity(out ent)) { ent.last_draw_tick = tick; } }

// Draw to native-size buffer at 1x size
GraphicsDevice.SetRenderTarget(screen.true_size_buffer);
Expand Down
10 changes: 6 additions & 4 deletions XNAVERGE/core/VERGEGame_Logic.cs
Original file line number Diff line number Diff line change
Expand Up @@ -82,8 +82,8 @@ private void _handleMapMovementInput() {
prev_player_coords.X /= map.tileset.tilesize;
prev_player_coords.Y /= map.tileset.tilesize;
} else prev_player_coords = default( Point );
for( int i = 0; i < map.num_entities; i++ ) {
ent = map.entities[i];
map.start_listing_entities();
while (map.get_next_entity(out ent)) {
if (followers.position_of(ent) < 0) ent.Update();
}
followers.Update();
Expand Down Expand Up @@ -130,8 +130,10 @@ private void _handleMapMovementInput() {
// Possibly this should be converted to something that's called in all cases, handling only
// essentials.
private void _idleMap(int elapsed) {
for (int i=0; i < map.num_entities; i++) {
map.entities[i].last_logic_tick += elapsed;
Entity ent;
map.start_listing_entities();
while (map.get_next_entity(out ent)) {
ent.last_logic_tick = ent.last_draw_tick = tick;
}
}

Expand Down
36 changes: 24 additions & 12 deletions XNAVERGE/map/VERGEMap.cs
Original file line number Diff line number Diff line change
Expand Up @@ -75,22 +75,35 @@ public void set_renderstring(String new_renderstring) {
// ---------------------------------
// ENTITY MANAGEMENT

public Entity get_entity(String ent_name) {
for (int i = 0; i < _num_entities; i++) {
if (entities[i].name == ent_name) return entities[i];
// Pseudo-enumerator API. Too lazy to implement/optimize a real enumerator for now.

protected int enum_idx = 0;
// resets the enumeration pointer to the first entity
public void start_listing_entities() {
enum_idx = 0;
}
// advance through entity enumerator. Note that this may be fewer entities than num_entities, since the
// latter counts lazily-deleted entities as well.
public bool get_next_entity(out Entity ent) {
ent = null;
while (enum_idx < num_entities) {
if (!entities[enum_idx].deleted) {
ent = entities[enum_idx];
enum_idx++;
return true;
}
enum_idx++;
}
return null;
return false;
}

// Returns a (possibly empty) list of all entities whose hitboxes intersect with the given rectangle.
public List<Entity> entities_in_region(Rectangle region) {
List<Entity> list = new List<Entity>(_num_entities); // initial list capacity = number of entities on map

public Entity get_entity(String ent_name) {
for (int i = 0; i < _num_entities; i++) {
if (entities[i].hitbox.Intersects(region)) list.Add(entities[i]);
if (entities[i].name == ent_name && !entities[i].deleted) return entities[i];
}
return list;
return null;
}


public Entity spawn_entity(String ent_name, int x_coord, int y_coord, Direction facing, String asset_name, String animation) {
Entity ent = new Entity(asset_name, ent_name);
Expand Down Expand Up @@ -120,7 +133,6 @@ public Entity spawn_entity(String ent_name, int x_coord, int y_coord, Direction
//Console.WriteLine("{0} is #{1}", ent.name, ent.index);
return ent;
}
// overload ALL the things!
public Entity spawn_entity(int x_coord, int y_coord, Direction facing, String asset_name, String animation) {
return spawn_entity("", x_coord, y_coord, facing, asset_name, animation);
}
Expand Down Expand Up @@ -150,7 +162,7 @@ public Entity spawn_entity(String ent_name, int x_coord, int y_coord, String ass
public bool delete_entity(Entity entity) {
VERGEGame.game.entity_space.Remove(entity);
for (int i = 0; i < _num_entities; i++) {
if (entities[i] == entity) {
if (entities[i] == entity && !entities[i].deleted) {
entity.deleted = true;
if (VERGEGame.game.player == entity) VERGEGame.game.player = null;
return true;
Expand Down

0 comments on commit ecfc802

Please sign in to comment.