Permalink
Browse files

Sprite animations are loaded from entityname.animation.resource in th…

…e resources folder
  • Loading branch information...
1 parent f6745d9 commit 3d379d9c1cc96b637c65fa24f07178ae5c824fac @mandyrosler mandyrosler committed Oct 12, 2011
@@ -23,6 +23,8 @@
05CC7B81143FCE3400052D06 /* DTServer.m in Sources */ = {isa = PBXBuildFile; fileRef = D8B05725143FA20F002CE09B /* DTServer.m */; };
05CC7B82143FCE3900052D06 /* DTPlayer.m in Sources */ = {isa = PBXBuildFile; fileRef = D8B05723143FA20F002CE09B /* DTPlayer.m */; };
05CC7BA61441FC9F00052D06 /* DTEntityEnemyBase.m in Sources */ = {isa = PBXBuildFile; fileRef = 05CC7BA51441FC9F00052D06 /* DTEntityEnemyBase.m */; };
+ 3809AC331444E57500B0C9AF /* DTRenderEntities.m in Sources */ = {isa = PBXBuildFile; fileRef = 3809AC321444E57500B0C9AF /* DTRenderEntities.m */; };
+ 3809AC3614460AFC00B0C9AF /* DTAnimation.m in Sources */ = {isa = PBXBuildFile; fileRef = 3809AC3514460AFC00B0C9AF /* DTAnimation.m */; };
994E55F3144115DF009EC13C /* DTResourceManager.m in Sources */ = {isa = PBXBuildFile; fileRef = 994E55F2144115DF009EC13C /* DTResourceManager.m */; };
994E55F614411609009EC13C /* DTTexture.m in Sources */ = {isa = PBXBuildFile; fileRef = 994E55F514411609009EC13C /* DTTexture.m */; };
994E55F914411645009EC13C /* DTResource.m in Sources */ = {isa = PBXBuildFile; fileRef = 994E55F814411645009EC13C /* DTResource.m */; };
@@ -95,6 +97,10 @@
05CC7B73143FC4BB00052D06 /* SPLowVerbosity.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = SPLowVerbosity.m; sourceTree = "<group>"; };
05CC7BA41441FC9F00052D06 /* DTEntityEnemyBase.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = DTEntityEnemyBase.h; sourceTree = "<group>"; };
05CC7BA51441FC9F00052D06 /* DTEntityEnemyBase.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = DTEntityEnemyBase.m; sourceTree = "<group>"; };
+ 3809AC311444E57500B0C9AF /* DTRenderEntities.h */ = {isa = PBXFileReference; fileEncoding = 4; path = DTRenderEntities.h; sourceTree = "<group>"; };
+ 3809AC321444E57500B0C9AF /* DTRenderEntities.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = DTRenderEntities.m; sourceTree = "<group>"; };
+ 3809AC3414460AFC00B0C9AF /* DTAnimation.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = DTAnimation.h; sourceTree = "<group>"; };
+ 3809AC3514460AFC00B0C9AF /* DTAnimation.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = DTAnimation.m; sourceTree = "<group>"; };
994E55F1144115DF009EC13C /* DTResourceManager.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = DTResourceManager.h; path = Graphics/DTResourceManager.h; sourceTree = "<group>"; };
994E55F2144115DF009EC13C /* DTResourceManager.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = DTResourceManager.m; path = Graphics/DTResourceManager.m; sourceTree = "<group>"; };
994E55F414411609009EC13C /* DTTexture.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = DTTexture.h; path = Graphics/DTTexture.h; sourceTree = "<group>"; };
@@ -219,6 +225,8 @@
994E55FE14412A30009EC13C /* DTImage.m */,
994E56151441CB3C009EC13C /* DTSpriteMap.h */,
994E56161441CB3C009EC13C /* DTSpriteMap.m */,
+ 3809AC3414460AFC00B0C9AF /* DTAnimation.h */,
+ 3809AC3514460AFC00B0C9AF /* DTAnimation.m */,
);
name = Graphics;
sourceTree = "<group>";
@@ -385,6 +393,8 @@
D8B05720143FA202002CE09B /* DTClient.m */,
D822B7C0143FDDF80097FC9E /* DTCamera.h */,
D822B7C1143FDDF80097FC9E /* DTCamera.m */,
+ 3809AC311444E57500B0C9AF /* DTRenderEntities.h */,
+ 3809AC321444E57500B0C9AF /* DTRenderEntities.m */,
);
name = Client;
sourceTree = "<group>";
@@ -537,6 +547,8 @@
994E56171441CB3C009EC13C /* DTSpriteMap.m in Sources */,
05CC7BA61441FC9F00052D06 /* DTEntityEnemyBase.m in Sources */,
052650DC1444FFAF00A881C5 /* NSString+UUID.m in Sources */,
+ 3809AC331444E57500B0C9AF /* DTRenderEntities.m in Sources */,
+ 3809AC3614460AFC00B0C9AF /* DTAnimation.m in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
@@ -15,6 +15,7 @@ typedef void(^DTClientMessageCallback)(NSString *message);
@class DTPhysics;
@class DTServer, DTRoom, DTWorld, DTEntityPlayer, DTLevelRepository;
@class DTCamera;
+@class DTRenderEntities;
@interface DTClient : NSObject
-(id)init; // to local
@@ -42,4 +43,6 @@ typedef void(^DTClientMessageCallback)(NSString *message);
@property(nonatomic,copy) DTClientHealthCallback healthCallback;
@property(nonatomic,copy) DTClientScoresCallback scoresCallback;
@property(nonatomic,copy) DTClientMessageCallback messageCallback;
+
+@property (nonatomic, retain) DTRenderEntities *entityRenderer;
@end
@@ -25,6 +25,7 @@
#import "DTResourceManager.h"
#import "DTTexture.h"
#import "DTSpriteMap.h"
+#import "DTRenderEntities.h"
@interface DTClient () <TCAsyncHashProtocolDelegate>
@property (nonatomic, strong) DTResourceManager *resources;
@@ -39,6 +40,7 @@ @implementation DTClient {
@synthesize rooms, playerEntity, levelRepo, currentRoom;
@synthesize camera;
@synthesize resources, healthCallback, scoresCallback, messageCallback;
+@synthesize entityRenderer;
-(id)init;
{
@@ -49,6 +51,7 @@ -(id)initConnectingTo:(NSString *)host port:(NSUInteger)port;
if(!(self = [super init])) return nil;
self.resources = [[DTResourceManager alloc] initWithBaseURL:[[NSBundle mainBundle] URLForResource:@"resources" withExtension:nil]];
+ self.entityRenderer = [[DTRenderEntities alloc] init];
AsyncSocket *socket = [[AsyncSocket alloc] initWithDelegate:self];
socket.delegate = _proto = [[TCAsyncHashProtocol alloc] initWithSocket:socket delegate:self];
@@ -104,6 +107,8 @@ -(void)tick:(double)delta;
[physics runWithEntities:currentRoom.entities.allValues world:currentRoom.world delta:delta];
if(self.healthCallback) self.healthCallback(followThis.maxHealth, followThis.health);
+
+ [self.entityRenderer tick:delta];
}
-(void)draw;
@@ -147,52 +152,7 @@ -(void)draw;
}
//glDisable(GL_TEXTURE_2D);
-
- DTSpriteMap *defaultSprite = [resources spriteMapNamed:@"sten.spritemap"];
- //[defaultSprite.texture use];
- //DTSpriteMapFrame frame = [defaultSprite frameAtIndex:0];
-
- glTranslatef(-camera.position.x, -camera.position.y, 0);
-
- for(DTEntity *entity in currentRoom.entities.allValues) {
-
- if([$castIf(DTEntityPlayer, entity) immune] && (frameCount/2)%2)
- continue;
-
- DTSpriteMap *sprite = entity.walkSprite;
- if (sprite == nil) sprite = defaultSprite;
- [sprite.texture use];
- DTSpriteMapFrame frame = [sprite frameAtIndex:0];
-
- glPushMatrix();
- glTranslatef(entity.position.x, entity.position.y, 0);
- glTranslatef(entity.size.x/2, entity.size.y/2, 0);
- glRotatef(entity.rotation, 0, 0, 1);
- glTranslatef(-entity.size.x/2, -entity.size.y/2, 0);
- glBegin(GL_QUADS);
-
- if(entity.damageFlashTimer > 0)
- frame = [sprite frameAtIndex:1]; // TODO: set a correct sprite frame here...
- else {
- //frame = [sprite frameAtIndex:0];
- frame = [sprite frameAtIndex:entity.currentWalkSpriteFrame];
- }
-
- glColor3f(1., 1., 1.);
- glTexCoord2fv(&frame.coords[0]); glVertex3f(entity.size.x, 0., 0.);
- glTexCoord2fv(&frame.coords[2]); glVertex3f(entity.size.x, entity.size.y, 0.);
- glTexCoord2fv(&frame.coords[4]); glVertex3f(0., entity.size.y, 0);
- glTexCoord2fv(&frame.coords[6]); glVertex3f(0., 0., 0.);
- glEnd();
- glColor3f(0,0,1.);
- glBegin(GL_POINTS);
- if(entity.lookDirection == EntityDirectionLeft)
- glVertex3f(0, entity.size.y/3, 0);
- else if(entity.lookDirection == EntityDirectionRight)
- glVertex3f(entity.size.x, entity.size.y/3, 0);
- glEnd();
- glPopMatrix();
- }
+ [self.entityRenderer draw:camera frameCount:frameCount];
}
@@ -273,6 +233,7 @@ -(void)command:(id)proto addEntity:(NSDictionary*)hash;
ent.uuid = key;
ent.world = room.world;
+ [self.entityRenderer addEntity:ent];
[room.entities setObject:ent forKey:key];
}
-(void)command:(id)proto removeEntity:(NSDictionary*)hash;
@@ -282,6 +243,7 @@ -(void)command:(id)proto removeEntity:(NSDictionary*)hash;
DTRoom *room = [rooms objectForKey:roomName];
if(!room) return;
+ [self.entityRenderer removeEntity:[room.entities objectForKey:key]];
[room.entities removeObjectForKey:key];
}
@@ -294,6 +256,8 @@ -(void)command:(id)proto playerEntity:(NSDictionary*)hash;
currentRoom = room;
playerEntity = [room.entities objectForKey:key];
+
+ [self.entityRenderer setEntitiesToDraw:currentRoom.entities.allValues];
}
-(void)command:(id)proto cameraFollow:(NSDictionary*)hash;
View
@@ -0,0 +1,25 @@
+//
+// DTAnimation.h
+// Deathtroid
+//
+// Created by Amanda Rösler on 2011-10-12.
+// Copyright (c) 2011 __MyCompanyName__. All rights reserved.
+//
+
+#import <Foundation/Foundation.h>
+#import "DTResource.h"
+#import "DTSpriteMap.h"
+
+@interface DTAnimation : DTResource
+
+@property (nonatomic, retain) DTSpriteMap *spriteMap;
+
+- (DTSpriteMapFrame)frameAtIndex:(NSInteger)frameIndex forAnimation:(NSString*)animationName;
+- (NSUInteger) frameCountForAnimation:(NSString*)animationName;
+- (NSUInteger) framesPerSecondForAnimation:(NSString*)animationName;
+
+@end
+
+@interface DTResourceManager (DTAnimation)
+-(DTAnimation *)animationNamed:(NSString *)name;
+@end
View
@@ -0,0 +1,88 @@
+//
+// DTAnimation.m
+// Deathtroid
+//
+// Created by Amanda Rösler on 2011-10-12.
+// Copyright (c) 2011 __MyCompanyName__. All rights reserved.
+//
+
+#import "DTAnimation.h"
+#import "DTSpriteMap.h"
+#import "DTTexture.h"
+#import "DTResource.h"
+
+@interface DTAnimation ()
+
+@property (nonatomic, retain) NSMutableDictionary *animations;
+
+@end
+
+
+@implementation DTAnimation
+
+@synthesize spriteMap;
+@synthesize animations;
+
+-(id)initWithResourceId:(NSString *)resourceId spriteMap:(DTSpriteMap*)spriteMap_ animations:(NSMutableDictionary*)animations_
+{
+ if(![self initWithResourceId:resourceId]) return nil;
+
+ self.spriteMap = spriteMap_;
+ self.animations = animations_;
+
+ return self;
+}
+
+-(DTSpriteMapFrame)frameAtIndex:(NSInteger)frameIndex forAnimation:(NSString*)animationName {
+
+ NSDictionary *animation = [self.animations objectForKey:animationName];
+ NSArray *frames = [animation objectForKey:@"tiles"];
+ return [self.spriteMap frameAtIndex:[[frames objectAtIndex:frameIndex] intValue]];
+}
+
+- (NSUInteger) frameCountForAnimation:(NSString*)animationName {
+
+ NSDictionary *animation = [self.animations objectForKey:animationName];
+ return [(NSArray*)[animation objectForKey:@"tiles"] count];
+}
+
+- (NSUInteger) framesPerSecondForAnimation:(NSString*)animationName {
+
+ NSDictionary *animation = [self.animations objectForKey:animationName];
+ return [[animation objectForKey:@"fps"] intValue];
+}
+
+@end
+
+@interface DTAnimationLoader : DTResourceLoader
+@end
+
+@implementation DTAnimationLoader
++(void)load{
+ [DTResourceManager registerResourceLoader:self withTypeName:@"animation"];
+}
+
+-(id<DTResource>)loadResourceAtURL:(NSURL *)url usingManager:(DTResourceManager *)manager
+{
+ [super loadResourceAtURL:url usingManager:manager];
+
+ DTSpriteMap *spriteMap = [manager spriteMapNamed:[self.definition objectForKey:@"spriteMap"]];
+
+ NSMutableDictionary *animations = [NSMutableDictionary dictionary];
+ NSArray *listOfAnimations = [self.definition objectForKey:@"animations"];
+ for (NSDictionary *animation in listOfAnimations) {
+ [animations setObject:[animation objectForKey:@"data"] forKey:(NSString*)[animation objectForKey:@"name"]];
+ }
+
+ DTAnimation *animation = [[DTAnimation alloc] initWithResourceId:url.dt_resourceId spriteMap:spriteMap animations:animations];
+
+ return animation;
+}
+
+@end
+
+@implementation DTResourceManager (DTAnimation)
+-(DTAnimation *)animationNamed:(NSString *)name;{
+ return [self resourceNamed:name];
+}
+@end
View
@@ -13,6 +13,7 @@
@class DTWorld;
@class DTTraceResult;
@class DTSpriteMap;
+@class DTAnimation;
#define $doif(key, then) ({id o = [rep objectForKey:key]; if(o) { then; } });
@@ -56,8 +57,6 @@ typedef struct {
-(BOOL)damage:(int)damage from:(Vector2*)damagerLocation killer:(DTEntity*)killer;
-(void)remove;
-- (void) animateWalk:(double)delta;
-
@property (nonatomic) int health;
@property (nonatomic) int maxHealth;
@property (nonatomic) BOOL destructible;
@@ -77,11 +76,11 @@ typedef struct {
@property (nonatomic,strong) NSString *uuid;
@property (nonatomic,readonly) NSString *typeName;
+@property (nonatomic, retain) NSString *currentState;
+
// protected
-@property (nonatomic, retain) DTSpriteMap *walkSprite;
-@property (nonatomic, assign) int currentWalkSpriteFrame;
-@property (nonatomic, assign) float walkAnimationCounter;
+@property (nonatomic, retain) DTAnimation* animation;
@property (nonatomic, assign) float rotation;
@end
Oops, something went wrong.

0 comments on commit 3d379d9

Please sign in to comment.